这实际上是两个问题:
-
是否可以有条件地订阅铁路由器的waitOn选项中的集合?
-
是否可以在 Router.go() 中将对象作为参数传入?
在我的应用程序中创建新帖子时,我正在尝试减少呈现视图的延迟。我尝试传入一个isNew属性作为Router.go()的参数,但没有运气:
// Router call after creating a new post
Router.go('postPage', {
_id: id,
isNew: true,
post: newPostObject
});
// router.js
Router.map(function() {
this.route('postsList', {
path: '/'
});
this.route('postPage', {
path: '/:_id',
waitOn: function() {
//This returns only _id for some reason.
console.log(this.params);
if (this.params.isNew != true) {
return [
Meteor.subscribe('singlePost', this.params._id),
Meteor.subscribe('images', this.params._id),
]
}
},
data: function() {
if (this.params.isNew == true) {
return this.params.post
else {
return Posts.findOne(this.params._id);
}
}
});
});
经过一番挖掘,看起来 Iron Router 支持选项 hash 作为 Router.go() 方法中的第三个参数:
Router.go( 'postPage', {_id: id}, {isNew: true} );
要在路由中访问它,您可以使用然后使用 this.options
.若要获取上述示例中 isNew
的值,请使用 this.options.isNew
。
您只能通过this.params
访问动态路径段。 所以要this.params.isNew
工作,你需要像这样。
this.route('postPage', {
path: '/:_id/:isNew',
waitOn: function() {}
...
});