In my backbone.js app, there is a Trips collection
that holds Trip models
, which is working with LocalStorage
. I am able to call
Collection.fetch()
will call reset
on success, which in turn will trigger a 'reset' event. Any subscribers to the collections reset event should receive the event.
The key here is "on success." I had this problem, only to discover that backbone was silently swallowing my errors messages. Pass in an error handler that, at least, logs to console.log()
, and see what's happening:
trips.fetch({error: function() { console.log(arguments); }});
(Note: Old versions of backbone.js will trigger "refresh" instead of "reset")
As of backbone 1.0, model.fetch() triggers a 'sync'. That's what you should bind to.
Here's the relevant part from the backbone.js source where the 'sync' event is fired:
fetch: function(options) {
options = options ? _.clone(options) : {};
if (options.parse === void 0) options.parse = true;
var model = this;
var success = options.success;
options.success = function(resp) {
if (!model.set(model.parse(resp, options), options)) return false;
if (success) success(model, resp, options);
// HERE'S THE TRIGGER!
model.trigger('sync', model, resp, options);
};
wrapError(this, options);
return this.sync('read', this, options);
},
If you are using backbone 1.0, you'll need to pass reset:true in the fetch() call in order to bind with the reset event:
trips.fetch({reset: true});