Publish functions can return a Collection.Cursor, in which case Meteor will publish that cursor’s documents to each subscribed client. You can also return an array of Collection.Cursors, in which case Meteor will publish all of the cursors.
// server
Meteor.publish("posts", function(filter) {
check(filter, Match.Optional(String)); // validate input.
if (this.userId) { // check to see if user is logged in. You can remove it if you don't need it.
if (filter) {
return posts.find({ category: filter}, {sort: {time_created: 1}, limit: 5});
}
return posts.find({}, {sort: {time_created: 1}, limit: 5});
}
return this.ready(); // Needed if you're not returning anything.
});
//client
Meteor.subscribe("posts", "science");
If you’re using Iron router, you can put the subscription in waitOn hook for that route, which is reactive and gets destroyed when you go to a different route.
I JUST became aware you can pass data in to the subscription model. So you’re exactly right on the subscription from client. Makes sense with minimongo! Very cool.
Now, last question which as I can see in the docs, I can build some sort of array to pass multiple data sets back.
You can parameterize your publish as much as you like.
Meteor.publish("posts", function(param1, param2, param3, ...) {
// you can return cursors from multiple collections here, like
return [
posts.find({}),
users.find({}),
...
];
// however you can't return multiple cursors from the same collection in the same publish. You have to build your query to handle that instead.
return posts.find({category: {$in: ["science", "art", ...]}});
});
I think I need to have a Simple Schema to do this. Sigh. All the small things that get in the way of development. Why doesn’t Meteor just come with routing, schemas, and things that friggin let you work out of the box.