0

I've stumbled across something quite strange

I'm fetching a collection, and listening on the reset event, but somehow the event is lost

I have this minimal example:

$(function() {
  var collection = new Backbone.Collection();
  collection.url = 'http://localhost:9000/api/Usuario';
  collection.on('reset', function() {
    console.log('collection reset!');
  });
  collection.fetch();
});

Inspecting the network I can see that the request is seuccessful, and the web service returns json data

But there's no way that the cosole.log('collection reset!') callback is executed.

There must be something really silly that I'm missing...

opensas
  • 60,462
  • 79
  • 252
  • 386

1 Answers1

1

From Backbone documentation

It uses set to (intelligently) merge the fetched models, unless you pass {reset: true},

So I guess, Using this will solve your problem.

collection.fetch({
    reset: true,
    success: function() {
        // Do Something
        // This is called when all add, remove and update operations have been done
    }
});
Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
  • yea, I realized about it, I just wonder what would be the default way yo do it. It's strange that they don trigger a "fetched" event or something like that after all the changes (add, destroy, updates) have been issued – opensas May 14 '13 at 08:27
  • I think there is no need of 'fetched event' when we have successhandler. Check this http://backbonejs.org/#Collection-fetch You can define a successhandler and in that you can do whatever you want. Updating my code. Check it. – Sachin Jain May 14 '13 at 10:00