trainingowl

🦉 App 🦉

Content

Overview

Every Owl application has a root element, a set of templates, an environment and possibly a few other settings. The App class is a simple class that represents all of these elements. Here is an example:

const {Component, App } = owl;

class MyComponent extends Component { ... }

const app = new App(MyComponent, { props: {...}, templates: "..."});
app.mount(document.body);

The basic workflow is: create an App instance configured with the root component, the templates, and possibly other settings. Then, we mount that instance somewhere in the DOM.

API

Configuration

The config object is an object with some of the following keys:

mount helper

Note that there is a mount helper to do that in just a line:

const { mount, Component } = owl;

class MyComponent extends Component {
    ...
}

mount(MyComponent, document.body, { props: {...}, templates: "..."});

Here is the mount function signature:

mount(Component, target, config) with the following arguments:

Most of the time, the mount helper is more convenient, but whenever one needs a reference to the actual Owl App, then using the App class directly is possible.

Roots

An application can have multiple roots. It is sometimes useful to instantiate sub components in places that are not managed by Owl, such as an html editor with dynamic content (the Knowledge application in Odoo).

To create a root, one can use the createRoot method, which takes two arguments:

The createRoot method returns an object with a mount method (same API as the App.mount method), and a destroy method.

const root = app.createRoot(MyComponent, { props: { someProps: true } });
await root.mount(targetElement);

// later
root.destroy();

Note that, like with owl App, it is the responsibility of the code that created the root to properly destroy it (before it has been removed from the DOM!). Owl has no way of doing it itself.

Loading templates

Most applications will need to load templates whenever they start. Here is what it could look like in practice:

// in the main js file:
const { loadFile, mount } = owl;

// async, so we can use async/await
(async function setup() {
  const templates = await loadFile(`/some/endpoint/that/return/templates`);
  const env = {
    _t: someTranslateFn,
    templates,
    // possibly other stuff
  };

  mount(Root, document.body, { env });
})();

Dev mode

Dev mode activates some additional checks and developer amenities: