4

I've been trying to implement a "delete" button for backgrid rows. A solution seems to be described here:

How to add a custom delete option for backgrid rows

However, I don't seem to get it working. Here is what I tried:

var DeleteCell = Backgrid.Cell.extend({
    template: _.template('<button>Delete</button>'),
    events: {
      "click": "deleteRow"
    },
    deleteRow: function (e) {
      console.log("Hello");
      e.preventDefault();
      this.model.collection.remove(this.model);
    },
    render: function () {
      this.el.html(this.template());
      this.delegateEvents();
      return this;
    }
});

and then using it like

var columns = [{
        name: "id", // The key of the model attribute
        label: "ID", // The name to display in the header
        editable: false, // By default every cell in a column is editable, but *ID* shouldn't be
        renderable: false,
        // Defines a cell type, and ID is displayed as an integer without the ',' separating 1000s.
        cell: Backgrid.IntegerCell.extend({
            orderSeparator: ''
        })
    }, {
        name: "weight",
        label: "Hello",
        cell: DeleteCell
    },{
        name: "datemeasured",
        label: "DateMeasured",
        // The cell type can be a reference of a Backgrid.Cell subclass, any Backgrid.Cell subclass instances like *id* above, or a string
        cell: "datetime" // This is converted to "StringCell" and a corresponding class in the Backgrid package namespace is looked up
    },{
        name: "weight",
        label: "Weight",
        cell: "number" // An integer cell is a number cell that displays humanized integers
    }];

what I get is an error:

TypeError: this.el.html is not a function
[Break On This Error]   

this.el.html(this.template());

Any suggestions?

Thanks & cheers

4

2 回答 2

4

由于您尝试在错误的视图属性上调用该函数,因此您遇到了异常。Backbone 视图有两种不同的方式来访问它的 el,直接在 DOM 中或作为 jQuery 对象,如下所示:

  1. this.el- 这是对 DOM 元素的直接引用。
  2. this.$el- DOM 元素的 jQuery 对象。

重要的是要记住,您需要调用属性上的append()和之类的函数,因为它们是 jQuery 函数。html()$el

于 2013-07-12T12:07:07.847 回答
0

dcarson 当然是正确的,但只是为了非常清楚地拼出我能够用来使其正常工作的渲染函数的代码,用 this.$el 代替 this.el:

render: function () {
    this.$el.html(this.template());
    this.delegateEvents();
    return this;
}
于 2015-02-05T15:35:59.900 回答