我一直在做一些 Backbone.js 编码,并且遇到了一个特殊的问题,我在迭代集合的内容时遇到了麻烦。由于某种原因,函数 inTasker_TodoList.each(this.addOne, this);
中的行没有正确执行,抛出错误:addAll
AppView
Uncaught TypeError: undefined is not a function
LINK: RUNNING APP WITH ERROR -> 在输入框中输入内容,按“+”按钮在控制台中查看错误。
有问题的代码是:
$(function() {
var Todo = Backbone.Model.extend({
defaults: {
title: "Some Title...",
status: 0 //not completed
}
});
var TodoList = Backbone.Collection.extend({
model: Todo,
localStorage: new Store('tasker')
});
var Tasker_TodoList = new TodoList();
var TodoView = Backbone.View.extend({
tagName: 'li',
template: _.template($('#todoTemplate').html()),
events: {
'click .delbtn': 'delTodo'
},
initialize: function(){
console.log("a new todo initialized");
//this.model.on('change', this.render, this);
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this;
},
delTodo: function(){
console.log("deleted todo");
}
});
var AppView = Backbone.View.extend({
el: 'body',
events: {
'click #addBtn': 'createOnClick'
},
initialize: function(){
Tasker_TodoList.fetch();
Tasker_TodoList.on('add', this.addAll);
console.log(Tasker_TodoList);
},
addAll: function(){
$('#tasksList').html('');
console.log("boooooooma");
Tasker_TodoList.each(this.addOne, this);
},
addOne: function(todo){
console.log(todo);
},
createOnClick: function(){
Tasker_TodoList.create();
}
});
var Tasker = new AppView();
});
有人可以帮我找出我做错了什么吗?感谢大家的帮助 :-)