Alex Epelde
Alex Epelde

Reputation: 1739

Delegating events

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?

Upvotes: 0

Views: 123

Answers (2)

ataddeini
ataddeini

Reputation: 4951

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.

Upvotes: 2

Yaroslav
Yaroslav

Reputation: 4659

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);
    }
})

Upvotes: 1

Related Questions