waitOn Iron Router without subscribe


#1

Hi!

Here’s my situation:

In my client.js i have a subscription to myCollection that triggers when there is a new Meteor.userId()

subscribe(‘myCollection’);

In my router.js i have a route like the following where i want to wait until some data are present in myCollection but i don’t want to subscribe again my collection in the waitOn because this causes to trigger another server request for publishing.

Is there a way to do that?

Router.route('/groups', {
     
   waitOn: function(){
       /*WHAT TO DO HERE? I DO NOT WANT TO SUBSCRIBE AGAIN*/
   },
    data: function(){    
      return {
      /*data for templating*/
    },

    action: function(){
      this.render();
    }

});

Thanks!


#2

You can use https://github.com/meteorhacks/subs-manager.


#3
handle = Meteor.subscribe('myCollection');
Router.route('/groups', {
   waitOn: function(){
       return handle
   }
});

#4

This is a simple and effective solution, thanks!
I still have something to ask, this code does not seem to trigger the ‘loading’ template so i tried to add in the action function this code :

handle = Meteor.subscribe('myCollection');

Router.route('/groups', {
   waitOn: function(){
       return handle
   },

   action: function(){

    if ( handle.ready() ){ /*i also tried this.ready()*/
     this.render();
   }
   else {
     this.render('loading');
   }
 }
});

but it still does not work, what am i missing?


#5

this.ready() should work. I’ll try to play around


#6

i’m discovering that this works only the first time but when the publish triggers again (because a new user log in, for example) is like the server does not send anymore the ready message (and i’m sure that the server calls the publish function beacuse i put a console.log there).

I’m trying to investigate by myself also, thank you very much for your help.


#7

I think finally i have the right solution here :

The server correctly sends the ready message every publish but the handler var does not update…so the route does not benefit of the ready call… we have to put the subscription inside an autorun and everything works.

var handle;
Tracker.autorun(function () {
    handle = Meteor.subscribe('myCollection', Meteor.userId());
});

Router.route('/groups', {
   waitOn: function(){
       return handle
   },

   action: function(){

    if ( this.ready() ){
     this.render();
   }
   else {
     this.render('loading');
   }
 }
});

this can seem a straightforward implementation here, i know, but i started with the assumption that i did not want to put the subscription in autorun that’s because i see the server publishing stuff even if my subscription is not in the autorun and i still do not fully understand this…

If you have further comments i will really appreciate them but don’t get mad with that since i have a working solution.

Thanks again!


#8

If you want to improve your knowledge of the problem i found a very good article about this here:

in particulare the final paragraph ‘A Note on Subscriptions’.

Enjoy!


#9

Have you tried this? Since Meteor.userId() is reactive this will rerun. In your template used for this route you can check if data is present

Router.route('/groups', {
   waitOn: function(){
       return Meteor.subscribe('myCollection', Meteor.userId());
   },

   data: function(){
    if ( this.ready() ){
     return MyCollection.find();
   }
 }
});