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 回答