2016-04-15 130 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

我认为这是正确的会话? –