mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
committed by
Géry Debongnie
parent
22e48e3be0
commit
c2ab9774fb
+11
-2
@@ -13,8 +13,17 @@ functions are all available in the `owl.utils` namespace.
|
||||
|
||||
## `whenReady`
|
||||
|
||||
The function `whenReady` is useful to register some code that need to be executed
|
||||
as soon as the document (page) is ready:
|
||||
The function `whenReady` returns a `Promise` resolved when the DOM is ready (if
|
||||
not ready yet, resolved directly otherwise). If called with a callback as
|
||||
argument, it executes it as soon as the DOM ready (or directly).
|
||||
|
||||
```js
|
||||
Promise.all([loadTemplates(), owl.utils.whenReady()]).then(function ([templates]) {
|
||||
const qweb = new owl.QWeb(templates);
|
||||
const app = new App({ qweb });
|
||||
app.mount(document.body);
|
||||
});
|
||||
```
|
||||
|
||||
```js
|
||||
owl.utils.whenReady(function() {
|
||||
|
||||
+4
-2
@@ -11,11 +11,13 @@
|
||||
*/
|
||||
|
||||
export function whenReady(fn) {
|
||||
return new Promise(function(resolve) {
|
||||
if (document.readyState !== "loading") {
|
||||
fn();
|
||||
resolve();
|
||||
} else {
|
||||
document.addEventListener("DOMContentLoaded", fn, false);
|
||||
document.addEventListener("DOMContentLoaded", resolve, false);
|
||||
}
|
||||
}).then(fn || function () {});
|
||||
}
|
||||
|
||||
const loadedScripts: { [key: string]: Promise<void> } = {};
|
||||
|
||||
+13
-13
@@ -127,22 +127,22 @@ async function makeApp(js, css, xml) {
|
||||
.map(l => (l === "" ? "" : " " + l))
|
||||
.join("\n");
|
||||
|
||||
const JS = `async function startApp() {
|
||||
// Loading templates
|
||||
let TEMPLATES;
|
||||
const JS = `
|
||||
async function loadTemplates() {
|
||||
try {
|
||||
TEMPLATES = await owl.utils.loadTemplates('app.xml');
|
||||
return owl.utils.loadTemplates('app.xml');
|
||||
} catch(e) {
|
||||
document.write(\`This app requires a static server. If you have python installed, try 'python app.py'\`);
|
||||
return;
|
||||
console.error(\`This app requires a static server. If you have python installed, try 'python app.py'\`);
|
||||
}
|
||||
}
|
||||
|
||||
function start([TEMPLATES]) {
|
||||
// Application code
|
||||
${processedJS}
|
||||
}
|
||||
|
||||
// wait for DOM ready before starting
|
||||
owl.utils.whenReady(startApp);`;
|
||||
Promise.all([loadTemplates(), owl.utils.whenReady()]).then(start);
|
||||
`;
|
||||
|
||||
zip.file("app.js", JS);
|
||||
zip.file("app.css", css);
|
||||
@@ -354,13 +354,13 @@ class TabbedEditor extends owl.Component {
|
||||
//------------------------------------------------------------------------------
|
||||
async function start() {
|
||||
document.title = `${document.title} (v${owl.__info__.version})`;
|
||||
const templates = await owl.utils.loadTemplates("templates.xml");
|
||||
const [templates] = await Promise.all([
|
||||
owl.utils.loadTemplates("templates.xml"),
|
||||
owl.utils.whenReady()
|
||||
]);
|
||||
const qweb = new owl.QWeb(templates);
|
||||
const env = { qweb };
|
||||
owl.utils.whenReady(() => {
|
||||
const app = new App(env);
|
||||
const app = new App({ qweb });
|
||||
app.mount(document.body);
|
||||
});
|
||||
}
|
||||
|
||||
start();
|
||||
|
||||
Reference in New Issue
Block a user