I'm trying to learn Meteor, and currently trying to wrap my head around publications and subscriptions. I'm following the discover meteor book and one main point just doesn't make sense to me and was hoping some explanations in simple terms can be shared.
So a publication is what "fetches" the data from the mongo database to store within Meteor:
Meteor.publish('posts', function() {
return Posts.find();
});
Then on the client side I subscribe to the publication. Woopy
Meteor.subscribe('posts');
What doesn't make sense is the Template Helpers. Originally Discover Meteor tells you to create an array of static posts that iterate through each post using a template helper. Well now that I'm turning things dynamically my template helper becomes:
Template.postsList.helpers({
posts: function () {
return Posts.find();
}
});
What's the point of running the Posts.find() on both the server and client template helper?
Posts in publishing are server side collection. Posts in helpers are client-side collection which contains all published Posts. If you have thousands of posts you usually don't want to publish all Posts because it will take a few seconds to download the data.
You should publish just the data you need.
Meteor.publish('posts', function(limit) {
return Posts.find({}, { limit: limit});
});
When you call this subscribe function, client-side collection Posts will contain just 100 posts.
var limit = 100;
Meteor.subscribe('posts', limit);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With