mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
small widget refactoring
This commit is contained in:
@@ -21,7 +21,7 @@ export class Widget<T extends WEnv> {
|
|||||||
env: T;
|
env: T;
|
||||||
el: HTMLElement | null = null;
|
el: HTMLElement | null = null;
|
||||||
state: Object = {};
|
state: Object = {};
|
||||||
refs: { [key: string]: any } = {}; // either HTMLElement or Widget
|
refs: { [key: string]: Widget<T> | HTMLElement | undefined } = {}; // either HTMLElement or Widget
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
// Lifecycle
|
// Lifecycle
|
||||||
@@ -86,9 +86,7 @@ export class Widget<T extends WEnv> {
|
|||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
async render(): Promise<VNode> {
|
async render(): Promise<VNode> {
|
||||||
const promises: Promise<void>[] = [];
|
const vnode = await this._render();
|
||||||
let vnode = this.env.qweb.render(this.name, this, { promises });
|
|
||||||
await Promise.all(promises);
|
|
||||||
if (!this.el) {
|
if (!this.el) {
|
||||||
this.el = document.createElement(vnode.sel!);
|
this.el = document.createElement(vnode.sel!);
|
||||||
}
|
}
|
||||||
@@ -97,6 +95,12 @@ export class Widget<T extends WEnv> {
|
|||||||
return vnode;
|
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) {
|
private visitSubTree(callback: (w: Widget<T>) => void) {
|
||||||
callback(this);
|
callback(this);
|
||||||
for (let child of this.children) {
|
for (let child of this.children) {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Clock } from "./clock";
|
|||||||
import { Counter } from "./counter";
|
import { Counter } from "./counter";
|
||||||
|
|
||||||
const template = `
|
const template = `
|
||||||
<div class="o_discuss" t-debug="1">
|
<div class="o_discuss">
|
||||||
<span>DISCUSS!!</span>
|
<span>DISCUSS!!</span>
|
||||||
<button t-on-click="resetCounter">Reset</button>
|
<button t-on-click="resetCounter">Reset</button>
|
||||||
<button t-on-click="resetCounterAsync">Reset in 3s</button>
|
<button t-on-click="resetCounterAsync">Reset in 3s</button>
|
||||||
@@ -26,20 +26,24 @@ export class Discuss extends Widget<Env> {
|
|||||||
widgets = { Clock, Counter };
|
widgets = { Clock, Counter };
|
||||||
state = { validcounter: true };
|
state = { validcounter: true };
|
||||||
|
|
||||||
mounted() {
|
mounted() {}
|
||||||
debugger;
|
|
||||||
}
|
|
||||||
resetCounter(ev: MouseEvent) {
|
resetCounter(ev: MouseEvent) {
|
||||||
this.refs.counter.updateState({ counter: 3 });
|
if (this.refs.counter instanceof Counter) {
|
||||||
|
this.refs.counter.updateState({ counter: 3 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resetCounterAsync(ev: MouseEvent) {
|
resetCounterAsync(ev: MouseEvent) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.refs.counter.updateState({ counter: 3 });
|
if (this.refs.counter instanceof Counter) {
|
||||||
|
this.refs.counter.updateState({ counter: 3 });
|
||||||
|
}
|
||||||
}, 3000);
|
}, 3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
toggle() {
|
toggle() {
|
||||||
this.updateState({ validcounter: !this.state.validcounter });
|
if (this.refs.counter instanceof Counter) {
|
||||||
|
this.updateState({ validcounter: !this.state.validcounter });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user