简体   繁体   English

使用条件&&订阅多个收藏集

[英]Subscribe several collections with conditional &&

Why the follow code works in Iron router, when I'm subscribing several collections at the same time, Im returning Collection1&&Collection2 etc [view code]. 为什么以下代码在Iron路由器中起作用,为什么当我同时订阅多个集合时,我返回了Collection1&&Collection2 etc [查看代码]。

I read this question Multiple subscriptions in iron router where he is suggesting to add several subscriber via an array , I'm doing different just cause is working and I found was working after try and error. 我读到这个问题,他建议在铁路由器中通过一个array添加多个订户,我在做不同的正当原因,而在尝试错误后我发现它正在工作。 But to be honest I don't know why is working. 但是说实话,我不知道为什么会起作用。 Can someone explain why conditions works to return all the collections?. 有人可以解释为什么条件可以返回所有馆藏吗?

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();
            }
        });
});

Returning multiple subscriptions with && should work because Meteor.subscribe('subscription') will always return an object with a subscription ID, and onReady / onStop callbacks, even if you subscribe to something that does not exists. 使用&&返回多个订阅应该可以工作,因为Meteor.subscribe('subscription')将始终返回具有订阅ID和onReady / onStop回调的对象,即使您订阅的对象不存在。 But if the subscription does not exists the onReady callback won't be called. 但是,如果订阅不存在,则不会调用onReady回调。

So, the return statement will evaluate each objects as "true" and go through all the statements to launch all the subscriptions. 因此,return语句会将每个对象评估为“ true”,并遍历所有语句以启动所有订阅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM