如何模拟慢速流星发布?

亚当·蒙森

我正在尝试模拟发布,进行大量工作并花费很长时间返回游标。

我的发布方法强制睡眠(使用将来),但该应用程序始终仅显示

载入中...

这是出版物:

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

和路由器:

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

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

戴维·韦尔顿

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

Meteor.publish('people', function() {
  Meteor._sleepForMs(2000);
  return People.find();
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章