Meteor 中模板之间的共享功能 [英] Sharing functions between templates in Meteor

查看:21
本文介绍了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屋!

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