can't catch Backbone Collection reset event

2019-09-14 16:58发布

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

1条回答
不美不萌又怎样
2楼-- · 2019-09-14 17:13

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
    }
});
查看更多
登录 后发表回答