5

Whenever I encounter code snippets on the web, I see something like

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

The client can then display all posts of "bob-smith".

The subscription returns several documents.

What I need, in contrast, is a single-document subscription in order to show an article's body field. I would like to filter by (article) id:

Meteor.subscribe('articles', articleId);

But I got suspicious when I searched the web for similar examples: I cannot find even one single-document subscription example.

What is the reason for that? Why does nobody use single-document subscriptions?

ideaboxer
  • 3,863
  • 8
  • 43
  • 62

2 Answers2

6

Oh but people do!

This is not against any best practice that I know of.

For example, here is a code sample from the github repository of Telescope where you can see a publication for retrieving a single user based on his or her id.

Here is another one for retrieving a single post, and here is the subscription for it.

It is actually sane to subscribe only to the data that you need at a given moment in your app. If you are writing a single post page, you should make a single post publication/subscription for it, such as:

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

// Then, from an iron-router route for example:
Meteor.subscribe('singleArticle', this.params.articleId);
SylvainB
  • 4,765
  • 2
  • 26
  • 39
1

A common pattern that uses a single document subscription is a parameterized route, ex: /posts/:_id - you'll see these in many iron:router answers here.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39