4

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.

massimosgrelli
  • 121
  • 1
  • 7

1 Answers1

6

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

Ethaan
  • 11,291
  • 5
  • 35
  • 45
  • Thanks @Ethaan I checked Meteor 1.0.4 documentation and template level subscriptions should be perfect. In my understanding, every template level subscription expires once the template is destroyed. So I suppose that inner templates don't have to subscribe a collection again if the outer templates already did that for them. Is that correct? – massimosgrelli Mar 22 '15 at 21:10
  • Yes with the template subscriptions its enought – Ethaan Mar 22 '15 at 21:42
  • Yes, sorry. I forgot to get back here. I'm now using Template level subscriptions and it works perfectly. – massimosgrelli Apr 16 '15 at 12:21
  • @massimosgrelli if this answer help you could you please accept this one? – Ethaan Apr 16 '15 at 15:16
  • What do you mean by "accept"? I need to have my reputation at 15 – at least – to up vote this answer. – massimosgrelli Apr 17 '15 at 17:57
  • oh "check" it the green marker is just below the 2 arrows with the up vote reputation, – Ethaan Apr 17 '15 at 18:04
  • Still puzzling over how to use the subscriptionsReady() function in the context of having a template wait until subscriptions are ready for it. Used in iron-router? In the template renderer? Can you give a simplified example? – Wes Modes Apr 22 '15 at 05:26
  • Hi sorry for trouble again after the issue has been closed but after using this on JS file how to show data on HTML file ?? – monk May 17 '15 at 11:27