0

I have a Backbone Collection initialized by running collection.fetch() method, and then after a while, I asked the collection to fetch again in order to refresh the models. so, is there any event from Model fired that I can subscribe from View in order to remove/update the view.

Cyclone
  • 1,580
  • 1
  • 8
  • 14
Shuping
  • 5,388
  • 6
  • 43
  • 66

1 Answers1

2

There isn't a specific "the collection has been refetched" event but you don't need one. fetch resets the collection:

fetch collection.fetch([options])
[...]
When the model data returns from the server, the collection will reset.

And reset triggers a "reset" event:

reset collection.reset(models, [options])
[...]
Use reset to replace a collection with a new list of models (or attribute hashes), triggering a single "reset" event at the end.

So just listen for "reset" events from the collection and re-render the view when you get one.


The behavior of fetch changed in Backbone 1.0, from the 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}.

And if we look at set:

set collection.set(models, [options])

The set method performs a "smart" update of the collection with the passed list of models. If a model in the list isn't yet in the collection it will be added; if the model is already in the collection its attributes will be merged; and if the collection contains any models that aren't present in the list, they'll be removed. All of the appropriate "add", "remove", and "change" events are fired as this happens.

So you can say collection.fetch({ reset: true }) if you want to keep using the "reset" event or you can collection.fetch() and listen for individual "add", "remove", and "change" events.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • I don't think so. The fetch will create new models and the old ones will simply be forgotten (not destroyed) so there won't be any model events, just the single collection event. – mu is too short Nov 07 '12 at 17:18
  • OK, is it free to fetch for multiple times? I am worrying whether it will cause memory problem or not. – Shuping Nov 07 '12 at 17:26
  • Should be fine as long as you don't have any stray references to the models in the collection. So listen to the collection ([which forwards events on its models](http://backbonejs.org/#Collection)) and you should be fine. – mu is too short Nov 07 '12 at 17:59
  • Reset doesn't always appear to fire. – Nathan C. Tresch Aug 09 '13 at 19:56
  • @NathanC.Tresch: Backbone 1.0 changed how `fetch` works, it now ["uses *set* to (intelligently) merge the fetched models, unless you pass `{reset: true}`, in which case the collection will be (efficiently) reset."](http://backbonejs.org/#Collection-fetch). So you can use the `reset:true` option or listen for individual `"add"` or `"remove"` events. You might also notice that 1.0 came out *after* this answer. – mu is too short Aug 09 '13 at 20:02
  • @muistooshort I am indeed passing reset:true, but it's still not triggering. I'm not sure what I'm doing wrong. – Nathan C. Tresch Aug 09 '13 at 20:05