路由器数据中未定义的辅助变量
Helper variable undefined from router data
我正在检索基于会话变量的集合文档,然后将其作为名为 store
的变量通过 iron:router 数据上下文传递。问题是它有时 returns undefined
,就好像它没有及时准备好让助手执行一样。如何确保在 helper/template 运行之前始终定义变量?
这是我的路线,您可以看到数据上下文包括根据存储在 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 中的常见问题。当您等待订阅准备就绪时,这并不意味着您的查找功能有时间 return 做某事。您可以通过一些防御性编码来解决它:
Template.posProducts.helpers({
'products': function() {
var self = this;
var storeLocationId = self.data.store && self.data.store.locationId;
if (storeLocationId) {
... removed for brevity ...
return products;
}
}
});
我正在检索基于会话变量的集合文档,然后将其作为名为 store
的变量通过 iron:router 数据上下文传递。问题是它有时 returns undefined
,就好像它没有及时准备好让助手执行一样。如何确保在 helper/template 运行之前始终定义变量?
这是我的路线,您可以看到数据上下文包括根据存储在 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 中的常见问题。当您等待订阅准备就绪时,这并不意味着您的查找功能有时间 return 做某事。您可以通过一些防御性编码来解决它:
Template.posProducts.helpers({
'products': function() {
var self = this;
var storeLocationId = self.data.store && self.data.store.locationId;
if (storeLocationId) {
... removed for brevity ...
return products;
}
}
});