【问题标题】:What is Template.myTemplate.helpers(helpers) for and why should I use it?Template.myTemplate.helpers(helpers) 的用途是什么,为什么要使用它?
【发布时间】:2014-06-29 16:31:05
【问题描述】:

在 Meteor 中,如果下面是我的 HTML,

   <template name="myTemplate">
The value is {{value}}
</template>

我可以通过两种方式定义值

Template.myTemplate.helpers
    value: ->
        'insideHelper1'

Template.myTemplate.value =
    'outsideHelper1'

第一种方法是使用此处记录的帮助程序 (http://docs.meteor.com/#template_helpers),而第二种方法是使用此处定义的实时模板示例 (http://docs.meteor.com/#livehtmltemplates)

只是想知道,这两者有什么区别,我什么时候应该使用一个而不是另一个?

【问题讨论】:

  • 没有区别。我总是使用第一种方式,因为帮助程序的结构将与事件回调相同,并且易于与创建、销毁和呈现的回调分开。这让我有更多的可读性。您的 outsideHelper 只是 insideHelper 的简写形式。
  • 同样的问题:stackoverflow.com/questions/17099929/…我也一直使用第一种方式,因为如果你以后更改模板名称,你只需要在一个地方为所有助手更改名称。

标签: meteor handlebars.js


【解决方案1】:

它们完全一样。

如果你使用.helpers,它实际上只是将它添加到Template.。

这取决于您的编码风格。你可能更喜欢使用.helpers,因为如果你在同一个模板上有很多助手,它会使代码更简洁。

从技术上讲,它们不同的一个小方法是 Template.helpers 在 Meteor 应用程序启动时将帮助程序添加到您的 Meteor 应用程序中,而使用 Template.helpername 之前会添加它。所以如果你需要覆盖任何助手,你可以使用.helpers 方法。最后运行的将具有活动助手。

【讨论】:

    猜你喜欢
    • 2010-12-04
    • 2010-09-27
    • 2011-12-17
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    相关资源
    最近更新 更多