如何模拟慢速Meteor出版物?

Ada*_*sen 11 javascript meteor

我正在尝试模拟出版物做一堆工作并花费很长时间来返回光标.

我的发布方法有一个强制睡眠(使用未来),但应用程序始终只显示

载入中...

这是出版物:

Meteor.publish('people', function() {
  Future = Npm.require('fibers/future');
  var future = new Future();

  //simulate long pause
  setTimeout(function() {
    // UPDATE: coding error here. This line needs to be
    //   future.return(People.find());
    // See the accepted answer for an alternative, too:
    //   Meteor._sleepForMs(2000);
    return People.find();
  }, 2000);

  //wait for future.return
  return future.wait();
});
Run Code Online (Sandbox Code Playgroud)

和路由器:

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

Router.map(function() {
  return this.route('home', {
    path: '/',
    waitOn: function() {
      return [Meteor.subscribe('people')];
    },
    data: function() {
      return {
        'people': People.find()
      };
    }
  });
});

Router.onBeforeAction('loading');
Run Code Online (Sandbox Code Playgroud)

完整源代码:https://gitlab.com/meonkeys/meteor-simulate-slow-publication

Dav*_*don 29

最简单的方法是使用未记录的Meteor._sleepForMs函数,如下所示:

Meteor.publish('people', function() {
  Meteor._sleepForMs(2000);
  return People.find();
});
Run Code Online (Sandbox Code Playgroud)

  • 提示:不要试图在客户端查看"Meteor._sleepForMs",它只是一个服务器方法. (6认同)