使用条件 && 订阅多个集合
Subscribe several collections with conditional &&
为什么以下代码在 Iron router 中有效,当我同时订阅多个集合时,我 returning Collection1&&Collection2 etc
[查看代码]。
我阅读了这个问题 Multiple subscriptions in iron router,他建议通过 array
添加多个订阅者,我正在做不同的事情,因为它正在工作,我发现在尝试和错误之后正在工作。但老实说,我不知道为什么要工作。有人可以解释为什么条件适用于 return 所有集合吗?
Meteor.startup(function(){
Router.route('/',
{
name : "dashboard",
waitOn : function(){
if(!Meteor.loggingIn() && !Meteor.user()) {
this.redirect("login");
}
return Meteor.subscribe("collection_1") &&
Meteor.subscribe("collection_2") &&
Meteor.subscribe("collection_3") &&
Meteor.subscribe("collection_4");
},
onBeforeAction : function(){
if(!Meteor.loggingIn() && !Meteor.user()) {
this.redirect("login");
}
this.next();
},
action : function(){
this.render();
}
});
});
使用 && 返回多个订阅应该有效,因为 Meteor.subscribe('subscription')
将始终 return 具有订阅 ID 和 onReady
/ onStop
回调的对象,即使您订阅到不存在的东西。但如果订阅不存在,则不会调用 onReady
回调。
因此,return 语句会将每个对象评估为 "true" 并遍历所有语句以启动所有订阅。
为什么以下代码在 Iron router 中有效,当我同时订阅多个集合时,我 returning Collection1&&Collection2 etc
[查看代码]。
我阅读了这个问题 Multiple subscriptions in iron router,他建议通过 array
添加多个订阅者,我正在做不同的事情,因为它正在工作,我发现在尝试和错误之后正在工作。但老实说,我不知道为什么要工作。有人可以解释为什么条件适用于 return 所有集合吗?
Meteor.startup(function(){
Router.route('/',
{
name : "dashboard",
waitOn : function(){
if(!Meteor.loggingIn() && !Meteor.user()) {
this.redirect("login");
}
return Meteor.subscribe("collection_1") &&
Meteor.subscribe("collection_2") &&
Meteor.subscribe("collection_3") &&
Meteor.subscribe("collection_4");
},
onBeforeAction : function(){
if(!Meteor.loggingIn() && !Meteor.user()) {
this.redirect("login");
}
this.next();
},
action : function(){
this.render();
}
});
});
使用 && 返回多个订阅应该有效,因为 Meteor.subscribe('subscription')
将始终 return 具有订阅 ID 和 onReady
/ onStop
回调的对象,即使您订阅到不存在的东西。但如果订阅不存在,则不会调用 onReady
回调。
因此,return 语句会将每个对象评估为 "true" 并遍历所有语句以启动所有订阅。