0

今、本当に頭を悩ませています。次の backbone.js コードの何が問題になっていますか? 「Uncaught Error: Method "undefined" does not exist backbone.js:1291」と不平を言い続けます

失敗したコードはこちら: http://jsfiddle.net/toeinriver/ntK7r/19/

(function($){
    var Person = Backbone.Model.extend({
        defaults:{
         age: 0,
         name: "tom" 
        }
    });

    var People = Backbone.Collection.extend({
        model: Person});

    ListView = Backbone.View.extend({
        el: $("body"),
        events: {
            "click button#btn": this.addItem
        },
        initialize: function(){
            this.count = 0;
            _.bindAll(this, "render", "appendItem","addItem");
            this.collection = new People();
            this.collection.bind("add", this.appendItem);
            this.counter = 0;
            this.render();
        },
        render: function(){
            var self = this;
            $(this.el).append("<button id='btn'>Press me</button>");
            $(this.el).append("<ul></ul>");
            _(this.collection.models).each(function(item){
                self.appendItem(item);
                this.count+=1;
            },this);
        },
        appendItem: function(item){
            $("ul", this.el).append("<li>" + item.get("name") +" at" + item.get("age") + "</li>");
        },
        addItem: function(){
            var p = new Person();
            p.set({age:this.count});
            this.count += 1;
            this.collection.add(p);
        }
    });

    var listView = new ListView();


})(jQuery);
4

1 に答える 1

2
events: {
  "click button#btn": "addItem"
},
于 2012-10-18T02:05:25.233 に答える