在 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
}
});
帮助程序只是函数,因此可以随意传递并分配给其他变量,因此您可以定义一个函数,然后将其分配给模板帮助程序的 helper2 键,并通过事件处理程序中的原始引用进行调用。
var helperFunction = function() {
return Collection2.find({_id: this.object2_id});
};
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: helperFunction
});
Template.myTemplate.events({
'submit form': function(event) {
event.preventDefault();
var cursor = helperFunction();
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)