Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor: Single-Document Subscription

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?

like image 250
ideaboxer Avatar asked Sep 03 '15 13:09

ideaboxer


2 Answers

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);
like image 142
SylvainB Avatar answered Nov 19 '22 13:11

SylvainB


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.

like image 38
Michel Floyd Avatar answered Nov 19 '22 13:11

Michel Floyd