Enable Dark Mode!
javascript-framework-services-in-odoo-17.jpg
By: Cybrosys Technologies

JavaScript Framework- Services in Odoo 17

Technical Odoo 17

In the dynamic realm of web development, where efficiency and modularity are the keys to success, Odoo emerges as a game-changer with its unique approach – the use of powerful "services." These services, akin to behind-the-scenes wizards, provide a structured and modular foundation for developing web applications within the Odoo framework. In this blog, we'll embark on a journey to unravel the magic of Odoo services. These long-lived code entities serve as the backbone of Odoo applications, offering features and functionalities that simplify complex tasks. From user communication to data fetching and seamless navigation, Odoo services act as the unsung heroes that elevate your web development experience.
Join us as we explore real-world examples, breaking down the complexities of Odoo services into simple, actionable insights. By the end of this exploration, you'll not only understand the significance of Odoo services but also be equipped with the knowledge to harness their power in your web development endeavours. Let's dive into the world of Odoo services and unlock the true potential they hold for crafting efficient, maintainable, and scalable web applications.

import { registry } from "@web/core/registry";
const myService = {
    dependencies: ["notification"],
    start(env, { notification }) {
        let counter = 1;
        setInterval(() => {
            notification.add(`Tick Tock ${counter++}`);
        }, 5000);
    }
};
registry.category("services").add("myService", myService);

The provided instance demonstrates the registration of a basic service designed to showcase a notification every 5 seconds. It employs a service named "myService," configured with a dependency on "notification." Upon initiation, the service utilizes a timer to trigger the display of a notification with the message "Tick Tock" appended by an incrementing counter. This service is then added to the "services" category in the registry under the name "myService." During startup, the web client initiates all services listed in the services registry, emphasizing that the registry name aligns with the service name.

Defining a service

Defining a service requires adherence to the following interface:
Dependencies:
An optional list of strings representing the dependencies (other services) required by this service.

start(env, deps):

Arguments
env (Environment()): The application environment.
deps (Object()): All requested dependencies.
Returns
The value of the service or a Promise resolving to the value of the service.
This serves as the primary definition for a service, allowing it to return either a direct value or a Promise. If the latter, the service loader patiently awaits the resolution of the Promise, and the resulting value becomes the service's output.

Async:

An optional value, which, if provided, should be either true or a list of strings.
Certain services may necessitate an asynchronous API. For instance, the RPC service operates asynchronously, or the ORM service provides a series of functions for communicating with the Odoo server.
In such cases, components utilizing a service might be terminated before an asynchronous function call concludes. Typically, it is crucial to disregard such calls to prevent potential risks when the underlying component is no longer active. The async flag serves as a mechanism to signal to the service creator that all asynchronous calls from components should be kept pending if the component is destroyed.

Service Utilization

When employing a service that relies on other services and correctly declares its dependencies, the service conveniently acquires references to the corresponding services in the second argument of the start method. The recommended approach for utilizing a service in a component is through the use Service hook. It returns a reference to the service value, enabling subsequent usage by the component. For instance:

import { useService } from "@web/core/utils/hooks";
class MyComponent extends Component {
  setup() {
    const rpc = useService("rpc");
    onWillStart(async () => {
      this.someValue = await rpc(...);
    });
  }
}

JavaScript Framework- Services in Odoo 17-cybrosysJavaScript Framework- Services in Odoo 17-cybrosysJavaScript Framework- Services in Odoo 17-cybrosysJavaScript Framework- Services in Odoo 17-cybrosys

In this blog, we have explored the concept of services in Odoo 17, which play a crucial role in providing a structured and modular foundation for developing web applications within the Odoo framework. Services act as behind-the-scenes wizards, simplifying complex tasks and enhancing the overall web development experience.



If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message