7
Tweets = new Meteor.Collection('tweets');
if (Meteor.isClient) {
Meteor.subscribe('tweets');
Template.Panel.helpers({
items: function() {
var days_tweets = Tweets.find();
console.log(days_tweets.count());
return days_tweets;
});
}
if (Meteor.isServer) {
Meteor.publish('tweets', function() {
return Tweets.find({}, {limit: 1000});
});
<body>
<h1>This is a list of tweets</h1>
{{> Panel}}
</body>
<template name="Panel">
<h2>A list of tweets sorted by size</h2>
{{#each items}}
<p>item</p>
{{/each}}
</template>
而且在頁面加載時的控制檯輸出:
Tweet count: 0
Tweet count: 129
Tweet count: 272
Tweet count: 366
Tweet count: 457
Tweet count: 547
Tweet count: 672
Tweet count: 814
Tweet count: 941
Tweet count: 1000
所以輔助功能觸發頁面上10倍負載(次數變化)。任何人都可以解釋這裏發生了什麼?我找不到任何對此的引用,接受在模板上從多個{{}}調用助手的情況。還有什麼方法來阻止它?最終,我需要在呈現之前一次處理推文。
好,謝謝。我假設了這樣的事情。有沒有辦法阻止它這樣做? (假設服務器上的集合沒有改變,這裏就是這種情況)。在固定服務器和客戶端集合之間進行同步異步運行似乎很奇怪。 – kendlete
@kendlete更新了我的答案,舉例說明了如何處理流星訂閱系統的行爲 –
Meteor.subscribe()在文檔中也有一個onready回調函數,可以用來延遲插入更多的代碼,直到訂閱爲止準備。這與.ready()不同,如果訂閱已準備就會返回布爾值。可能比Marco的方法更麻煩一點。 – Paul