Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 489
user1831230 Avatar asked Nov 17 '12 03:11

user1831230


2 Answers

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
    });
}
like image 100
rzymek Avatar answered Oct 06 '22 06:10

rzymek


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

like image 22
Andreas Avatar answered Oct 06 '22 07:10

Andreas