Skip to content Skip to sidebar Skip to footer

Backbone.js Calling Render After Collection Is Populated

I'm trying to call render after fetch of a collection. In my initialize method I have: this.collection.bind('all', this.render, this); this.collection.fetch(); In IE it seems that

Solution 1:

If you want to guarantee that data has been fetched before rendering it, then I'd suggest using jQuery's deferred object:

this.collection.deferred = this.collection.fetch();
self = this;
this.collection.deferred.done(function() {
    self.collection.render();
  }
);

Basically anything you put into the function you send to done will only be called after fetch is, well, done.

More on deferreds:

Solution 2:

Looking at Backbone's Catalog of Events sounds like you want to listen to the reset event

Solution 3:

The following should work:

this.collection.on('reset', this.render);
this.collection.fetch();

Normally in Backbone.js one would use on() to bind a callback to an object.

Solution 4:

The following should definitely work, it worked for me...

instead of the below

this.collection.fetch();

use this

this.collection.fetch(async: false);

Post a Comment for "Backbone.js Calling Render After Collection Is Populated"