From 8563d989f3e30165691365a6a8bb55320cb9fc80 Mon Sep 17 00:00:00 2001 From: Younn Olivier Date: Thu, 2 Sep 2021 16:54:26 +0000 Subject: [PATCH] [FIX] web_editor: reduce M2M widget rpc Every options UI is updated when one is updated. The Many2manyUserValueWidget is built in a way that every UI update will trigger 2 rpc to get the records to display to the user, even if it is not the option containing the widget that has been updated. These rpc are now cached at the widget level. This commit also fixes a bad computation of the Many2manyUserValueWidget search domain. task-2629245 X-original-commit: 847aecb4f9f4647e55e454d8c6560aa267f17c64 Part-of: odoo/odoo#77873 --- .../static/src/js/editor/snippets.options.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/addons/web_editor/static/src/js/editor/snippets.options.js b/addons/web_editor/static/src/js/editor/snippets.options.js index 04bb2dfcb8e..43997186007 100644 --- a/addons/web_editor/static/src/js/editor/snippets.options.js +++ b/addons/web_editor/static/src/js/editor/snippets.options.js @@ -1918,7 +1918,13 @@ const ListUserValueWidget = UserValueWidget.extend({ if (this.createWidget) { const selectedIds = currentValues.map(({ id }) => id) .filter(id => typeof id === 'number'); - this.createWidget.options.domain = [...this.createWidget.options.domain, ['id', 'not in', selectedIds]]; + const selectedIdsDomain = ['id', 'not in', selectedIds]; + const selectedIdsDomainIndex = this.createWidget.options.domain.findIndex(domain => domain[0] === 'id' && domain[1] === 'not in'); + if (selectedIdsDomainIndex > -1) { + this.createWidget.options.domain[selectedIdsDomainIndex] = selectedIdsDomain; + } else { + this.createWidget.options.domain = [...this.createWidget.options.domain, selectedIdsDomain]; + } this.createWidget.setValue(''); this.createWidget.inputEl.value = ''; $(this.createWidget.inputEl).trigger('input'); @@ -2384,6 +2390,7 @@ const Many2oneUserValueWidget = SelectUserValueWidget.extend({ init(parent, title, options, $target) { this.afterSearch = []; this.displayNameCache = {}; + this._rpcCache = {}; const {dataAttributes} = options; Object.assign(options, { limit: '5', @@ -2502,6 +2509,18 @@ const Many2oneUserValueWidget = SelectUserValueWidget.extend({ // Private //-------------------------------------------------------------------------- + /** + * Caches the rpc. + * + * @override + */ + async _rpc() { + const cacheId = JSON.stringify(...arguments); + if (!this._rpcCache[cacheId]) { + this._rpcCache[cacheId] = this._super(...arguments); + } + return this._rpcCache[cacheId]; + }, /** * Searches the database for corresponding records and updates the dropdown *