Detect when a backbone collection has been fetched (Backbone 1.0.0)

backbone.jsbackbone.js-collections

There's a new behaviour in the latest version of Backbone (1.0.0 in which the reset event is no longer triggered by default after fetching a Collection.

http://backbonejs.org/#changelog

Renamed Collection's "update" to set, for parallelism with the similar
model.set(), and contrast with reset. It's now the default updating
mechanism after a fetch. If you'd like to continue using "reset", pass
{reset: true}.

The problem is that I want to capture the event when the collection has been finally fetched (pretty common case, indeed!)

I could listen to add, remove and change event, but if the collection is empty I don't know how to catch the event.

So, what would be the new, recommended way to catch when the collection request has finalized, or is it passing a { reset = true } the only way to achieve it???

ps: here's the original question, BTW can't catch Backbone Collection reset event

Best Answer

From Backbone.sync doc,

Whenever a model or collection begins a sync with the server, a "request" event is emitted. If the request completes successfully you'll get a "sync" event, and an "error" event if not.

For example,

var C = Backbone.Collection.extend({
    url: '/echo/json/'
});

var c = new C();
c.on('sync', function() {
    console.log('sync');
});
c.fetch();

And a demo http://jsfiddle.net/nikoshr/GLATm/

Related Topic