2014-11-08 28 views
10

我試圖模擬一個出版物做一堆工作,並花了很長時間來返回一個遊標。如何模擬緩慢的流星發佈?

我的發佈方法有強制睡眠(使用未來),但應用程序始終只顯示

載入中...

這裏的刊物:

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

回答

27

做到這一點,最簡單的方法是使用無證Meteor._sleepForMs功能,像這樣:

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

方便的,謝謝!我更新了我的問題,以顯示我編寫的錯誤。 – 2014-11-08 16:26:34

+6

提示:不要試圖在客戶端查看「Meteor._sleepForMs」,這是一種僅服務器方法。 – 2014-11-08 16:28:51

+0

問題:這對我有用,謝謝。但只有一次。我猜在第一次加載之後,發佈緩存在客戶端上。有沒有辦法在每次刷新時模擬一個較慢的連接? (或者至少再次測試它?) – Arrowcatch 2016-02-24 16:57:03