Search code examples
meteoriron-router

SubsManager with Iron-Router new API


any idea how I could connect SubsManager with IR new API? I use syntax like:

Router.route('/', function () {
    this.subscribe('some_subscription').wait();

  if (this.ready()) {
     //something
  } else {
    //something
  }
},{
    name:'1234'
});

Tried subs.subscribe('some_subscription').wait(); but it threw error Template never rendered, did you forget to call 'this.next()?(of course I initialized object subs)


Solution

  • From the subscription manager docs https://github.com/meteorhacks/subs-manager/#limitations

     At the moment, the following functionality doesn't work (patches welcome):
    .........................
    
    ...............
    chained .wait() call in Iron Router (issue - you can use waitOn instead)
    

    so subs.subscribe('some_subscription').wait(); this will throw error

    From Iron-router docs https://github.com/EventedMind/iron-router#migrating-from-094

    onRun and onBeforeAction hooks now require you to call this.next(), and no longer take a pause() argument. So the default behaviour is reversed. For example, if you had:

    Router.onBeforeAction(function(pause) {
      if (! Meteor.userId()) {
        this.render('login');
        pause();
      }
    });
    

    You'll need to update it to

    Router.onBeforeAction(function() {
      if (! Meteor.userId()) {
        this.render('login');
      } else {
        this.next();
      }
    });
    

    Make sure that this.next() is called on onRun and onBeforeAction blocks

    and also organize your router code like in lib/routes.js file

    //configuration

     Router.configure({
            layoutTemplate: 'layout',
            loadingTemplate: 'loading',
            notFoundTemplate: 'error'
        });
    

    //keep all your routers under a common function

    Router.map(function() {
         this.route("index",{
               path:"/",
               onBeforeAction:function(){ //do something },
               data:return something,
               onAfterAction
        });
    });
    

    Finally my answer to the question any idea how I could connect SubsManager with IR new API?

    Router.map(function() {
             this.route("index",{
                   path:"/",
                   waitOn: function() {
                       return subs.subscribe('some_subscription');
                   },
                   data:function(){
                        if (this.ready()) {
                             //do something like this.render("loading")
                         } else {
                              //do something this.render("mytemplate")
                         }
                   },
                   onAfterAction
            });
        });