logo
Odoo insights

Building Your First Owl.js Component in Odoo 17

VH CHAUDHARY

Explore Odoo servicesBook a discovery call

Building Your First Owl.js Component in Odoo 17

Understanding Owl.js

  • Reactive Framework: Owl.js is a component-based JavaScript framework developed by Odoo. It's inspired by modern frameworks like React and Vue.js. Owl components react to changes in data, updating the user interface seamlessly.
  • Component Structure: Owl components are self-contained building blocks consisting of:
    • JavaScript logic (defining behavior)
    • XML template (defining structure)

Prerequisites

  • Odoo 17 development environment setup
  • Basic understanding of HTML, JavaScript, and XML.

Step-by-Step Guide

  1. Create a Custom Module:

    • Set up a basic Odoo module with the necessary manifest files.
  2. Define the JavaScript File:

    • Create a JavaScript file (e.g., my_component.js) within the /static/src/js directory of your module.
    • Here's a simple counter component example:
    const { useState } = owl.hooks;
    const { xml } = owl.tags;
    
    class Counter extends owl.Component {
        static template = xml`
            <div class="counter">
                <button t-on-click="decrement">-</button>
                <span t-esc="state.count" />
                <button t-on-click="increment">+</button>
            </div>`;
    
        setup() {
            this.state = useState({ count: 0 });
        }
    
        decrement() {
            this.state.count--;
        }
    
        increment() {
            this.state.count++;
        }
    }
    
  3. Define the XML Template

    • Optionally, create a corresponding XML file (e.g., my_component.xml) in the /static/src/xml directory to hold your template (if not embedded in the JavaScript file as above).
  4. Load Your Component

    • Update your module's assets file (e.g., assets_backend.xml) to load the JavaScript file:
    <templates>
        <t t-name="web.assets_backend">
            <xpath expr="." position="inside">
                <script type="text/javascript" src="/your_module/static/src/js/my_component.js"/>
            </xpath>
        </t>
    </templates>
    
  5. Use the Component in a View:

    • Integrate the component into a relevant Odoo view:
     
    <div class="my-custom-container">
        <Counter/>
    </div>
    

Explanation

  • Import Statements: You import the necessary hooks (useState) and template tags (xml) from the owl library.
  • Counter Class: Defines your Owl component.
  • template: Contains the HTML structure of the component. t- attributes provide dynamic behaviors:
    • t-esc: Displays the value of state.count
    • t-on-click: Calls the increment or decrement methods.
  • setup(): Initializes the component's state using the useState hook.
  • Methods: The increment and decrement methods modify the state, causing re-rendering.

Key Points

  • State variables defined with useState trigger re-rendering when updated.
  • Odoo uses a virtual DOM for efficient updates.
  • Owl offers a rich set of features for complex components, including lifecycle hooks, props, and more.

Remember to install and restart your module to see the component in action!

Source credit

https://www.odoo.com/documentation/17.0/developer/reference/frontend/owl_components.html

https://github.com/odoo/owl/blob/master/doc/readme.md

About our Odoo practice

We implement, customize, and support Odoo for operations-heavy teams across retail, manufacturing, logistics, and services. Phased delivery, clear ownership, and support that continues after go-live.
View Odoo services