Skip to content

Commit 03c21a8

Browse files
committed
feat(events): add the $event local variable to the handler context
1 parent 156f3d9 commit 03c21a8

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

modules/core/src/compiler/view.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,16 +391,18 @@ export class ProtoView {
391391
}
392392

393393
static _addNativeEventListener(element: Element, eventName: string, expr, view: View) {
394+
var locals = MapWrapper.create();
394395
DOM.on(element, eventName, (event) => {
395396
if (event.target === element) {
396397
// Most of the time the event will be fired only when the view is
397398
// in the live document. However, in a rare circumstance the
398399
// view might get dehydrated, in between the event queuing up and
399400
// firing.
400-
// TODO(rado): replace with
401-
// expr.eval(new ContextWithVariableBindings(view.context, {'$event': event}));
402-
// when eval with variable bindinds works.
403-
if (view.hydrated()) expr.eval(view.context);
401+
if (view.hydrated()) {
402+
MapWrapper.set(locals, '\$event', event);
403+
var context = new ContextWithVariableBindings(view.context, locals);
404+
expr.eval(context);
405+
}
404406
}
405407
});
406408
}

modules/core/test/compiler/view_spec.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,24 +404,29 @@ export function main() {
404404
});
405405

406406
describe('event handlers', () => {
407-
var view, ctx, called;
407+
var view, ctx, called, receivedEvent, dispatchedEvent;
408408

409409
function createViewAndContext(protoView) {
410410
view = createView(protoView);
411411
ctx = view.context;
412412
called = 0;
413-
ctx.callMe = () => called += 1;
413+
receivedEvent = null;
414+
ctx.callMe = (event) => {
415+
called += 1;
416+
receivedEvent = event;
417+
}
414418
}
415419

416420
function dispatchClick(el) {
417-
DOM.dispatchEvent(el, DOM.createMouseEvent('click'));
421+
dispatchedEvent = DOM.createMouseEvent('click');
422+
DOM.dispatchEvent(el, dispatchedEvent);
418423
}
419424

420425
function createProtoView() {
421426
var pv = new ProtoView(el('<div class="ng-binding"><div></div></div>'),
422427
new ProtoRecordRange());
423428
pv.bindElement(new TestProtoElementInjector(null, 0, []));
424-
pv.bindEvent('click', parser.parseBinding('callMe()', null));
429+
pv.bindEvent('click', parser.parseBinding('callMe(\$event)', null));
425430
return pv;
426431
}
427432

@@ -431,6 +436,7 @@ export function main() {
431436
dispatchClick(view.nodes[0]);
432437

433438
expect(called).toEqual(1);
439+
expect(receivedEvent).toBe(dispatchedEvent);
434440
});
435441

436442
it('should not fire on a bubbled native events', () => {
@@ -448,6 +454,7 @@ export function main() {
448454

449455
view.dehydrate();
450456
dispatchClick(view.nodes[0]);
457+
expect(called).toEqual(0);
451458
});
452459
});
453460

0 commit comments

Comments
 (0)