[英]Uncaught TypeError: Object [object Window] has no method 'each'
[英]Uncaught TypeError: Object [object Window] has no method 'each' function
大家好,這是我的js文件,我在第24行收到有關每個函數的錯誤消息,但我不知道為什么找不到錯誤。 我只是想在console.log面板上查看項目列表,但它甚至沒有在html頁面上列出我的列表。
(function() {
window.App = {
Models: {},
Collections: {},
Views: {}
};
window.template = function(id){
return _.template( $('#' + id).html() );
};
App.Models.Task = Backbone.Model.extend({});
App.Collections.Task = Backbone.Collection.extend({
model: App.Models.Task
});
App.Views.Tasks = Backbone.View.extend({
tagName: 'ul',
render: function(){
this.collection.each( this.addOne, this);
return this;
},
addOne: function(task){
//creating new child view
var taskView = new App.Views.Task({ model: task });
//append to the root element
this.$el.append(taskView.render().el);
}
});
App.Views.Task = Backbone.View.extend({
tagName: 'li',
template: template('taskTemplate'),
events: {
'click .edit': 'editTask'
},
editTask: function(){
alert('you are editing the tas.');
},
render: function(){
var template = this.template( this.model.toJSON() );
this.$el.html(template);
return this;
}
});
var tasksCollection = new App.Views.Task([
{
title: 'Go to the store',
priority: 4
},
{
title: 'Go to the mall',
priority: 3
},
{
title: 'get to work',
priority: 5
}
]);
var tasksView = new App.Views.Tasks({ collection: tasksCollection });
$('.tasks').html(tasksView.render().el);
})();
您正在創建一個視圖實例,就像它是一個類一樣:
App.Views.Tasks = Backbone.View.extend({ /* ... */ });
var tasksCollection = new App.Views.Task([
{
title: 'Go to the store',
priority: 4
},
//...
然后創建該視圖的另一個實例,並將其tasksCollection
,就好像它確實是一個集合:
var tasksView = new App.Views.Tasks({ collection: tasksCollection });
但是視圖和集合是不同的東西,只有集合具有each
方法(除非您將each
添加到視圖中)。
您想要將tasksCollection
創建為App.Collections.Task
:
var tasksCollection = new App.Collections.Task([
{
title: 'Go to the store',
priority: 4
},
//...
嗨,這正在發生,因為您的每種方法都無法找到集合。 以及單個任務到任務
在此行:更改此
var tasksCollection = new App.Views.Task([
到,這個:
var tasksCollection = new App.Collections.Tasks([
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.