lkahtz
lkahtz

Reputation: 4796

Uncaught Error: Method "undefined" does not exist backbone.js:1291

Really scratching my head now. what is wrong with the following backbone.js code? It keeps complaining "Uncaught Error: Method "undefined" does not exist backbone.js:1291"

Failed code is here: 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);

Upvotes: 0

Views: 1169

Answers (1)

Matt Whipple
Matt Whipple

Reputation: 7134

events: {
  "click button#btn": "addItem"
},

Upvotes: 2

Related Questions