0

Imagine I have 2 collections

Post {
  _id: ...
  title: ...
}

Comment {
  _id: ...
  postId: ...
  text: ....
}

On a post detail page, I want to see the post title and all of its comments which must be reactive.

  • I can declare a Meteor.methods to return the post and its comments with one request but I dont know how to make the comments reactive.
  • I can get the post first and then Meteor.subscribe to its comments based on the post's id, but this solution requires 2 sequential requests which is not ideal.

How can I have both of them and still have comments reactive.

Thank you.

4

2 回答 2

0

实际上,您可以在“Meteor.publish”函数中返回多个集合:

Meteor.publish("postWithComments", function(postId){
   return [Posts.find({_id: postId}), Comments.find({postId: postId})];
});

如果您订阅此内容,您的本地 mini mongo 将收到这两个集合。限制是每个游标必须来自不同的集合。阅读文档

于 2013-05-29T19:38:57.110 回答
-1

如果评论嵌入到每个帖子的同一数据库集合中,那么您可以一次性返回帖子及其相关评论。如果它们在单独的集合中(如您的情况),那么您需要订阅/请求两者。

在反应性方面,Meteor 的发布和订阅功能会自动将新鲜内容从服务器传送到客户端。您还可以将 Session 变量传递到您的客户端订阅函数中,在这种情况下,您将使用 Deps.autorun 在每次 Session 变量更改时自动重新运行请求。Meteor发布和订阅文档中对此进行了讨论。

于 2013-05-28T23:43:00.347 回答