流星:在发布中使用 Meteor._sleepForMs() 时,Iron router 不显示加载模板

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

我是 Meteor 的新手,我遇到了一个问题,即 Iron Router 在 "waitOn" 函数期间没有显示我的加载模板。

我正在使用 Meteor._sleepForMs(2000) 来模拟页面发布中的网络延迟。延迟很明显,但在页面加载之前我只是得到一个空白模板。我的印象是加载模板会一直显示到 'waitOn' 完成(但也许这是不正确的?)。

我的加载模板(当我直接在页面上调用模板 {{> loading}} 时显示正常):

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

我的路由器代码:

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);
  }
});

和我的 'userProfile' 出版物:

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
        }
    });
  }
});

确保您的 return 订阅了您对 waitOn

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