bgm*_*ter 5 javascript meteor meteor-blaze
在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});
}
});
Run Code Online (Sandbox Code Playgroud)
该模板还有一个事件处理程序来修改上面的两个对象.我试图从上面访问helper1和helper2,但如果我调用模板的数据上下文,我只能访问未修改版本的object1.如何访问上面定义的帮助程序?
Template.myTemplate.events({
'submit form': function(event) {
event.preventDefault();
// Access helper2 object and attributes here instead of calling Collection2.find() again
}
});
Run Code Online (Sandbox Code Playgroud)
帮助程序只是函数,因此可以随意传递并分配给其他变量,因此您可以定义一个函数,然后将其分配给模板帮助程序的 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();
}
});
Run Code Online (Sandbox Code Playgroud)