0

如何在使用IronRouter的Meteor 应用程序的动作函数中设置附加数据?请参阅下面的 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');
  }
});
4

1 回答 1

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)在操作中调用,因为这会使旧数据对象无效,启动反应性重新加载,并导致无限循环!
于 2013-11-01T18:01:15.077 回答