Вопрос

How can I set additional data in an action function in a Meteor Application that uses IronRouter ? See comments in emailWelcome and emailContract functions below...

Code:

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');
  }
});
Это было полезно?

Решение

You can get access to the data with this.getData() in your action functions:

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');
}
  • be careful not to call this.data(), as that will regenerate the data instead of getting you a reference to the already generated data object.
  • also be careful not to call this.setData(newData) within an action as that will invalidate the old data object, initiating a reactivity reload, and lead to an infinite loop!
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top