refactor(core): use `Function.bind` for referring to event listeners instead of a closure.

This commit is contained in:
Tobias Bosch 2016-04-29 12:09:29 -07:00
parent 4d691b61ee
commit aecb60a604
1 changed files with 6 additions and 12 deletions

View File

@ -83,14 +83,9 @@ export class CompileEventListener {
listenToRenderer() { listenToRenderer() {
var listenExpr; var listenExpr;
var eventListener = o.THIS_EXPR.callMethod('eventHandler', [ var eventListener = o.THIS_EXPR.callMethod(
o.fn([this._eventParam], 'eventHandler',
[ [o.THIS_EXPR.prop(this._methodName).callMethod(o.BuiltinMethod.bind, [o.THIS_EXPR])]);
new o.ReturnStatement(
o.THIS_EXPR.callMethod(this._methodName, [EventHandlerVars.event]))
],
o.BOOL_TYPE)
]);
if (isPresent(this.eventTarget)) { if (isPresent(this.eventTarget)) {
listenExpr = ViewProperties.renderer.callMethod( listenExpr = ViewProperties.renderer.callMethod(
'listenGlobal', [o.literal(this.eventTarget), o.literal(this.eventName), eventListener]); 'listenGlobal', [o.literal(this.eventTarget), o.literal(this.eventName), eventListener]);
@ -107,10 +102,9 @@ export class CompileEventListener {
listenToDirective(directiveInstance: o.Expression, observablePropName: string) { listenToDirective(directiveInstance: o.Expression, observablePropName: string) {
var subscription = o.variable(`subscription_${this.compileElement.view.subscriptions.length}`); var subscription = o.variable(`subscription_${this.compileElement.view.subscriptions.length}`);
this.compileElement.view.subscriptions.push(subscription); this.compileElement.view.subscriptions.push(subscription);
var eventListener = o.THIS_EXPR.callMethod('eventHandler', [ var eventListener = o.THIS_EXPR.callMethod(
o.fn([this._eventParam], 'eventHandler',
[o.THIS_EXPR.callMethod(this._methodName, [EventHandlerVars.event]).toStmt()]) [o.THIS_EXPR.prop(this._methodName).callMethod(o.BuiltinMethod.bind, [o.THIS_EXPR])]);
]);
this.compileElement.view.createMethod.addStmt( this.compileElement.view.createMethod.addStmt(
subscription.set(directiveInstance.prop(observablePropName) subscription.set(directiveInstance.prop(observablePropName)
.callMethod(o.BuiltinMethod.SubscribeObservable, [eventListener])) .callMethod(o.BuiltinMethod.SubscribeObservable, [eventListener]))