feat(events): add the $event local variable to the handler context
This commit is contained in:
parent
156f3d99e0
commit
03c21a8cb2
|
@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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('<div class="ng-binding"><div></div></div>'),
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue