【发布时间】:2015-03-13 19:02:01
【问题描述】:
在 Meteor 中,我将两个对象从我的数据库发送到一个模板:
Template.myTemplate.helpers({
helper1: function() {
var object1 = this; // data context set in iron:router...path is context dependent
// modify some values in object1
return this;
},
helper2: function() {
return Collection2.find({_id: this.object2_id});
}
});
这个模板也有一个事件处理程序来修改上面的两个对象。我试图从上面访问 helper1 和 helper2,但是如果我调用模板的数据上下文,我只能访问 object1 的未修改版本。如何访问上面定义的帮助程序?
Template.myTemplate.events({
'submit form': function(event) {
event.preventDefault();
// Access helper2 object and attributes here instead of calling Collection2.find() again
}
});
【问题讨论】:
-
无法使用当前的公共 API 调用助手。
-
佩佩,你可能知道这一点,但我刚刚发现有一个用于访问助手的内部 api - Template.myTemplate.__helpers.get('helper');
-
鉴于这是内部的,我假设 api 将来可能会改变,因此在您的代码中经常使用它可能不是最好的主意。
-
请记住,您不能依赖在您以这种方式调用的助手中使用
this,我猜Template.instance()也不会起作用,除非您执行更多公共API 违规行为。 -
@bgmaster,
Template.myTemplate.__helpers.get('helper');太老套了,将来随时可能过时。 Blaze 不是为这种方式设计的...在下面查看我的答案
标签: javascript meteor meteor-blaze