2015-02-12 144 views
0

我在主干问题上没有找到我的视图。这里的代码骨干不初始化视图。 “undefined不是函数”

这里的意见。

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

+0

面对几乎类似的问题,关于这个问题的任何更新? ! – semuzaboi 2015-07-21 11:14:18

回答

0

你是指SummaryListView声明之前,因此误差所以 你必须在SummaryTableView

声明SummaryListView的顺序应该是这样的

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

+0

不是这样。我曾尝试过,但没有奏效。我收到你的回复后再次尝试 - 没有改变。我用我的代码在问题中显示的相同方式创建了其他表。在切换代码的​​位置之后,现在控制台显示无法找到SummaryTableView的错误。任何想法为什么这样做? – user1828605 2015-02-12 13:47:38

+0

没关系!我不知道发生了什么事。我在一个单独的文件中重新创建了视图,现在它正在工作。这很奇怪,因为我甚至证实view.js已加载。我真的不知道发生了什么事,我一定做了一些错误的事情,一旦我重新创建了文件,不知何故被纠正了。现在正在工作。 谢谢 – user1828605 2015-02-12 15:22:54