recipeswithbackbone / recipeswithbackbone.github.com

Marketing site for the best damn backbone.js book evar
23 stars 1 forks source link

_.bindAll #46

Closed redthor closed 12 years ago

redthor commented 12 years ago

Hi Guys, Thanks for the book.

_.bindAll()

is used in a few spots and I understand what it does from the underscore site, but I'm unsure as to why it is required where it is used. I may have missed where you explain why it is necessary. For e.g. (version 2012-01-02 of the book, page 54):

Views.Appointments = Backbone.View.extend({
    template: Templates.Appointments,
        initialize: function(options) {
            _.bindAll(this, 'render', 'addAll', 'addOne');
           this.collection.bind('add', this.addOne);
        },
...

is it necessary to bindAll 'render', 'addAll', and 'addOne'?

Next question: Is using bindAll now deprecated in favor of:

this.collection.on('add', this.addOne, this);

Thanks.

ngauthier commented 12 years ago

Hi Redthor,

You are correct, it is no longer necessary to use _.bindAll with backbone. Event triggered methods are automatically bound to the scope of the object and the preferred method of binding events is, as you noted, this.collection.on('add', this.addOne, this);.

We'll leave this issue open until we update the book to this new style.

eee-c commented 12 years ago

I think we have all of the bindAll() statements removed in source. An updated errata should be available shortly.

I would prefer to switch from .bind() to .on() after we update for Backbone 0.9 / 1.0.

eee-c commented 12 years ago

Fixed in the most recent version of the book.