mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] component: can trigger event handlers even if unmounted
From the beginning, Owl was designed to only call event handler on components that are mounted in the DOM. The main benefit is that if a component is destroyed, we are guaranteed to not execute any useless (or potentially dangerous code). However, there is one downside: if a component tree is being mounted, and a child component trigger an event in its mounted hook, then it cannot be caught by the parent, since the parent is technically not yet mounted. This may not be a good situation, but the point is that Owl unnecessarily prevent the handler to be called. We can fix this issue by simply checking if the component is not destroyed instead of checking that it is mounted. closes #809
This commit is contained in:
@@ -163,7 +163,7 @@ describe("basic widget properties", () => {
|
||||
expect(fixture.innerHTML).toBe("<div><span></span></div>");
|
||||
});
|
||||
|
||||
test("cannot be clicked on and updated if not in DOM", async () => {
|
||||
test("can be clicked on and updated if not in DOM", async () => {
|
||||
class Counter extends Component {
|
||||
static template = xml`
|
||||
<div><t t-esc="state.counter"/><button t-on-click="state.counter++">Inc</button></div>`;
|
||||
@@ -178,8 +178,8 @@ describe("basic widget properties", () => {
|
||||
const button = (<HTMLElement>counter.el).getElementsByTagName("button")[0];
|
||||
button.click();
|
||||
await nextTick();
|
||||
expect(target.innerHTML).toBe("<div>0<button>Inc</button></div>");
|
||||
expect(counter.state.counter).toBe(0);
|
||||
expect(target.innerHTML).toBe("<div>1<button>Inc</button></div>");
|
||||
expect(counter.state.counter).toBe(1);
|
||||
});
|
||||
|
||||
test("widget style and classname", async () => {
|
||||
@@ -2292,7 +2292,27 @@ describe("other directives with t-component", () => {
|
||||
parent.unmount();
|
||||
expect(child.__owl__.isMounted).toBe(false);
|
||||
el.click();
|
||||
expect(steps).toEqual(["click"]);
|
||||
expect(steps).toEqual(["click", "click"]);
|
||||
});
|
||||
|
||||
test("triggering custom event on mounted components", async () => {
|
||||
let value = false;
|
||||
class Child extends Component {
|
||||
static template = xml`<div/>`;
|
||||
mounted() {
|
||||
this.trigger("coucou");
|
||||
}
|
||||
}
|
||||
class Parent extends Component {
|
||||
static template = xml`<Child t-on-coucou="doSomething"/>`;
|
||||
static components = { Child };
|
||||
doSomething() {
|
||||
value = true;
|
||||
}
|
||||
}
|
||||
const parent = new Parent();
|
||||
await parent.mount(fixture);
|
||||
expect(value).toBe(true);
|
||||
});
|
||||
|
||||
test("t-on with .capture modifier", async () => {
|
||||
|
||||
Reference in New Issue
Block a user