odoo17核心概念view2——view_service

2023-12-27 03:01

本文主要是介绍odoo17核心概念view2——view_service,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这是view系列的第二篇文章,介绍view_service.js
static\src\views\view_service.js

一、前端js

/** @odoo-module **/import { registry } from "@web/core/registry";
import { UPDATE_METHODS } from "@web/core/orm_service";export const viewService = {dependencies: ["orm"],start(env, { orm }) {let cache = {};function clearCache() {cache = {};const processedArchs = registry.category("__processed_archs__");processedArchs.content = {};processedArchs.trigger("UPDATE");}env.bus.addEventListener("CLEAR-CACHES", clearCache);env.bus.addEventListener("RPC:RESPONSE", (ev) => {const { model, method } = ev.detail.data.params;if (["ir.ui.view", "ir.filters"].includes(model)) {if (UPDATE_METHODS.includes(method)) {clearCache();}}});/*** Loads various information concerning views: fields_view for each view,* fields of the corresponding model, and optionally the filters.** @param {LoadViewsParams} params* @param {LoadViewsOptions} [options={}]* @returns {Promise<ViewDescriptions>}*/async function loadViews(params, options = {}) {const { context, resModel, views } = params;const loadViewsOptions = {action_id: options.actionId || false,load_filters: options.loadIrFilters || false,toolbar: (!context?.disable_toolbar && options.loadActionMenus) || false,};for (const key in options) {if (!["actionId", "loadIrFilters", "loadActionMenus"].includes(key)) {loadViewsOptions[key] = options[key];}}if (env.isSmall) {loadViewsOptions.mobile = true;}const filteredContext = Object.fromEntries(Object.entries(context || {}).filter(([k, v]) => k == "lang" || k.endsWith("_view_ref")));const key = JSON.stringify([resModel, views, filteredContext, loadViewsOptions]);if (!cache[key]) {debuggercache[key] = orm.call(resModel, "get_views", [], {context: filteredContext,views,options: loadViewsOptions,}).then((result) => {const { models, views } = result;const viewDescriptions = {fields: models[resModel],relatedModels: models,views: {},};for (const viewType in views) {const { arch, toolbar, id, filters, custom_view_id } = views[viewType];const viewDescription = { arch, id, custom_view_id };if (toolbar) {viewDescription.actionMenus = toolbar;}if (filters) {viewDescription.irFilters = filters;}viewDescriptions.views[viewType] = viewDescription;}return viewDescriptions;}).catch((error) => {delete cache[key];return Promise.reject(error);});}return cache[key];}return { loadViews };},
};registry.category("services").add("view", viewService);

1 、服务开头提供了一个清理缓存的函数clearCache, 将注册表中的registry.category(“processed_archs”)清空,并且触发它的update事件。
2、后面就是最重要的loadViews函数了,加载有关视图的各种信息:每个视图的字段视图,对应模型的字段,以及可选的过滤器。
输入参数params: 包含了上下文信息,模型,视图 ,举个例子:

context:{params:{action:201,cids:1,menu_id:124,model:'crax_demo.crax.demo',view_type:'list'}}
resModel:crax_demo.crax.demo
views:[[false,'list'],[false,'form'],[false,'search']]

输入参数option:是一个字典

actionid
loadActionMenus:true
LoadIrFilters:true

这一段代码是对传入的参数进行结构赋值,并且组装传入的option到loadViewsOptions ,同时组装filteredContext

  const { context, resModel, views } = params;const loadViewsOptions = {action_id: options.actionId || false,load_filters: options.loadIrFilters || false,toolbar: (!context?.disable_toolbar && options.loadActionMenus) || false,};for (const key in options) {if (!["actionId", "loadIrFilters", "loadActionMenus"].includes(key)) {loadViewsOptions[key] = options[key];}}if (env.isSmall) {loadViewsOptions.mobile = true;}const filteredContext = Object.fromEntries(Object.entries(context || {}).filter(([k, v]) => k == "lang" || k.endsWith("_view_ref")));

然后将这些变量转换成字符串

const key = JSON.stringify([resModel, views, filteredContext, loadViewsOptions]);

判断缓存中是否有这个key,如果有,则从缓存中返回,否则调用orm从后端获取

cache[key] = orm.call(resModel, "get_views", [], {context: filteredContext,views,options: loadViewsOptions,}).then((result) => {const { models, views } = result;const viewDescriptions = {fields: models[resModel],relatedModels: models,views: {},};for (const viewType in views) {const { arch, toolbar, id, filters, custom_view_id } = views[viewType];const viewDescription = { arch, id, custom_view_id };if (toolbar) {viewDescription.actionMenus = toolbar;}if (filters) {viewDescription.irFilters = filters;}viewDescriptions.views[viewType] = viewDescription;}return viewDescriptions;}).catch((error) => {delete cache[key];return Promise.reject(error);});

orm是一个promise,返回值直接付给了缓存, 后面统一返回缓存中的值。
call, then ,catch 这是promise标准的写法。

.call(resModel, "get_views", [], {context: filteredContext,views,options: loadViewsOptions,})

任何一个模型都有一个get_views方法,这是继承自它的父类 base,具体的实现是在base模块的ir_ui_view中
odoo\addons\base\models\ir_ui_view.py

@api.modeldef get_views(self, views, options=None):""" Returns the fields_views of given views, along with the fields ofthe current model, and optionally its filters for the given action.The return of the method can only depend on the requested view types,access rights (views or other records), view access rules, options,context lang and TYPE_view_ref (other context values cannot be used).Python expressions contained in views or representing domains (onpython fields) will be evaluated by the client with all the contextvalues as well as the record values it has.:param views: list of [view_id, view_type]:param dict options: a dict optional boolean flags, set to enable:``toolbar``includes contextual actions when loading fields_views``load_filters``returns the model's filters``action_id``id of the action to get the filters, otherwise loads the globalfilters or the model:return: dictionary with fields_views, fields and optionally filters"""省略100行。。。。return result

views: views:[[false,‘list’],[false,‘form’],[false,‘search’]]
options: actionid 等

返回值:
views : 各种类型view的id 和arch 以及其他信息
models: 模型的字段信息

.then((result) => {const { models, views } = result;const viewDescriptions = {fields: models[resModel],relatedModels: models,views: {},};for (const viewType in views) {const { arch, toolbar, id, filters, custom_view_id } = views[viewType];const viewDescription = { arch, id, custom_view_id };if (toolbar) {viewDescription.actionMenus = toolbar;}if (filters) {viewDescription.irFilters = filters;}viewDescriptions.views[viewType] = viewDescription;}return viewDescriptions;

这一段是根据orm的返回值 组装viewDescriptions 字段,并返回,这个字段包含了:

                    fields: models[resModel],relatedModels: models,views: {},

这里有个关键点 relatedModels, 通过debug可以看到,这是一个模型的所有的字段信息。是视图的显示中,它起到了驱动的作用。 关于这一点,后面再细聊。
后面的for循环是为了填充views这个字段,对于每种类型的视图,都包含了arch,id,custom_view_id等字段。

总结一波:
view_service有两个功能:
1、 清理缓存
2、从缓存中获取视图信息,如果没有则调用orm服务调用model的get_views方法从后端读取, 然后将返回的信息封装成一个对象返回,这个对象包含了

模型的字段信息
模型信息(包括模型的所有字段以及字段的相关信息),relatedModels 在前端是一个非常重要的概念
视图信息(各种类型视图的id,xml结构等)

关于menuService的介绍就告一段落。

这篇关于odoo17核心概念view2——view_service的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/541695

相关文章

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

Spring Boot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)

《SpringBoot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)》本文将以一个实际案例(用户管理系统)为例,详细解析SpringBoot中Co... 目录引言:为什么学习Spring Boot分层架构?第一部分:Spring Boot的整体架构1.1

Java Instrumentation从概念到基本用法详解

《JavaInstrumentation从概念到基本用法详解》JavaInstrumentation是java.lang.instrument包提供的API,允许开发者在类被JVM加载时对其进行修改... 目录一、什么是 Java Instrumentation主要用途二、核心概念1. Java Agent

Python异常处理之避免try-except滥用的3个核心原则

《Python异常处理之避免try-except滥用的3个核心原则》在Python开发中,异常处理是保证程序健壮性的关键机制,本文结合真实案例与Python核心机制,提炼出避免异常滥用的三大原则,有需... 目录一、精准打击:只捕获可预见的异常类型1.1 通用异常捕获的陷阱1.2 精准捕获的实践方案1.3

深入浅出Java中的Happens-Before核心规则

《深入浅出Java中的Happens-Before核心规则》本文解析Java内存模型中的Happens-Before原则,解释其定义、核心规则及实际应用,帮助理解多线程可见性与有序性问题,掌握并发编程... 目录前言一、Happens-Before是什么?为什么需要它?1.1 从一个问题说起1.2 Haht

Kotlin 协程之Channel的概念和基本使用详解

《Kotlin协程之Channel的概念和基本使用详解》文章介绍协程在复杂场景中使用Channel进行数据传递与控制,涵盖创建参数、缓冲策略、操作方式及异常处理,适用于持续数据流、多协程协作等,需注... 目录前言launch / async 适合的场景Channel 的概念和基本使用概念Channel 的

redis-sentinel基础概念及部署流程

《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包... 目录一. 引言二. 核心功能三. 核心组件四. 故障转移流程五. 服务部署六. sentinel部署

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

Python进阶之列表推导式的10个核心技巧

《Python进阶之列表推导式的10个核心技巧》在Python编程中,列表推导式(ListComprehension)是提升代码效率的瑞士军刀,本文将通过真实场景案例,揭示列表推导式的进阶用法,希望对... 目录一、基础语法重构:理解推导式的底层逻辑二、嵌套循环:破解多维数据处理难题三、条件表达式:实现分支

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?