Backbone没有初始化视图。 “undefined不是一个函数”

时间:2015-02-12 05:27:00

标签: backbone.js backbone-views

我在骨干方面遇到问题但是没有找到我的观点。这是代码

以下是观点。

App.Views.SummaryTableView = Backbone.View.extend({
    tagName: 'tbody',

    initialize: function () {
        this.childViews = [];
        this.collection.on('add', this.addOne, this);
        this.collection.on('change reset', this.render, this);
        console.log(this.collection);
    },

    addOne: function (appSummary) {
        console.log('should be receiving model');
        console.log(appSummary);

        var appSumTable = new App.Views.SummaryListView({ model: appSummary });
        console.log(appSummary);        
        this.$el.append(appSumTable.render().el);
        this.childViews.push(appSumTable);
    },
    render: function () {
        console.log('rendiring collection: ' + this.collection);
        this.collection.each(this.addOne, this);
        console.log('sending the model');
        return this;
    },
    close: function () {
        this.remove();
        this.unbind();

        this.childViews = [];
    }

});

App.Views.SummaryListView = Backbone.View.extend({
    tagName: 'tr',
    template: template('app-summary-table-template'),
    initialize: function () {
        console.log(this.model);
        this.model.on('add', this.render, this);
    },
    render: function () {
        console.log('rendering');
        var mod = this.model.toJSON();
        this.$el.html(this.template(mod));
        return this;
    },
    close: function () {
        this.remove();
        this.unbind();

    }
});

SummaryTableView具有集合,视图将模型发送到SummaryListView。该集合工作正常,模型包含数据。但由于某些原因,当我运行代码时,它一直说SummaryListView是未定义的。它无法找到视图。难道我做错了什么?我在这行中得到错误: var appSumTable = new App.Views.SummaryListView({ model: appSummary });

1 个答案:

答案 0 :(得分:0)

在声明之前你是在引用SummaryListView,因此错误是 您必须在SummaryListView

之前声明SummaryTableView

订单应该是这样的

App.Views.SummaryListView = Backbone.View.extend({
    tagName: 'tr',
      .
      .
});

App.Views.SummaryTableView = Backbone.View.extend({
    tagName: 'tbody',
     . 
     .
     addOne: function (appSummary) {
       console.log('should be receiving model');
       console.log(appSummary);
       var appSumTable = new App.Views.SummaryListView({ model: appSummary});
     .
     .
});

Fiddle