骨干事件没有解雇

时间:2012-05-16 15:47:19

标签: javascript backbone.js

尝试在视图中触发事件,但似乎没有触发。此视图从另一个视图中运行,因此不确定事件是否未正确设置。

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

1 个答案:

答案 0 :(得分:3)

您正在覆盖this.el,而您想要做的是

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

如果这导致你的DOM表示有问题,周围有一个额外的包裹元素,请尝试这样做:

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