在流星中定义助手的两种方法 [英] Two ways to define helpers in Meteor

查看:122
本文介绍了在流星中定义助手的两种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

似乎有两种方法可以定义Meteor中明显称为助手的东西:

It seems that there are two ways to define what are apparently called helpers in Meteor:

Template.foo.helper1 = function() { ... }

其他方法:

Other way:

Template.foo.helpers({
  helper2: function() { ... }
});

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

Is there any semantic or usage difference between the two? The only restriction I can see is that the first usage can't use reserved keywords. I'm wondering if the distinction arose only for historical reasons or if there is something else.

推荐答案

根据http://docs.meteor.com/#template_helpers 它们与语法无关,并且 Template.myTemplate .foo 语法不适用于保留的模板名称。

According to http://docs.meteor.com/#template_helpers they are equivalent aside from the syntax, and the Template.myTemplate.foo syntax will not work for reserved template names.

使用传递给模板的字典的好处。

The nice thing about using a dictionary passed to Template.myTemplate.helpers is that you can reuse it across multiple templates.

    var reusableHelpers = { stuff: function() { return "stuff"; } };
    Template.foo.helpers( reusableHelpers );
    Template.bar.helpers( reusableHelpers );

这篇关于在流星中定义助手的两种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆