According to doc here: http://documentcloud.github.com/backbone/#FAQ-events collection has sync event fired when I do something to sync collection with server. I try to invoke fetch method on collection and wait for sync event on it, but it never happens. Add event is fired, but I need only one event after syncing all items in collection to update corresponding view. There is another way to get this event fired?
The solution is to fire up sync event maunually in 'success' callback passed as param to fetch method.
this.collection.fetch({add: true, success: function(collection, response){
collection.trigger('sync');
}});
I believe the "sync" event is only fired when you change a model. So if you create, update or delete a model, then the "sync" event will fire.
In your case, I think you want to listen for the "reset" event on the collection.
Edit:
If you're setting the {add:true}
option, then there is no single Backbone event that will fire after all the models have been added. You have a few options:
add
event and expect it be called repeatedly_.debounce()
function.The return value from fetch()
is a jQuery XMLHttpRequest object. It implements the jQuery Deferred interface. So you could listen for that finish. For example:
myCollection.fetch({add:true}).done(function(){
myView.render(); //or whatever
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With