在最新版本的Backbone中有一个新的行为(1.0.0,其中在获取集合后,默认情况下不再触发重置事件.
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}.
问题是我想捕获这个事件时收集已经被最终取出(很常见的情况,确实!)
我可以听取添加,删除和更改事件,但如果集合是空的,我不知道如何捕获事件.
那么,当收集请求已经定稿时,什么是新的,推荐的方式来捕获,还是通过{reset = true}实现它的唯一方法?
ps:这是原来的问题,BTW can’t catch Backbone Collection reset event
解决方法
从
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.
例如,
var C = Backbone.Collection.extend({ url: '/echo/json/' }); var c = new C(); c.on('sync',function() { console.log('sync'); }); c.fetch();