如何在Meteor上使用IronRouter设置动作中的数据?

时间:2013-11-01 18:01:15

标签: javascript meteor iron-router

如何在使用Meteor ApplicationIronRouter的动作函数中设置其他数据?请参阅下面的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');
  }
});

1 个答案:

答案 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!