Registries are actually ordered keys or value mappings. Registries are used to register and manage reusable components and services across the application. Odoo provides a global repository for commonly used elements, mainly to avoid duplication. Registries also ensure modularity. In Odoo 18 there are different registries for specific purposes.
The commonly used registries in Owl are component, service, and field registries.
Example for importing a registry in Odoo 18:
We can register a new service using the registry object.
Ie,
import { registry } from "@web/core/registry";
const myService = {
myMethod() {
console.log("My service!");
},
};
registry.category("services").add("my_service", myService);
Here "services" is the category, "myService" is the unique key for the service and my_service is the service to register.
Accessing the service:
const myService = registry.category("services").get("my_service");
myService.myMethod();
Extending Existing Registries:
const existingComponent = registry.category("components").get("existing_component");
existingComponent.newMethod = function () {
console.log("Extended functionality!");
};
Categories of Registries
Registries are categorized into different categories to manage different types of assets, components, or services efficiently. Here are some categories of registries.
1. Effect registry
By using the Effects registry developers can define and manage effects that are reusable and can trigger locally or globally within components.
Example:
const effectRegistry = registry.category("effects");
effectRegistry.add("my_custom_effect", myCustomEffect);
2. Formatter registry
By using registering formatters, developers can create reusable data processing functions, and the functions are easy to retrieve and apply across the application. The format has the following APIs:
format (value [, options])
Example:
const formatRegistry = registry.category("formatters");
formatRegistry.add("custom_formatter", customFormatter);
3. Main components registry
The main component registry is used to manage the core reusable UI components. And it will add higher-level components to the web client.
Example:
registry.category("main_components").add("BlockUI", BlockUiFromRegistry);
4. Service registry
Service registry is a category that is used to manage and register services. These are reusable, centralized functionalities or APIs that can be accessed throughout the application.
Example:
registry.category("services").add("myService", myService);
5. Systray registry
By using the systray registry, developers can register custom icons and actions that users can interact with from the system tray. Systray is actually located to the right of the navigation bar.
Example:
registry.category("systray").add("custom_systray_item", CustomSystray);
The Registries used in the OWL framework are actually initialized during the application's startup and can be customized through loadAssets or similar hooks in Odoo 18.
To read more about Registries in Odoo 16, refer to our blog Registries in Odoo 16.