Search code examples
javascriptmeteoriron-router

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].

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

Solution

  • 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. But if the subscription does not exists the onReady callback won't be called.

    So, the return statement will evaluate each objects as "true" and go through all the statements to launch all the subscriptions.