现在真的很挠头。以下backbone.js 代码有什么问题?它一直在抱怨“未捕获的错误:方法“未定义”不存在主干.js:1291”
失败的代码在这里:http: //jsfiddle.net/toeinriver/ntK7r/19/
(function($){
var Person = Backbone.Model.extend({
defaults:{
age: 0,
name: "tom"
}
});
var People = Backbone.Collection.extend({
model: Person});
ListView = Backbone.View.extend({
el: $("body"),
events: {
"click button#btn": this.addItem
},
initialize: function(){
this.count = 0;
_.bindAll(this, "render", "appendItem","addItem");
this.collection = new People();
this.collection.bind("add", this.appendItem);
this.counter = 0;
this.render();
},
render: function(){
var self = this;
$(this.el).append("<button id='btn'>Press me</button>");
$(this.el).append("<ul></ul>");
_(this.collection.models).each(function(item){
self.appendItem(item);
this.count+=1;
},this);
},
appendItem: function(item){
$("ul", this.el).append("<li>" + item.get("name") +" at" + item.get("age") + "</li>");
},
addItem: function(){
var p = new Person();
p.set({age:this.count});
this.count += 1;
this.collection.add(p);
}
});
var listView = new ListView();
})(jQuery);