I need to perform a specific rendering task when a Collection has completed its load on the client. What's the best strategy for knowing when the data is available in the Collection on the client.
I've come up with this solution:
Meteor.subscribe( 'alldrawings', myRendering );
function myRendering(){
//do some no-markup stuff with the data
//(i'm creating canvas objs and drawing on them)
}
Is this the way to do it? Is there a recommended method other than this?
You can setup a reactive variable yourself:
alldrawingsReady = new ReactiveVar(false);
Meteor.subscribe('alldrawings', function() {
alldrawingsReady.set(true);
});
Tracker.autorun(function(){
if(!alldrawingsReady.get()) {
return;
}
// Do some no-markup stuff with the data
// (eg. creating canvas objs and drawing on them)
});
And if you'd need to have a collection and a template ready, use this:
Template.my_template.rendered = function() {
this.autorun(function(){
if(!alldrawingsReady.get()) {
return;
}
// Do some DOM manipulations based on the data
});
}
You can of course use the subscription callback. AFAIK, this is the only possible way to detect, if a subscription was updated completely.
Another option is to use an observer with a collection cursor: http://docs.meteor.com/#observe. But I think the observers are called constantly, one by one, as the data arrives, and not once on completion (only).
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