0

Im trying to utilize Backbone's view events hash to bind a change event to the current view's collection:

events: {
    'this.collection change': 'render'
}

as opposed to my current method:

initialize: function() {
    this.collection.on('change', this.render, this);
}

but this does not seem to fire the event. Are their limitations on binding events to collections using the events hash?

2 Answers2

3
events: {
    // hash
}

Is for hooking up your DOM events to your view functions.

this.collection.on('change', this.render, this);  // Except change is for models

in the initialization function is the way to do it, except the change event is for models and not collections, unless you have designated a custom change event for the collection that is properly triggered.

What you might be looking for is to bind an add remove or reset event to your collection.

jmk2142
  • 8,581
  • 3
  • 31
  • 47
  • If you have more details as to what you're trying to accomplish with your collection and view, if you spell it out I can probably tailor my answer to better solve your problem in context. – jmk2142 Aug 22 '12 at 04:10
0

If you were using Marionette views, you can use collectionEvents in the way you mentioned. See http://marionettejs.com/docs/marionette.view.html#viewmodelevents-and-viewcollectionevents.

collectionEvents: {
    'change': 'render'
}

If you're using plain Backbone, orangewarp's suggestion of listening to the collection's change event using this.collection.on is the way to go.

Jon Onstott
  • 13,499
  • 16
  • 80
  • 133