2016-04-15 128 views
0

訂閱所有數據可能需要大量的時間和壓力在服務器上,特別是如果你有成千上萬的數據;然而有時候我們無法避免它。MeteorJS發佈和訂閱

例如:

我得到了儀表盤在那裏我需要的所有可用查找用戶數據。

我不能在發佈上限制它,因爲我無法正確搜索用戶集合。

有沒有一種方法可以推薦(一個包或一個進程),它能夠以更快的方式訂閱大量數據,並且在服務器中壓力更小?謝謝

+0

你需要它被動嗎?如果答案是否定的,那麼你可以使用流星方法。我有類似的問題上次發佈成千上萬的記錄。從字面上看,頁面需要花費大量時間(> 30-60秒)來發布所有記錄。所以我使用了方法,而且它適用於我的用例。 – Kishor

+0

@Kishor - 感謝您的回覆,請問您是怎麼​​做的?謝謝。一個簡短的示例代碼將非常有幫助。 –

回答

1

這不是對原始問題的回答,但我添加了使用流星方法而不是出版物(無反應性)的流程。

對於下面的例子中,可以說有大量記錄的集合是「UserPosts」

//on server side 
Meteor.methods({ 
    getUserPosts: function (userId) { 
     return UserPosts.find({ userId: userId }); 
    } 
}); 

//on client side 
Template.yourTemplate.onCreated(function() { 
    Session.set("current-user-posts", []); 
    var template = this; 
    template.autorun(function() { 
     var userId = Meteor.userId(); //Instead of this, add your reactive data source. That is, this autorun will run whenever Meteor.userId() changes, so change it according to your needs. 
     Meteor.call("getUserPosts", function (err, result) { 
      if (err) console.log("There is an error while getting user posts.."); 
      result = err ? [] : result; 
      Session.set("current-user-posts", result); 
     }); 
    }); 
}); 

Template.yourTemplate.helpers({ 
    userPosts: function() { 
     return Session.get("current-user-posts"); 
    } 
}); 

Template.yourTemplate.onDestroyed(function() { 
    Session.set("current-user-posts", null); 
}); 

現在你可以使用你的模板助手等地Session.get("current-user-posts")得到用戶的帖子。

+0

非常感謝你。 。讓我試試看,也許它會理清我的問題:) –

+0

它的工作;然而,我對如何在客戶端顯示數據感到困惑。除非有辦法將數據從模板onCreated傳遞給模板幫助程序,否則看起來幫助程序是無用的。 我可以知道您是如何在客戶端上展示數據的? –

+0

我認爲這是正確的會話? –