Reputation: 6851
I'm trying to implement the following pattern in meteor: the server has some data sets, which are published to client as smaller chunks, i.e. the client subscribes to a subset of data.
//server
Meteor.publish("dataWindow", function(){
//publish a subset
return Data.find({},{limit:100};});
});
//client
Meteor.subscribe("dataWindow", function onReady(){
//do stuff with dataWindow, but this doesn't work
//display(dataWindow)
});
However with this implementation on the client the dataWindow is empty.
A couple of things I don't understand:
onReady
(formerly onComplete
)?Upvotes: 1
Views: 1340
Reputation: 64312
I'd recommend reading the meteor book to better understand the basic concepts of meteor. I'd also suggest having a look at the publish and subscribe section of the docs. Without a basic understanding of publish and subscribe it may be hard for you to understand the answers to your questions but I'll give it a shot:
Because they are shared between the server and the client, put the definitions in a directory directly underneath your application directory, e.g. /collections/posts.js
.
Posts = new Meteor.Collection("posts");
I'm going to assume for the rest of this answer that your collection is Posts
, because Data
is a little too generic. So your publish function could look like:
Meteor.publish("postsForDashboard", function(){
return Posts.find({}, {limit: 100});
});
And your subscribe could look like:
Meteor.subscribe("postsForDashboard");
No. The name of the publish function (in our example postsForDashboard
) is just an identifier. The documents will be published to the client using the same collection they were found in (Posts
). This also answers your first question.
You don't. It's just an identifier used to select the publish function. In our example you could do:
console.log(Posts.find().fetch());
Note that it's pretty unusual to directly use the callback for a subscribe. The typical use case is to simply subscribe to the data and then use it in a template.
Iron router is a client-side router which is useful when you want to have more than one route in your app (e.g. /dashboard, /posts, /posts/abc123/edit). A nice feature of iron-router is that it can wait for your data to be ready and display templates accordingly. So for example, while your posts were syncing it could show a spinner, and when they were done it could show a template displaying them. Again, you do not need to directly access the subscribe's callback in order to make this happen.
I hope this was helpful, but I'd strongly recommend reading through the above sources to gain a better understanding of how all this works.
Upvotes: 3