From 03c21a8cb27bd42f5822bbdb07205bf8c42f8069 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 19 Jan 2015 13:00:27 +0100 Subject: [PATCH] feat(events): add the $event local variable to the handler context --- modules/core/src/compiler/view.js | 10 ++++++---- modules/core/test/compiler/view_spec.js | 15 +++++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/modules/core/src/compiler/view.js b/modules/core/src/compiler/view.js index 0ba733269d..b6ea93bd6f 100644 --- a/modules/core/src/compiler/view.js +++ b/modules/core/src/compiler/view.js @@ -391,16 +391,18 @@ export class ProtoView { } static _addNativeEventListener(element: Element, eventName: string, expr, view: View) { + var locals = MapWrapper.create(); DOM.on(element, eventName, (event) => { if (event.target === element) { // Most of the time the event will be fired only when the view is // in the live document. However, in a rare circumstance the // view might get dehydrated, in between the event queuing up and // firing. - // TODO(rado): replace with - // expr.eval(new ContextWithVariableBindings(view.context, {'$event': event})); - // when eval with variable bindinds works. - if (view.hydrated()) expr.eval(view.context); + if (view.hydrated()) { + MapWrapper.set(locals, '\$event', event); + var context = new ContextWithVariableBindings(view.context, locals); + expr.eval(context); + } } }); } diff --git a/modules/core/test/compiler/view_spec.js b/modules/core/test/compiler/view_spec.js index 20e69a65b1..ee150c82e4 100644 --- a/modules/core/test/compiler/view_spec.js +++ b/modules/core/test/compiler/view_spec.js @@ -404,24 +404,29 @@ export function main() { }); describe('event handlers', () => { - var view, ctx, called; + var view, ctx, called, receivedEvent, dispatchedEvent; function createViewAndContext(protoView) { view = createView(protoView); ctx = view.context; called = 0; - ctx.callMe = () => called += 1; + receivedEvent = null; + ctx.callMe = (event) => { + called += 1; + receivedEvent = event; + } } function dispatchClick(el) { - DOM.dispatchEvent(el, DOM.createMouseEvent('click')); + dispatchedEvent = DOM.createMouseEvent('click'); + DOM.dispatchEvent(el, dispatchedEvent); } function createProtoView() { var pv = new ProtoView(el('
'), new ProtoRecordRange()); pv.bindElement(new TestProtoElementInjector(null, 0, [])); - pv.bindEvent('click', parser.parseBinding('callMe()', null)); + pv.bindEvent('click', parser.parseBinding('callMe(\$event)', null)); return pv; } @@ -431,6 +436,7 @@ export function main() { dispatchClick(view.nodes[0]); expect(called).toEqual(1); + expect(receivedEvent).toBe(dispatchedEvent); }); it('should not fire on a bubbled native events', () => { @@ -448,6 +454,7 @@ export function main() { view.dehydrate(); dispatchClick(view.nodes[0]); + expect(called).toEqual(0); }); });