如何在使用Meteor Application的IronRouter的动作函数中设置其他数据?请参阅下面的emailWelcome和emailContract函数中的评论......
代码:
EmailController = RouteController.extend({
template: 'emailPage',
waitOn: function() {
return [
Meteor.subscribe('customers'),
];
},
data: function() {
var request = Requests.findOne(this.params._id);
if (!request)
return;
var customer = Customers.findOne({'_id': request.customerId});
if (!customer)
return;
return {
sender: Meteor.user(),
recipient: Customers.findOne({_id:Session.get('customerId')})
};
},
emailWelcome: function() {
// Set var in the context so that emailTemplate = 'welcomeEmail' here
this.render('emailPage');
},
emailContract: function() {
// Set var in the context so that emailTemplate = 'contractEmail' here
this.render('emailPage');
}
});
答案 0 :(得分:2)
您可以在动作函数中使用this.getData()
访问数据:
emailWelcome: function() {
var data = this.getData(); // get a reference to the data object
data.emailTemplate = 'welcomeEmail';
this.render('emailPage');
},
emailContract: function() {
var data = this.getData(); // get a reference to the data object
data.emailTemplate = 'contractEmail';
this.render('emailPage');
}
this.data()
,因为这会重新生成
数据而不是让您引用已生成的数据
对象。this.setData(newData)
,因为这会使旧数据对象失效,启动反应重新加载,lead to an infinite loop!