两种在Meteor中定义助手的方法

And*_*Mao 2 handlebars.js meteor meteor-helper

编辑:这在Meteor 1.0中已不再适用.第一种语法已被弃用,仅支持第二种语法.

似乎有两种方法来定义Meteor中显然称为帮助者的东西:

Template.foo.helper1 = function() { ... }
Run Code Online (Sandbox Code Playgroud)

另一种方式:

Template.foo.helpers({
  helper2: function() { ... }
});
Run Code Online (Sandbox Code Playgroud)

两者之间是否存在语义或用法差异?我能看到的唯一限制是第一次使用不能使用保留关键字.我想知道这种区别是出于历史原因还是其他原因而产生的.

小智 9

根据http://docs.meteor.com/#template_helpers,除语法外,它们是等效的,Template.myTemplate.foo语法不适用于保留的模板名称.

使用传递给字典的好处Template.myTemplate.helpers是你可以在多个模板中重用它.

    var reusableHelpers = { stuff: function() { return "stuff"; } };
    Template.foo.helpers( reusableHelpers );
    Template.bar.helpers( reusableHelpers );
Run Code Online (Sandbox Code Playgroud)