0
現在真的在撓我的腦袋。以下backbone.js代碼有什麼問題?它不斷抱怨「未捕獲的錯誤:方法 」未定義「 不存在Backbone.js的:1291」未捕獲錯誤:方法「undefined」不存在backbone.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);
你忘了某處添加一個回調。打開1291行的backbone.js,看看它試圖調用什麼。 –
我得到'TypeError:當我嘗試運行JSFiddle時,無法在Mac上的Chrome上讀取undefined'的屬性'bind'。你在運行哪個瀏覽器? – rjsvaljean
@rjsvaljean,我得到了使用無壓縮backbone.js的錯誤。壓縮版本會給你說的錯誤。 – lkahtz