From 77e071f4eb282a1f99475d85d571355ecf929457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Mon, 11 Mar 2019 17:00:42 +0100 Subject: [PATCH] define attachChild method in component --- web/static/src/ts/core/component.ts | 17 +++++++++++++++++ web/static/src/ts/ui/root.ts | 4 +--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/web/static/src/ts/core/component.ts b/web/static/src/ts/core/component.ts index 3f1732bd..d099e815 100644 --- a/web/static/src/ts/core/component.ts +++ b/web/static/src/ts/core/component.ts @@ -121,6 +121,23 @@ export class Component< // Public //-------------------------------------------------------------------------- + /** + * Attach a child widget to a given html element + * + * This is most of the time not necessary, since widgets should primarily be + * created/managed with the t-widget directive in a qweb template. However, + * for the cases where we need more control, this method will do what is + * necessary to make sure all the proper hooks are called (for example, + * mounted/willUnmount) + * + * Note that this method makes a few assumptions: + * - the child widget is indeed a child of the current widget + * - the target is inside the dom of the current widget (typically a ref) + */ + attachChild(child: Component, target: HTMLElement) { + target.appendChild(child.el!); + child.__mount(); + } async mount(target: HTMLElement): Promise { const vnode = await this._start(); if (this.__widget__.isDestroyed) { diff --git a/web/static/src/ts/ui/root.ts b/web/static/src/ts/ui/root.ts index 932da791..456a66ea 100644 --- a/web/static/src/ts/ui/root.ts +++ b/web/static/src/ts/ui/root.ts @@ -53,9 +53,7 @@ export class Root extends Widget { async applyController(controller: Controller) { const widget = await controller.create(this); if (widget) { - // to do: call some public method of widget instead... - (this.refs.content).appendChild(widget.el!); - widget.__mount(); + this.attachChild(widget, this.refs.content); widget.el!.classList.add("o_action_controller"); this.store.activateController(controller); }