2019-05-11 12:11:00 +02:00
|
|
|
# 🦉 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` is useful to register some code that need to be executed
|
|
|
|
|
as soon as the document (page) is ready:
|
|
|
|
|
|
|
|
|
|
```js
|
2019-06-11 08:58:37 +02:00
|
|
|
owl.utils.whenReady(function() {
|
|
|
|
|
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
|
|
|
|
|
a promise, so the caller can properly react when it is ready. Also, it is smart:
|
|
|
|
|
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 {
|
|
|
|
|
willStart() {
|
|
|
|
|
return owl.utils.loadJS('/static/libs/someLib.js');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
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() {
|
|
|
|
|
const templates = await owl.utils.loadTemplates('templates.xml');
|
|
|
|
|
const qweb = new owl.QWeb(templates);
|
|
|
|
|
return { qweb };
|
|
|
|
|
}
|
|
|
|
|
```
|
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`
|