我想根据文档中的 bool 属性(mode
)来决定用户获得哪些订阅,但我的设计方法存在问题。如果我在 waitOn
函数中使用 this.data()
和 if 子句,我将获得呈现的 404 页面,因为我首先需要订阅。
这是我的代码:
this.route('gamePage', {
path: '/game/:slug/viewGame/:produceDate?/:releaseDate?',
onBeforeAction: [memberFilter],
waitOn: function() {
var game = this.data();
if (game.mode) {
if (this.params.produceDate && this.params.releaseDate) {
return [Meteor.subscribe('singleGame', this.params.slug),
Meteor.subscribe('authorsMode', this.params.slug, this.params.produceDate, this.params.releaseDate)];
} else {
return [Meteor.subscribe('singleGame', this.params.slug), Meteor.subscribe('console', this.params.slug)];
}
} else {
return [Meteor.subscribe('singleGame', this.params.slug),
Meteor.subscribe('authors', this.params.slug)];
}
},
data: function() {
return Games.findOne({slug: this.params.slug});
}
任何帮助将不胜感激。
最佳答案
要直接实现这一目标,您需要 Iron Router 来让您拥有 2 个级别的订阅,第一个级别使数据可供下一个订阅级别使用。目前不存在此功能。我刚刚提交了 issue向 IR 存储库请求此功能。
解决此问题的方法是为每种情况使用不同的 URL,进行中间重定向:
this.route('gamePage', {
path: '/game/:slug/viewGame/:produceDate?/:releaseDate?',
onBeforeAction: function() {
var game = this.data();
var params = {
slug: this.params.slug,
produceDate: this.params.produceDate,
releaseDate: this.params.releaseDate
}
if (game.mode) {
if (this.params.produceDate && this.params.releaseDate) {
this.redirect('gamePageOption1', params);
} else {
this.redirect('gamePageOption2', params);
}
} else {
this.redirect('gamePageOption3', params);
}
},
waitOn: function() {
return [Meteor.subscribe('games')];
},
data: function() {
return Games.findOne({slug: this.params.slug});
}
}
如果您使用 IR v < 0.9.3,请注意有关中间重定向的问题:ref1 , ref2 。如果是这种情况,您必须在 Meteor.defer
内进行重定向:
Meteor.defer(function() {
this.redirect(...)
})
关于javascript - iron-router 在 waitOn 函数中选择不同的订阅 - Meteor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25949492/