[英]Backbone.js Not Iterating Over Collection Using .each()
I've been doing some Backbone.js coding and have come across a particular problem where I am having trouble iterating over the contents of a collection. 我一直在做一些Backbone.js编码,遇到了一个特殊的问题,在该问题上我无法遍历集合的内容。 The line
Tasker_TodoList.each(this.addOne, this);
行
Tasker_TodoList.each(this.addOne, this);
in the addAll
function in AppView
is not executing properly for some reason, throwing the error: Uncaught TypeError: undefined is not a function
由于某种原因,
AppView
的addAll
函数中的AppView
未正确执行,抛出错误: Uncaught TypeError: undefined is not a function
LINK: RUNNING APP WITH ERROR -> Type something in the input box, press '+' button see error in console. 链接: 运行应用程序时出现错误 ->在输入框中输入内容,然后按“ +”按钮在控制台中看到错误。
the code in question is: 有问题的代码是:
$(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();
});
can somebody help me in finding out what I am doing wrong? 有人可以帮助我找出我做错了什么吗? Thank you all for your help :-)
谢谢大家的帮助 :-)
change initialize function on AppView from this: 从此更改AppView上的初始化函数:
Tasker_TodoList.on('add', this.addAll);
to this: 对此:
Tasker_TodoList.on('add', this.addAll,this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.