尝试使用subscriptions
模式代替。
this.route('gamePage', {
path: '/game/:slug/',
subscriptions: function() {
return Meteor.subscribe('singlePlayer', this.params.slug);
},
onBeforeAction: function() {
var singlePlayer = this.data();
if (singlePlayer) {
if (singlePlayer.upgrade) {
this.subscribe('upgrades', this.params.slug);
}
}
this.next();
},
data: function() {
return Games.findOne({slug: this.params.slug});
},
waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]}
});
然而,重要的是你还包括loading
插件采取loadingTemplate
的优势。
Router.configure({
loadingTemplate: 'loading' // general purpose loading template
});
// built in plugin.. surprisingly not clearly specified in current docs, but you can dive in the code for plugins.
// https://github.com/EventedMind/iron-router/blob/devel/lib/plugins.js
Router.onBeforeAction('loading', { only: ['gamePage'] }); // only show loading for pages with subscriptions
Router.map(function() {
this.route('gamePage',{
//... your other options here ..
loadingTemplate: 'gamePageLoading', // game Page dedicated loading markup.
});
});
另外还有this.ready()
模式,如果你想留在你onBeforeAction
实现。
this.route('gamePage', {
path: '/game/:slug/',
onBeforeAction: [function() {
this.subscribe('singlePlayer', this.params.slug);
if(this.ready()) {
var singlePlayer = this.data();
if (singlePlayer) {
if (singlePlayer.upgrade) {
this.subscribe('upgrades', this.params.slug);
}
}
this.next();
} else {
this.render('loading');
}
}],
data: function() {
return Games.findOne({slug: this.params.slug});
},
waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]}
});
来源:https://github.com/EventedMind/iron-router/blob/devel/Guide.md#subscriptions
我想,因为.wait
图案被看作是不必要的链接这种变化是必要的,很容易出现(编码)错误。此外,当重写onBeforeAction
时,明确处理.next()
现在确保此挂钩的正确时间(并且可能大部分,如果不是全部其他挂钩)。
什么版本的铁路由器?另外:为什么在'onBeforeAction'回调周围使用数组(方括号)括号?我以前没有看过这种语法。不过,我只使用了铁路路由器0.9.4。 – 2014-11-10 01:13:19