javascript - Avoiding need to call _.bindAll in backbone -


from reading appears when extending backbone.js class such model, common pattern call _.bindall in constructor follows (see https://raw.github.com/eschwartz/backbone.googlemaps/master/lib/backbone.googlemaps.js):

googlemaps.location = backbone.model.extend({   constructor: function() {     _.bindall(this, 'select', 'deselect', 'toggleselect', 'getlatlng', 'getlatlng');     // etcetera 

i understand why gets done, need explicitly passing method names _.bindall seems maintenance issue -- if add new methods, have remember add them argument _.bindall well.

earlier today posted verbose solution here: https://stackoverflow.com/a/17977852/34806

however, shouldn't following simple technique altogether avoid need call _.bindall? , is, instead of customary way of assigning "custom" methods, instead attach them "this" within constructor:

constructor: function() {     this.foo = function() {};     this.bar = function() {}; } 

are there downsides technique?

your technique doesn't work, still need bind methods other. or use var = self methods cannot used in sub-classes or anywhere else matter.

just rid of bindall altogether , bind in time when need it, e.g. when passing method callback somewhere. should far easier on maintenance because chore localized. forgetting bind when passing method callback should feel wrong forgetting use var.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -