【发布时间】:2014-12-12 10:40:31
【问题描述】:
从 Meteor 0.9.4 开始,定义 Template.MyTemplate.MyHelperFunction() 不再有效。
我们弃用了 Template.someTemplate.myHelper = ... 语法,转而使用 Template.someTemplate.helpers(...)。使用旧语法仍然有效,但它会在控制台上显示弃用警告。
这对我来说似乎很好,因为它(至少)可以避免一些错误输入和重复的文本。然而,我很快发现我构建 Meteor 应用程序的方式依赖于这个新版本已弃用的功能。在我的应用程序中,我一直在使用旧语法定义助手/函数,然后从其他助手调用这些方法。我发现它帮助我保持代码干净和一致。
例如,我可能有这样的控件:
//Common Method
Template.myTemplate.doCommonThing = function()
{
/* Commonly used method is defined here */
}
//Other Methods
Template.myTemplate.otherThing1 = function()
{
/* Do proprietary thing here */
Template.myTemplate.doCommonThing();
}
Template.myTemplate.otherThing2 = function()
{
/* Do proprietary thing here */
Template.myTemplate.doCommonThing();
}
但这似乎不适用于 Meteor 建议的新方法(这让我一直认为我错了)。我的问题是,在模板的帮助程序之间共享通用的模板特定逻辑的首选方式是什么?
【问题讨论】:
-
我意识到这仍然是一个有效的问题,因为测试台最好能够调用模板助手。否则你最终会不必要地重写你的应用程序......
-
您可能会发现这个答案很有用:stackoverflow.com/questions/27755891/…
标签: javascript templates meteor