在View - Backbone上的元素后追加多个视图

时间:2015-10-11 09:42:41

标签: javascript jquery backbone.js

   addDimensions: function (order_id, counter) {
    this.dimensionsView = new dimensionsView({ el: "#panel-boxes-" + order_id + "_" + counter, id: order_id, counter: counter });
    $("#panel-boxes-" + order_id + "_1").append(this.dimensionsView.render().el);
  }

我试图在父视图中存在div后附加多个视图

但我得到错误el未定义虽然我已经定义了el

我的尺寸视图:

    var $ = jQuery = require('jquery'),
    Backbone = require('backbone'),
    Handlebars = require('handlebars'),
    _ = require('underscore'),
    messagesHandler = require('../../../libs/messages/messagesHandler'),
    dimensionsTemplate = require("../../../templates/order/actions/Dimensions.html");
var DimensionsView = Backbone.View.extend({
    tagName: "div",
    initialize: function (options) {
        this.id = options.id;
        this.counter = options.counter;
    },
    render: function () {
        this.$el.html(dimensionsTemplate({ id: this.id, counter: this.counter }));
    }

});

module.exports = DimensionsView;

错误:

 $("#panel-boxes-" + order_id + "_1").append(this.dimensionsView.render().el); //Error here

enter image description here

1 个答案:

答案 0 :(得分:3)

目前,DimensionsView的渲染方法不会返回任何内容,因此会返回undefined(默认值),因此dimensionsView.render().elundefined.el等效,导致错误。< / p>

您需要从render方法返回对视图的引用,以便您可以将其链接为render().el

render: function () {
    this.$el.html(dimensionsTemplate({ id: this.id, counter: this.counter }));
    return this;
}