【问题标题】:Reusing a blaze template, how do I access other template's helper functions?重复使用 blaze 模板,如何访问其他模板的辅助函数?
【发布时间】:2019-03-25 19:09:57
【问题描述】:

我正在尝试设置 MDC 对话框警告。我没有将它复制粘贴到每个需要它的视图中,而是将对话框包装在它自己的模板中。该模板似乎工作,对话框打开并正常运行,但是,我无法为其设置一个有效的辅助函数。我尝试使用父模板的辅助函数,甚至将新模板创建为自己的 js 文件。这些解决方案都不能正确抓取数据。

<template name="transactionAlert">
...
<div class="mdc-dialog__content" ><p>Are you sure you wish to continue with this transaction? It could cost up to: <b class="warning-value">${{maxCost}} USD</b></p>
...
</template>
<template name="transactionCreate">
...
    {{>transactionAlert}}
</template>
Template.transactionAlert.onCreated(function transactionAlertOnCreated() {
    console.log('test')
})

Template.transactionAlert.helpers({
    maxCost(){
        console.log('test 2')
        const instance = Template.instance()
        return instance.maxTxCost.get().toString().slice(0,5);
    }
})

【问题讨论】:

    标签: javascript meteor meteor-blaze mdc-components


    【解决方案1】:

    我尝试使用父模板的辅助函数

    此类问题通常是由设计问题引起的,而不是缺少或错误的实现。如果我们认为您的 transactionAlert 是无状态的(它不包含任何相关的视图逻辑或内部状态管理),那么它也不应该访问超出其范围的属性或助手。

    否则,您将创建如此紧密的耦合,以至于它会在两年左右(重构会话调用时)重新出现在您的面前。

    相比之下,父模板的职责是

    • 管理数据状态(订阅、数据后处理等)
    • 检查条件,transactionAlert 应该出现还是消失
    • 将正确的参数传递给transactionAlert模板

    因此,您可以将交易警报设计为参数化模板:

    <template name="transactionAlert">
    ...
    <div class="mdc-dialog__content" ><p>Are you sure you wish to continue with this transaction? It could cost up to: <b class="warning-value">${{maxCost}} USD</b></p>
    ...
    </template> 
    

    如您所见,它看起来完全一样。不同之处在于,您删除了 Template.transactionAlert.helpers 并导致模板查找 maxCost 被传递给模板。

    现在在您的父模板中,一旦警报条件适用,您将把数据传递给transactionalert

    <template name="transactionCreate">
      {{#if showAlert}}
        {{>transactionAlert maxCost=getMaxCost}}
      {{/if}}
    </template>
    

    助手现在在哪里:

    Template.transactionCreate.helpers({
        showAlert () {
          return Template.instance().showAlert.get()
        },
        getMaxCost(){
          const instance = Template.instance()
          return instance.maxTxCost.get().toString().slice(0,5);
        }
    })
    

    因为您需要反应性来显示/隐藏您将使用模板的内部跟踪器的警报:

    Template.transactionCreate.onCreated(function () {
      const instance = this
      instance.showAlert = new ReactiveVar(false)
      instance.autorun(() => {
        const maxCost = instance.maxTxCost.get()
        if (/* max cost exceeds limit */) {
          instance.showAlert.set(true)
        } else {
          instance.showAlert.set(false)
        } 
      })
    })
    

    编辑:有关反应性的其他信息

    反应性是 Meteor 客户端生态系统的主要概念。它基于Tracker 包,该包链接到任何Template 实例。反应式数据存储指南进一步解释了这个概念:https://guide.meteor.com/data-loading.html#stores

    【讨论】:

    • 谢谢,工作得很好。你能解释一下自动运行的作用/它是如何工作的吗?是否高效、良好的做法等。
    • 在答案末尾添加了一些有用的链接。
    • 我最终没有使用 autorun 也没有使用 showAlert 辅助函数。似乎现在正在工作。有什么理由保留它?
    • 保留它,如果它需要根据数据变化自动做出反应。
    猜你喜欢
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 2014-11-20
    • 2014-05-12
    • 2015-04-18
    • 2016-03-07
    • 2016-05-26
    • 2022-11-15
    相关资源
    最近更新 更多