So something like this:
this.listenTo( foo, 'event', function() {
this.trigger( 'change' );
}.bind(this));
this.listenTo( foo, 'event', function( bar ) {
this.add( bar );
}.bind(this));
Question
I've got two situations where I'm declaring var that = this
and I'm wondering if there is a smarter way of handling it with a native function like call()
or apply()
so I don't have to create an anonymous function?
var that = this;
this.listenTo( foo, 'event', function() {
that.trigger( 'change' );
});
var that = this;
this.listenTo( foo, 'event', function( bar ) {
that.add( bar );
});
La solution
So something like this:
this.listenTo( foo, 'event', function() {
this.trigger( 'change' );
}.bind(this));
this.listenTo( foo, 'event', function( bar ) {
this.add( bar );
}.bind(this));
Autres conseils
Not tested (especialy I think to much binds and this), but how about like this:
this.triggerFn = function(ev) {
return function() {
this.trigger(ev);
}.bind(this);
}.bind(this);
this.addFn = function() {
return function(bar) {
this.add(bar);
}.bind(this);
}.bind(this);
this.listenTo( foo, 'event', this.triggerFn('change'));
this.listenTo( foo, 'event', this.addFn( bar ));
For bind polyfill see that.