small widget refactoring

This commit is contained in:
Géry Debongnie
2019-01-25 10:25:42 +01:00
parent 2894d74db9
commit 179ad3edf4
2 changed files with 19 additions and 11 deletions
+8 -4
View File
@@ -21,7 +21,7 @@ export class Widget<T extends WEnv> {
env: T;
el: HTMLElement | null = null;
state: Object = {};
refs: { [key: string]: any } = {}; // either HTMLElement or Widget
refs: { [key: string]: Widget<T> | HTMLElement | undefined } = {}; // either HTMLElement or Widget
//--------------------------------------------------------------------------
// Lifecycle
@@ -86,9 +86,7 @@ export class Widget<T extends WEnv> {
//--------------------------------------------------------------------------
async render(): Promise<VNode> {
const promises: Promise<void>[] = [];
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<T extends WEnv> {
return vnode;
}
private async _render(): Promise<VNode> {
const promises: Promise<void>[] = [];
let vnode = this.env.qweb.render(this.name, this, { promises });
return Promise.all(promises).then(() => vnode);
}
private visitSubTree(callback: (w: Widget<T>) => void) {
callback(this);
for (let child of this.children) {
+11 -7
View File
@@ -4,7 +4,7 @@ import { Clock } from "./clock";
import { Counter } from "./counter";
const template = `
<div class="o_discuss" t-debug="1">
<div class="o_discuss">
<span>DISCUSS!!</span>
<button t-on-click="resetCounter">Reset</button>
<button t-on-click="resetCounterAsync">Reset in 3s</button>
@@ -26,20 +26,24 @@ export class Discuss extends Widget<Env> {
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 });
}
}
}