Files
owl/doc/utils.md
T

66 lines
1.6 KiB
Markdown
Raw Normal View History

# 🦉 Utils 🦉
2019-05-10 20:50:14 +02:00
Owl export a few useful utility functions, to help with common issues. Those
functions are all available in the `owl.utils` namespace.
## Content
2019-06-11 08:58:37 +02:00
- [`whenReady`](#whenready): executing code when DOM is ready
- [`loadJS`](#loadjs): loading script files
- [`loadTemplates`](#loadtemplates): loading xml files
- [`escape`](#escape): sanitizing strings
- [`debounce`](#debounce): limiting rate of function calls
2019-05-10 20:50:14 +02:00
## `whenReady`
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
2019-06-14 15:24:04 +02:00
Promise.all([loadTemplates(), owl.utils.whenReady()]).then(function([
templates
]) {
const qweb = new owl.QWeb(templates);
const app = new App({ qweb });
app.mount(document.body);
});
```
2019-05-10 20:50:14 +02:00
```js
2019-06-11 08:58:37 +02:00
owl.utils.whenReady(function() {
2019-06-14 15:24:04 +02:00
const qweb = new owl.QWeb();
const app = new App({ qweb });
app.mount(document.body);
2019-05-10 20:50:14 +02:00
});
```
2019-06-11 08:58:37 +02:00
## `loadJS`
2019-05-10 20:50:14 +02:00
2019-06-11 08:58:37 +02:00
`loadJS` takes a url (string) for a javascript resource, and loads it. It returns
2019-06-14 15:24:04 +02:00
a promise, so the caller can properly react when it is ready. Also, it is smart:
2019-06-11 08:58:37 +02:00
it maintains a list of urls previously loaded (or currently being loaded), and
prevent doing twice the work.
2019-05-10 20:50:14 +02:00
2019-06-11 08:58:37 +02:00
```js
class MyComponent extends owl.Component {
2019-06-14 15:24:04 +02:00
willStart() {
return owl.utils.loadJS("/static/libs/someLib.js");
}
2019-06-11 08:58:37 +02:00
}
```
2019-05-10 20:50:14 +02:00
2019-06-11 08:58:37 +02:00
## `loadTemplates`
2019-05-10 20:50:14 +02:00
2019-06-11 08:58:37 +02:00
```js
async function makeEnv() {
2019-06-14 15:24:04 +02:00
const templates = await owl.utils.loadTemplates("templates.xml");
const qweb = new owl.QWeb(templates);
return { qweb };
2019-06-11 08:58:37 +02:00
}
```
2019-05-10 20:50:14 +02:00
2019-06-11 08:58:37 +02:00
## `escape`
2019-05-10 20:50:14 +02:00
2019-06-11 08:58:37 +02:00
## `debounce`