why do bindAll in backbone.js views?

_.bindAll( this, ... ) is necessary not only for this.$( selector ).doSomething() but generally to be sure that this in your view’s method is always pointing to the view itself.

For example, if we want to refresh our view when the model changes, we bind the view’s render method to the model’s change event:

initialize: function() {
    this.model.bind( 'change', this.render );
},

Without _.bindAll( this, 'render' ), when the model changes this in render will be pointing to the model, not to the view, so we won’t have neither this.el nor this.$ or any other view’s properties available.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)