Kani
Kani

Reputation: 840

How to handle subscription in Meteor / Iron Router

whats the bestway to handle subscription based data. For example, you have a game where you have to create the character first before you can do any other things. Currently I thougth I can try to handle it with a onBeforeAction filter. So I have a global controller for every route that needs a the character.

DefaultController = LayoutController.extend({
  onBeforeAction : function() {
    var currentCharacter = Character.getCurrent.call({});
    if(currentCharacter === undefined) {
        this.render('CharacterCreate');
    } else {
        this.next();
    }
 },
 waitOn() {
    this.subscribe('characters.owned');
 }
});

You have a route like this:

Router.route('/game', { controller: 'DefaultController' });

The problem is until the the collection is loaded the game template will shown. Is there a better approach like this? And another problem when a route needs a character it throws an exception until the subscription is loaded.

Upvotes: 0

Views: 43

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20226

Just use a loading hook while the subscriptions are being loaded.

loading(){
  this.render('myLoadingTemplate');
}

The loading hook is run automatically while waiting for subscriptions to be ready.

You might find my post on building a clean router.js file useful.

Upvotes: 1

Related Questions