注册表

注册表是(有序的)键/值映射。它们是Odoo JavaScript框架提供的许多功能的主要Web客户端扩展点:每当它需要某个对象(例如字段、视图、客户端操作或服务)的定义时,它就会简单地查找注册表。然后,通过在正确的注册表中添加特定值来定制Web客户端。

import { Registry } from "@web/core/registry";

const myRegistry = new Registry();

myRegistry.add("hello", "odoo");

console.log(myRegistry.get("hello"));

registries 的一个有用特性是它们维护了一组子 registries,通过 category 方法获得。如果子 registry 还不存在,它会在运行时创建。所有 web 客户端使用的 registries 都是从一个根 registry 中获得的,该根 registry 导出在 @web/core/registry 中。

import { registry } from "@web/core/registry";

const fieldRegistry = registry.category("fields");
const serviceRegistry = registry.category("services");
const viewRegistry = registry.category("views");

注册表 API

class Registry()

创建一个新的注册表。请注意,注册表是一个事件总线,因此如果需要,可以监听“UPDATE”事件。注册表是有序的:getAll 方法返回一个按照序列号排序的值列表。

Registry.add(key, value[, options])
参数
  • key (string()) – 新条目的键

  • value (any()) – 新条目的值

  • options (Object()) – 选项

  • [options.force] (boolean()) – 如果键已经存在,则不要抛出异常

  • [options.sequence] (number()) – 序列号(有助于排序条目)

返回

注册表

在特定的键上插入一个值。如果该键已经被使用,此方法会抛出一个错误(除非选项 force 设置为 true)。选项 sequence 有助于在特定位置插入值。此方法还会触发一个 UPDATE 事件。

返回相同的注册表,因此可以链接 add 方法调用。

Registry.get(key[, defaultValue])
参数
  • key (string()) – 条目的键

  • any (defaultValue()) – 如果没有该键的条目,则返回值

返回与 key 参数对应的值。如果注册表中不包含该键,则此方法返回 defaultValue (如果已给出),否则会抛出错误。

Registry.contains(key)
参数
  • key (string()) – 条目的键

返回

布尔值

如果 key 在注册表中存在,则返回 true

Registry.getAll()
返回

any[]

返回注册表中所有元素的列表。它按照序列号排序。

Registry.remove(key)
参数
  • key (string()) – 应该被移除的条目的键

从注册表中删除一个键/值对。此操作会触发一个 UPDATE 事件。

Registry.category(subcategory)
参数
  • subcategory (string()) – 子类别的名称

返回

注册表

返回与 subcategory 相关联的子注册表。如果尚不存在,则会即时创建子注册表。

参考列表

分类

内容

effects

所有可用效果的实现

formatters

格式化值的实用函数(主要用于字段值)

main_components

顶级组件

parsers

用于解析值的实用函数(主要用于字段值)

services

应该被激活的所有服务

systray

在导航栏中显示在系统托盘区域的组件

user_menuitems

在用户菜单中显示的菜单项(位于导航栏的右上角)

特效注册表

“effects” 注册表包含所有可用效果的实现。有关更多详细信息,请参阅 effect service 部分。

格式化程序注册表

The formatters registry contains functions to format values. Each formatter has the following API:

format(value[, options])
参数
  • value (T | false()) – 一个特定类型的值,如果没有给出值则为 false

  • options (Object()) – 各种选项

返回

字符串

格式化一个值并返回一个字符串

另请参阅

主要组件注册表

主要组件注册表(main_components)对于在Web客户端中添加顶级组件非常有用。Web客户端有一个直接子组件`MainComponentsContainer`。该组件基本上是主要组件注册表中注册的组件有序列表的实时表示。

API
interface {
    Component: Owl Component class
    props?: any
}

例如,可以像这样将 LoadingIndicator 组件添加到注册表中:

registry.category("main_components").add("LoadingIndicator", {
  Component: LoadingIndicator,
});

解析器注册表

The parsers registry contains functions to parse values. Each parser has the following API:

parse(value[, options])
参数
  • value (string()) – 表示值的字符串

  • options (Object()) – 各种选项(特定于解析器)

返回

请输入有效值

解析字符串并返回一个值。如果字符串不表示有效的值,则解析器可能会失败并抛出错误。

另请参阅

服务注册表

服务注册表(类别: services )包含所有应由Odoo框架激活的 services

import { registry } from "@web/core/registry";

const myService = {
    dependencies: [...],
    start(env, deps) {
        // some code here
    }
};

registry.category("services").add("myService", myService);

系统托盘注册表

系统托盘是导航栏右侧的区域,包含各种小组件,通常显示某种类型的信息(例如未读消息数量),通知和/或让用户与它们交互。

The systray registry contains a description of these systray items, as objects with the following three keys:

  • Component: 组件类,代表该项。其根元素应为 <li> 标签,否则可能无法正确应用样式。

  • props (optional): props that should be given to the component

  • isDisplayed (optional): a function that takes the env and returns a boolean. If true, the systray item is displayed. Otherwise it is removed.

例如:

import { registry } from "@web/core/registry";

class MySystrayItem extends Component {
    // some component ...
}

registry.category("systray").add("myAddon.myItem", {
    Component: MySystrayItem,
});

系统托盘注册表是一个有序注册表(具有 sequence 数字):

const item = {
    Component: MySystrayItem
};
registry.category("systray").add("myaddon.some_description", item, { sequence: 43 });

序列号默认为50。如果提供了序列号,将用于对项目进行排序。序列号越小,在系统托盘菜单中的位置越靠右,序列号越大,在系统托盘菜单中的位置越靠左。

用户菜单注册表

用户菜单注册表(类别: user_menuitems )包含所有在打开用户菜单时显示的菜单项(位于右上角的用户名称的导航栏元素)。

用户菜单项由一个函数定义,该函数接受 env 并返回一个普通对象,包含以下信息:

  • description : the menu item text,

  • href : (可选)如果给定(并且为真),则将项目文本放在具有给定属性 href 的 a 标签中,

  • callback : callback to call when the item is selected,

  • hide:(可选)指示项目是否应该隐藏(默认值:false),

  • sequence: (optional) determines the rank of the item among the other dropwdown items (default: 100).

用户菜单每次打开时都会调用定义项目的所有函数。

例子:

import { registry } from "@web/core/registry";

registry.category("user_menuitems").add("my item", (env) => {
    return {
        description: env._t("Technical Settings"),
        callback: () => { env.services.action_manager.doAction(3); },
        hide: (Math.random() < 0.5),
    };
});