Search code examples
meteoriron-routersubscriptions

The best pattern to organize my subscriptions in Meteor without the risk to find the data not ready


I read some of the posts here about how to organize subscriptions in Meteorjs, but I still I don't understand what is the best pattern to avoid finding out some data I subscribed are not ready to be used in a template. I use Iron Router and now I have all my subscriptions organized using a waitOn option in Router.configure. I find out that this way doesn't help sometimes. If I have multiple subscriptions like this:

Router.configure({
    layoutTemplate: 'layout',
    loadingTemplate: 'loading',
    waitOn: function () {
        return [
            Meteor.subscribe('channels'),
            Meteor.subscribe('invitations'),
            Meteor.subscribe('messages')
        ];
    } 
});

I understood that order matters. If I change the order of my subscriptions in the array, the program respond differently. What I want to get is that ALL my subscriptions get completely loaded before navigating the app. Someone in a previous post talked about putting them in a separate file to solve this problem. But how? Where do I need to put that file? I would appreciate some examples here for my case.


Solution

  • With the release of Meteor 1.0.4 the templates instances now have a subscribe method that works exactly like the Meteor.subscribe, check this release notes to more information about

    So you can use that subscriptions inside a onCreated like the follow example.

    Template.notifications.onCreated(function () {
      this.subscribe("channels");
      this.subscribe("invitations");
      this.subscribe("messages");
    });
    

    Check Meteor Docs about the subscriptionsReady()