【问题标题】:How to set data in an action using IronRouter on Meteor?如何在 Meteor 上使用 IronRouter 在动作中设置数据?
【发布时间】:2013-11-12 23:55:10
【问题描述】:

如何在使用 IronRouterMeteor Application 中的操作函数中设置其他数据?请参阅下面的 emailWelcome 和 emailContract 函数中的 cmets...

代码:

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');
  }
});

【问题讨论】:

    标签: javascript meteor iron-router


    【解决方案1】:

    您可以在操作函数中使用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!

    【讨论】:

      猜你喜欢
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多