Rob
Rob

Reputation: 11368

Backbone event not firing

Trying to get an event to fire in a view but it doesn't seem to be triggering. This view runs from within another view, so not sure if the event is not being setup properly.

var ListRow = Backbone.View.extend(
{
    events:
    {
        'click .button.delete': 'destroy'
    },

    initialize: function()
    {
        _.bindAll(this, 'render', 'remove');
    },

    render: function()
    {
        this.el = _.template($('#tpl-sTableList_' + key + 'Row').html());

        return this;
    },


    destroy: function()
    {
        console.log('remove')
    }
});

Upvotes: 1

Views: 754

Answers (1)

Vincent Briglia
Vincent Briglia

Reputation: 3068

You're overwriting your this.el, what you want to do instead is

render: function ()
{
    var tpl = _.template($('#tpl-sTableList_' + key + 'Row').html());
    this.$el.empty().html(tpl);
    // or if you prefer the old way
    // $(this.el).empty().html(tpl);
    return this;
},

if that's causing you problems with your DOM representation having an extra wrapped element around it try this instead:

render: function ()
{
    var tpl = _.template($('#tpl-sTableList_' + key + 'Row').html());
    this.setElement(tpl, true); // this is a Backbone helper 
    return this;
},

Upvotes: 3

Related Questions