_.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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…