在Meteor中的模板之间共享功能 [英] Sharing functions between templates in Meteor
问题描述
如果我有两个要检查同一功能返回值的车把模板怎么办?有正确的方法吗?显然我可以这样做:
What if I have two handlebar templates that want to check the return value of the same function? Is there a right way to do this? Obviously I could do this:
var say_foo = function() {
alert('foo');
};
Template.foo.say_foo = say_foo;
Template.bar.say_foo = say_foo;
但是有什么方法可以直接通过Template对象执行此操作吗?由于这是JavaScript,因此我可以将函数分配给任何对象的任何属性,但是我尝试这样做:
But is there some way to do this directly through the Template object? Since this is JavaScript I can assign a function to any property of any object, but I tried doing:
Template.say_foo = function() { alert('foo'); };
当然模板没有找到此功能.
and of course the templates don't find this function.
只是对最佳实践等感到好奇.谢谢!
Just curious about best practices and the like. Thanks!
推荐答案
根据Meteor文档,正确的方法是使用UI名称空间,该名称空间将绑定到在下面使用的任何模板引擎,而不是使用Handlebars ,或直接使用空格键.这是文档中的链接. http://docs.meteor.com/#ui_registerhelper
因此,要从client.js中使用的任何模板访问通用功能:
So for generic function to be accessed from any template use in client.js:
UI.registerHelper('stub', function() {
// code
});
显然,文档已改回使用:
Template.registerHelper('stub', function() {
// code
});
然后在html文件中使用{{stub}}
访问变量.
Then in the the html file use {{stub}}
to access the variable.
这篇关于在Meteor中的模板之间共享功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!