2012-06-02 151 views
2

我正在嘗試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' 
+0

確定..怪異..使用「添加」而不是「推」作品.. – lecstor

+0

它不在我的電腦上。 –

+2

omg。好。抱歉。我去睡覺了。是的,我又錯了......我分手的jsfiddle使用了一個古老的骨幹,給出了相當不同的結果......祝你好運。 – lecstor

回答

4

OK,請測試一下,爲自己,但它似乎有下劃線的Github上版本的工作,所以也許有已經修正了一個錯誤.. http://jsfiddle.net/yjZVd/6/

+1

是的。它也適用於我的電腦。 – theotheo

+0

哇,不能相信這個作品。 'good'版本的下劃線:'https:// github.com/documentcloud/underscore/raw/master/underscore-min.js' – brittohalloran