骨干js,绑定模型来查看

时间:2012-06-02 12:06:57

标签: javascript backbone.js

我正在尝试使用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' 

1 个答案:

答案 0 :(得分:4)

好的,请亲自测试一下,但它似乎与Github版本的下划线一起工作,所以可能有一个错误已修复.. http://jsfiddle.net/yjZVd/6/