文章http://tomkelsey.co.uk/reining-in-the-reactivity-with-meteor/討論了這個確切的問題,筆者「使用的流星相結合的方法和一般的發佈/訂閱的方式。一個簡單的例子載列如下。
在主頁的onCreated方法我要求項_ids的排序後的數組從服務器,並將它們保存到一個會話變量稱爲itemIds:
Meteor.call('getItemIds', function(err, res) {
Session.set('itemIds', res);
});
在服務器上,該方法被定義爲:
Meteor.methods({
getItemIds: function() {
return Items.find({}, {fields: {_id: 1}, sort:
{rank:1}}).fetch();
}
});
然後我看的任何改變itemIds會話變量並(重新)因此訂閱:
var self = this;
self.autorun(function() {
self.subscribe('itemsById', Session.get('itemIds'));
});
在服務器上,公佈如下:在
Meteor.publish('itemsById', function(itemIds) {
return Items.find({}, {_id: {$in: itemIds}});
});
我的模板迭代itemIds數組而不是Items集合遊標。這意味着項目的順序保持不變。
對於itemIds中的每個_id,我可以做一個Items.find(_id) - 數據將位於我們的itemsById訂閱的客戶端Items集合中。
瞧我們,直到刷新頁面,將保留其順序反應的項目列表。」
聽起來像它可以適用於你:)
聽起來類似於我最近閱讀的文章:http://tomkelsey.co.uk/reining-in-the-reactivity-with-meteor/也許會幫助:) –
第二次閱讀您的問題後,您的前提是錯誤的。您首先要問的是如何「按需」(從客戶端)執行,然後以「每5秒」爲例,由服務器控制。請把你的問題縮小到一個**精確的**問題。 –