我正在尝试模拟出版物做大量工作并花费很长时间来返回光标。
我的发布方法有强制睡眠(使用未来),但应用程序始终只显示
这是出版物:
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
答案 0 :(得分:28)
最简单的方法是使用未记录的Meteor._sleepForMs
函数,如下所示:
Meteor.publish('people', function() {
Meteor._sleepForMs(2000);
return People.find();
});