Meteor:单文档订阅

Meteor: Single-Document Subscription

每当我在网络上遇到代码片段时,我都会看到类似

的内容
Meteor.subscribe('posts', 'bob-smith');

然后客户端可以显示 "bob-smith" 的 所有 个帖子。

订阅returns几个个文档。

相比之下,我需要的是单文档订阅,以便显示文章的正文字段。我想按(文章)id 过滤:

Meteor.subscribe('articles', articleId);

但是当我在网上搜索类似的例子时,我开始怀疑了:我连一个单文档订阅的例子都找不到。

这是什么原因?为什么没有人使用单文档订阅?

哦,但人们确实如此!

这并不违反我所知道的任何最佳实践。

例如,here is a code sample from the github repository of Telescope 您可以在其中看到根据用户 ID 检索单个用户的出版物。

Here is another one for retrieving a single post, and here是它的订阅。

订阅您在应用中特定时刻所需的数据实际上是明智的。如果你正在写一个 post 页,你 应该 为它做一个 post publication/subscription,例如:

Meteor.publish('singleArticle', function (articleId) {
  return Articles.find({_id: articleId});
});

// Then, from an iron-router route for example:
Meteor.subscribe('singleArticle', this.params.articleId);

使用单个文档订阅的常见模式是参数化路由,例如:/posts/:_id - 您会在此处的许多 iron:router 答案中看到这些。