我正在嘗試backbonejs,但卡住瞭如何將模型綁定到視圖。骨幹js,綁定模型查看
yepnope({
load : ["/static/js/lib/jquery-1.6.2.min.js", "/static/js/lib/underscore-min.js", "/static/js/lib/backbone-min.js"],
complete: nameList
});
function nameList() {
var PageItem = Backbone.Model.extend({
defaults: {name: "default name" }
});
var Page = Backbone.Collection.extend({
model: PageItem
});
var page = new Page;
var AppView = Backbone.View.extend({
el: $("#names"),
$artistList: $('#names_list'),
$inputField: $('input#new_name'),
events: {
"keypress input": "processKeyPress"
},
processKeyPress: function(event){
if(event.charCode == 13) {
event.preventDefault();
this.addName();
}
},
addName: function(event) {
var newName = this.$inputField.val();
this.$artistList.prepend('<li>' + newName + '</li>');
page.push(new PageItem({name: newName}));
// I've also tried page.push({name: newName});
} });
var app = new AppView;}
當我按下輸入字段上的輸入時,它會運行processKeyPress調用addName,新名稱將添加到html列表中,但不會推送到模型上。我不斷收到:
Uncaught TypeError: Object function (a){return new l(a)} has no method 'isObject'
確定..怪異..使用「添加」而不是「推」作品.. – lecstor
它不在我的電腦上。 –
omg。好。抱歉。我去睡覺了。是的,我又錯了......我分手的jsfiddle使用了一個古老的骨幹,給出了相當不同的結果......祝你好運。 – lecstor