如何在Meteor上使用IronRouter在操作中设置数据

How to set data in an action using IronRouter on Meteor?

本文关键字:操作 IronRouter 置数据 Meteor      更新时间:2023-09-26

如何在使用 IronRouter 的 Meteor 应用程序中的动作函数中设置其他数据?请参阅下面的电子邮件欢迎和电子邮件合同功能中的评论...

法典:

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');
  }
});
您可以使用操作

函数中的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),因为这会使旧数据对象无效,启动反应性重新加载,并导致无限循环!