tyhenry
tyhenry

Reputation: 184

Meteor: Iron router not showing loading template when using Meteor._sleepForMs() in publication

I'm fairly new to Meteor, and having an issue where iron router is not showing my loading template during the "waitOn" function.

I'm using Meteor._sleepForMs(2000) to simulate network lag in the publication for the page. The delay is evident, but I just get a blank template until the page loads. I was under the impression that the loading template would display until 'waitOn' is finished (but maybe this is incorrect?).

My loading template (which displays fine when I call the template {{> loading}} directly on a page):

<template name="loading">
   {{> spinner}}
</template>

My router code:

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


Router.route('/editor/:_id',{
//route template
  name: 'editorProfile',
  //subscription to userProfile publication of editor
  waitOn: function(){
    Meteor.subscribe('userProfile',this.params._id);
  },
  //data context is the user
  data: function(){
    return Meteor.users.findOne(this.params._id);
  }
});

and my 'userProfile' publication:

Meteor.publish('userProfile', function(_id){

  Meteor._sleepForMs(2000);//simulate network lag

  //find the user by id
  var user=Meteor.users.findOne(_id);

  //if not found, mark subscription ready and quit
  if (!user){
    this.ready();
    return;
  }
  //if user is the currently logged in user
  if (this.userId==user._id){
    //return the full user document
    return Meteor.users.find(this.userId);
  }
  //if viewing another user
  else {
    //only return the user's profile
    return Meteor.users.find({'_id': user._id}, 
        {fields: {
            profile: 1
        }
    });
  }
});

Upvotes: 1

Views: 651

Answers (1)

Lucas Blancas
Lucas Blancas

Reputation: 561

Make sure your return the subscribe in your call to waitOn

waitOn: function(){
    return Meteor.subscribe('userProfile',this.params._id);
}

Upvotes: 2

Related Questions