2013-11-01 25 views
0

如何在使用IronRouterMeteor Application中的動作函數中設置附加數據?參見下面emailWelcome和emailContract功能評論...如何在流星中使用IronRouter設置數據?

代碼:

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

回答

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!