From eb5d65cda78802a5c4287c3fcce4ed5658491371 Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Mon, 13 Mar 2017 16:40:14 +0100 Subject: [PATCH] [REF] web: factorize the special data cache saving code The _fetchSpecialXXX methods all save values in the _specialDataCache variable associated to a given record and fieldName. This code is being regrouped in a private function now. --- .../static/src/js/views/basic/basic_model.js | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js index 7797abfc3f6..a664344ddf5 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -1421,11 +1421,13 @@ var BasicModel = AbstractModel.extend({ } // avoid rpc if not necessary - var specialDataCache = {context: context, domain: domain}; - if (_.isEqual(record._specialDataCache[fieldName], specialDataCache)) { + var hasChanged = this._saveSpecialDataCache(record, fieldName, { + context: context, + domain: domain, + }); + if (!hasChanged) { return $.when(); } - record._specialDataCache[fieldName] = specialDataCache; var self = this; return this._rpc(field.relation, 'search_read') @@ -1472,11 +1474,13 @@ var BasicModel = AbstractModel.extend({ var domain = record.getDomain({fieldName: fieldName}); // avoid rpc if not necessary - var specialDataCache = {context: context, domain: domain}; - if (_.isEqual(record._specialDataCache[fieldName], specialDataCache)) { + var hasChanged = this._saveSpecialDataCache(record, fieldName, { + context: context, + domain: domain, + }); + if (!hasChanged) { return $.when(); } - record._specialDataCache[fieldName] = specialDataCache; return this._rpc(field.relation, "name_search") .args(["", domain]) @@ -2140,6 +2144,25 @@ var BasicModel = AbstractModel.extend({ return list; }); }, + /** + * Allows to save a value in the specialData cache associated to a given + * record and fieldName. If the value in the cache was already the given + * one, nothing is done and the method indicates it by returning false + * instead of true. + * + * @private + * @param {Object} record - an element from the localData + * @param {string} fieldName - the name of the field + * @param {*} value - the cache value to save + * @returns {boolean} false if the value was already the given one + */ + _saveSpecialDataCache: function (record, fieldName, value) { + if (_.isEqual(record._specialDataCache[fieldName], value)) { + return false; + } + record._specialDataCache[fieldName] = value; + return true; + }, /** * Do a /search_read to get data for a list resource. This does a * /search_read because the data may not be static (for ex, a list view).