ide*_*xer 5 subscription publish-subscribe meteor
每当我在网络上遇到代码片段时,我都会看到类似的内容
Meteor.subscribe('posts', 'bob-smith');
Run Code Online (Sandbox Code Playgroud)
然后客户端可以显示"bob-smith"的所有帖子.
订阅返回多个文档.
相比之下,我需要的是单文档订阅,以显示文章的正文字段.我想通过(文章)id过滤:
Meteor.subscribe('articles', articleId);
Run Code Online (Sandbox Code Playgroud)
但是当我在网上搜索类似的例子时,我开始怀疑:我甚至找不到一个单文档订阅示例.
这是什么原因?为什么没有人使用单文档订阅?
哦,但人们呢!
这并不违反我所知道的任何最佳做法.
例如,这里是从GitHub的库中的代码示例望远镜,你可以看到一个出版物检索基于他或她的ID单个用户.
实际上,仅仅订阅应用程序中给定时刻所需的数据是明智的.如果您正在撰写单个帖子页面,则应为其发布单个帖子/订阅,例如:
Meteor.publish('singleArticle', function (articleId) {
return Articles.find({_id: articleId});
});
// Then, from an iron-router route for example:
Meteor.subscribe('singleArticle', this.params.articleId);
Run Code Online (Sandbox Code Playgroud)