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) {
|
static _addNativeEventListener(element: Element, eventName: string, expr, view: View) {
|
||||||
|
var locals = MapWrapper.create();
|
||||||
DOM.on(element, eventName, (event) => {
|
DOM.on(element, eventName, (event) => {
|
||||||
if (event.target === element) {
|
if (event.target === element) {
|
||||||
// Most of the time the event will be fired only when the view is
|
// Most of the time the event will be fired only when the view is
|
||||||
// in the live document. However, in a rare circumstance the
|
// in the live document. However, in a rare circumstance the
|
||||||
// view might get dehydrated, in between the event queuing up and
|
// view might get dehydrated, in between the event queuing up and
|
||||||
// firing.
|
// firing.
|
||||||
// TODO(rado): replace with
|
if (view.hydrated()) {
|
||||||
// expr.eval(new ContextWithVariableBindings(view.context, {'$event': event}));
|
MapWrapper.set(locals, '\$event', event);
|
||||||
// when eval with variable bindinds works.
|
var context = new ContextWithVariableBindings(view.context, locals);
|
||||||
if (view.hydrated()) expr.eval(view.context);
|
expr.eval(context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -404,24 +404,29 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('event handlers', () => {
|
describe('event handlers', () => {
|
||||||
var view, ctx, called;
|
var view, ctx, called, receivedEvent, dispatchedEvent;
|
||||||
|
|
||||||
function createViewAndContext(protoView) {
|
function createViewAndContext(protoView) {
|
||||||
view = createView(protoView);
|
view = createView(protoView);
|
||||||
ctx = view.context;
|
ctx = view.context;
|
||||||
called = 0;
|
called = 0;
|
||||||
ctx.callMe = () => called += 1;
|
receivedEvent = null;
|
||||||
|
ctx.callMe = (event) => {
|
||||||
|
called += 1;
|
||||||
|
receivedEvent = event;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function dispatchClick(el) {
|
function dispatchClick(el) {
|
||||||
DOM.dispatchEvent(el, DOM.createMouseEvent('click'));
|
dispatchedEvent = DOM.createMouseEvent('click');
|
||||||
|
DOM.dispatchEvent(el, dispatchedEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createProtoView() {
|
function createProtoView() {
|
||||||
var pv = new ProtoView(el('<div class="ng-binding"><div></div></div>'),
|
var pv = new ProtoView(el('<div class="ng-binding"><div></div></div>'),
|
||||||
new ProtoRecordRange());
|
new ProtoRecordRange());
|
||||||
pv.bindElement(new TestProtoElementInjector(null, 0, []));
|
pv.bindElement(new TestProtoElementInjector(null, 0, []));
|
||||||
pv.bindEvent('click', parser.parseBinding('callMe()', null));
|
pv.bindEvent('click', parser.parseBinding('callMe(\$event)', null));
|
||||||
return pv;
|
return pv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -431,6 +436,7 @@ export function main() {
|
||||||
dispatchClick(view.nodes[0]);
|
dispatchClick(view.nodes[0]);
|
||||||
|
|
||||||
expect(called).toEqual(1);
|
expect(called).toEqual(1);
|
||||||
|
expect(receivedEvent).toBe(dispatchedEvent);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not fire on a bubbled native events', () => {
|
it('should not fire on a bubbled native events', () => {
|
||||||
|
@ -448,6 +454,7 @@ export function main() {
|
||||||
|
|
||||||
view.dehydrate();
|
view.dehydrate();
|
||||||
dispatchClick(view.nodes[0]);
|
dispatchClick(view.nodes[0]);
|
||||||
|
expect(called).toEqual(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue