From 179ad3edf4ca7cdba536c182e947b1f13e727dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Fri, 25 Jan 2019 10:25:42 +0100 Subject: [PATCH] small widget refactoring --- web/static/src/ts/core/Widget.ts | 12 ++++++++---- web/static/src/ts/widgets/Discuss.ts | 18 +++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/web/static/src/ts/core/Widget.ts b/web/static/src/ts/core/Widget.ts index 602df3d5..5c12040e 100644 --- a/web/static/src/ts/core/Widget.ts +++ b/web/static/src/ts/core/Widget.ts @@ -21,7 +21,7 @@ export class Widget { env: T; el: HTMLElement | null = null; state: Object = {}; - refs: { [key: string]: any } = {}; // either HTMLElement or Widget + refs: { [key: string]: Widget | HTMLElement | undefined } = {}; // either HTMLElement or Widget //-------------------------------------------------------------------------- // Lifecycle @@ -86,9 +86,7 @@ export class Widget { //-------------------------------------------------------------------------- async render(): Promise { - const promises: Promise[] = []; - let vnode = this.env.qweb.render(this.name, this, { promises }); - await Promise.all(promises); + const vnode = await this._render(); if (!this.el) { this.el = document.createElement(vnode.sel!); } @@ -97,6 +95,12 @@ export class Widget { return vnode; } + private async _render(): Promise { + const promises: Promise[] = []; + let vnode = this.env.qweb.render(this.name, this, { promises }); + return Promise.all(promises).then(() => vnode); + } + private visitSubTree(callback: (w: Widget) => void) { callback(this); for (let child of this.children) { diff --git a/web/static/src/ts/widgets/Discuss.ts b/web/static/src/ts/widgets/Discuss.ts index ea70004c..a729295f 100644 --- a/web/static/src/ts/widgets/Discuss.ts +++ b/web/static/src/ts/widgets/Discuss.ts @@ -4,7 +4,7 @@ import { Clock } from "./clock"; import { Counter } from "./counter"; const template = ` -
+
DISCUSS!! @@ -26,20 +26,24 @@ export class Discuss extends Widget { widgets = { Clock, Counter }; state = { validcounter: true }; - mounted() { - debugger; - } + mounted() {} resetCounter(ev: MouseEvent) { - this.refs.counter.updateState({ counter: 3 }); + if (this.refs.counter instanceof Counter) { + this.refs.counter.updateState({ counter: 3 }); + } } resetCounterAsync(ev: MouseEvent) { setTimeout(() => { - this.refs.counter.updateState({ counter: 3 }); + if (this.refs.counter instanceof Counter) { + this.refs.counter.updateState({ counter: 3 }); + } }, 3000); } toggle() { - this.updateState({ validcounter: !this.state.validcounter }); + if (this.refs.counter instanceof Counter) { + this.updateState({ validcounter: !this.state.validcounter }); + } } }