0

I've got a backbone view for an entire collection (a list of "clickable" categories). Can I delegate events on each item of the view so that I can find which category has been clicked?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Alex Epelde
  • 1,679
  • 1
  • 15
  • 16

2 Answers2

2

Here's a post that might help. Basically you use a data-* attribute in the item view to store and then retrieve the id of item clicked:

If you'd rather go directly to code, here's the jsFiddle that's used in the post to demonstrate. Hope that helps.

ataddeini
  • 4,931
  • 26
  • 34
1

I have no answer for your question (no, I think), but would like to share my approach: a general collection view component, which renders a collection using other view. It can be as simple as in the example below or more sophisticated (listening add/remove/reset events and react accordingly).

var CollectionView = Backbone.View.extend({
    render : function() {
        this.options.collection.each(function(model) {
            this.$el.append((new this.options.view({model : model})).el);
        }, this);
    }
})
Yaroslav
  • 4,543
  • 5
  • 26
  • 36