Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone - Object render has no method 'apply'

I am getting the error:

object render has no method apply for the below code.

What can be the reason ? The html page doesnt contain any code except the link for the javascript.
what should I do to remove the error ?

(function($) {

    window.Book = Backbone.Model.extend({});

    window.Library = Backbone.Collection.extend({

        model: Book

    }); // end of Collection
    window.LibraryView = Backbone.View.extend({

        el: $('body'),

        events: {
            'click button#btn_add': 'btn_add'

        },

        initialize: function() {
            $(this.el).append("View initialized");
            _.bindAll(this, 'render', 'btn_add');
            this.collections = new Library();
            this.collections.bind('add', 'render', this);
            this.startingDisplay();

        },
        startingDisplay: function() {
            $(this.el).append("<input type='text' id='t1' /><button id='btn_add'>Add</button>");

        },

        btn_add: function() {

            book = new Book({
                title: "first"
            });
            alert("Name : " + book.get('title'));
            this.collections.add(book);
        },

        render: function() {
            alert("render called");

        },

    }); // end of LibraryView
    libraryview = new LibraryView();

})(jQuery);​
like image 937
user1305989 Avatar asked Apr 01 '12 07:04

user1305989


1 Answers

You are not using the correct syntax to bind the add collection event. Use:

// this.collections.bind('add', 'render', this);
this.collections.bind('add', this.render, this);

The second parameter is expected to be a callback (a function).

DEMO

like image 183
Didier Ghys Avatar answered Nov 02 '22 09:11

Didier Ghys