diff --git a/doc/reference/component.md b/doc/reference/component.md index f212912c..00639b6e 100644 --- a/doc/reference/component.md +++ b/doc/reference/component.md @@ -567,11 +567,13 @@ A _business_ DOM event is triggered by a call to `trigger` on a component. } ``` -The call to `trigger` generates a [_CustomEvent_](https://developer.mozilla.org/docs/Web/Guide/Events/Creating_and_triggering_events) -of type `menu-loaded` and dispatches it on the component's DOM element -(`this.el`). The event bubbles and is cancelable. The parent component listening -to event `menu-loaded` will receive the payload in its `someMethod` handler -(in the `detail` property of the event), whenever the event is triggered. +The call to `trigger` generates an `OwlEvent`, a subclass of [_CustomEvent_](https://developer.mozilla.org/docs/Web/Guide/Events/Creating_and_triggering_events) +with an additional attribute `originalComponent` (the component that triggered +the event). The generated event is of type `menu-loaded` and dispatches it on +the component's DOM element (`this.el`). The event bubbles and is cancelable. +The parent component listening to event `menu-loaded` will receive the payload +in its `someMethod` handler (in the `detail` property of the event), whenever +the event is triggered. ```js class ParentComponent { diff --git a/src/component/component.ts b/src/component/component.ts index c31daaeb..4ee69634 100644 --- a/src/component/component.ts +++ b/src/component/component.ts @@ -1,4 +1,5 @@ import { Observer } from "../core/observer"; +import { OwlEvent } from "../core/owl_event"; import { CompiledTemplate, QWeb } from "../qweb/index"; import { patch, VNode } from "../vdom/index"; import "./directive"; @@ -388,7 +389,7 @@ export class Component { */ trigger(eventType: string, payload?: any) { if (this.el) { - const ev = new CustomEvent(eventType, { + const ev = new OwlEvent(this, eventType, { bubbles: true, cancelable: true, detail: payload diff --git a/src/core/owl_event.ts b/src/core/owl_event.ts new file mode 100644 index 00000000..85573152 --- /dev/null +++ b/src/core/owl_event.ts @@ -0,0 +1,15 @@ +import { Component } from "../component/component"; + +/** + * We define here OwlEvent, a subclass of CustomEvent, with an additional + * attribute: + * - originalComponent: the component that triggered the event + */ + +export class OwlEvent extends CustomEvent { + originalComponent: Component; + constructor(component, eventType, options) { + super(eventType, options); + this.originalComponent = component; + } +}