2

私はbackbonejsを試していますが、モデルをビューにバインドする方法に行き詰まりました。

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;}

入力フィールドでEnterキーを押すと、addNameを呼び出すprocessKeyPressが実行され、新しい名前がhtmlリストに追加されますが、モデルにはプッシュされません。私は得続けます:

Uncaught TypeError: Object function (a){return new l(a)} has no method 'isObject' 
4

1 に答える 1

4

わかりました、これを自分でテストしてください。ただし、アンダースコアのGithubバージョンで動作するように見えるので、修正されたバグがあった可能性があります.. http://jsfiddle.net/yjZVd/6/

于 2012-06-02T13:52:26.733 に答える