user1831230
user1831230

Reputation: 136

Meteor.js How do I know my collection is ready on the client

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?

Upvotes: 6

Views: 1727

Answers (2)

rzymek
rzymek

Reputation: 9281

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
    });
}

Upvotes: 3

Andreas
Andreas

Reputation: 1622

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).

Upvotes: 2

Related Questions