如何在Meteor上使用IronRouter在操作中设置数据
How to set data in an action using IronRouter on Meteor?
如何在使用 IronRouter 的 Meteor 应用程序中的动作函数中设置其他数据?请参阅下面的电子邮件欢迎和电子邮件合同功能中的评论...
法典:
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');
}
});
您可以使用操作
函数中的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)
,因为这会使旧数据对象无效,启动反应性重新加载,并导致无限循环!
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- 如何确定javascript已经完成了某些操作.ios上的
- 防止Alt+Shift默认操作或检测多种操作系统语言的Javascript
- 从JavaScript访问struts操作中的属性
- fluxxor向一个flux实例添加一组以上的操作
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- 操作放置在画布上的元素之间的连接
- 使用“+="操作人员
- Rails操作只调用一次,但我在ajax中每秒钟都调用一次
- Jquery未定义函数正在停止其他操作
- 如何操作iframe之外的元素
- javascript对象操作:根据指定条件选择属性
- jQuery:暂停按钮可以暂停所有其他操作
- 用于选择/文本框操作的JavaScript
- Google 脚本:用于创建日历活动的脚本运行时不会出错,但不会执行任何操作
- 通过javascript操作图像,非常简单
- 将文本框中的值用于按钮窗体操作上的变量
- 当通过AJAX调用时,控制器操作不会执行
- 如何在Meteor上使用IronRouter在操作中设置数据