From 4ee39282cad72b40c0059f90684cb0e204206b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 18 Apr 2019 10:15:50 +0200 Subject: [PATCH] [IMP] component: add 'set' method to allow extending state closes #49 --- src/component.ts | 4 ++++ tests/component.test.ts | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/component.ts b/src/component.ts index 39d6b526..f13d7f72 100644 --- a/src/component.ts +++ b/src/component.ts @@ -321,6 +321,10 @@ export class Component< return shouldUpdate ? this._updateProps(nextProps) : Promise.resolve(); } + set(target: any, key: string | number, value: any) { + this.__owl__.observer!.set(target, key, value); + } + //-------------------------------------------------------------------------- // Private //-------------------------------------------------------------------------- diff --git a/tests/component.test.ts b/tests/component.test.ts index 837a4db8..17af6946 100644 --- a/tests/component.test.ts +++ b/tests/component.test.ts @@ -1449,4 +1449,19 @@ describe("widget and observable state", () => { ); } }); + + test("widget can add observed keys to its state", async () => { + class TestWidget extends Widget { + state: any = { a: 1 }; + inlineTemplate = `
`; + } + const widget = new TestWidget(env); + await widget.mount(fixture); + + expect(fixture.innerHTML).toBe("
1
"); + + widget.set(widget.state, "b", 2); + await nextTick(); + expect(fixture.innerHTML).toBe("
12
"); + }); });