【发布时间】:2015-03-19 13:09:00
【问题描述】:
我正在检索基于 Session 变量的 Collection 文档,然后将其作为名为 store 的变量通过 iron:router 数据上下文传递。问题是它有时会返回undefined,就好像它没有及时准备好让助手执行一样。如何确保在帮助程序/模板运行之前始终定义变量?
这是我的路线,您可以看到数据上下文包括根据存储在Session 变量中的_id 从集合中检索文档:
Router.route('/sales/pos', {
name: 'sales.pos',
template: 'pos',
layoutTemplate:'defaultLayout',
loadingTemplate: 'loading',
waitOn: function() {
return [
Meteor.subscribe('products'),
Meteor.subscribe('stores'),
Meteor.subscribe('locations'),
Meteor.subscribe('inventory')
];
},
data: function() {
data = {
currentTemplate: 'pos',
store: Stores.findOne(Session.get('current-store-id'))
}
return data;
}
});
这里是依赖于 store 变量的助手被传递给模板:
Template.posProducts.helpers({
'products': function() {
var self = this;
var storeLocationId = self.data.store.locationId;
... removed for brevity ...
return products;
}
});
【问题讨论】:
标签: meteor iron-router