From 07c581975c7ec4fa5589969695eb7beb49d99733 Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Mon, 13 Mar 2017 16:42:48 +0100 Subject: [REF] web: use `specialData` system in domain field The domain field needs two pieces of information that traditional fields do not have without extra RPCs and computation: the value of another field in the view to know the model to use and the number of records matched in this model with the current field value. While the first one can be retrieved synchronously by the field widget, the second one required an extra RPC during the rendering phase. The `_fetchSpecialData` system introduced in the `BasicModel` implementation allows to stop doing this extra RPC during rendering. As the RPC requires the model value to be done, the information is also fetched by the `_fetchSpecialData` code and given directly to the widget. This also removes the utility of the `resetOnAnyFieldChange` option used by this field widget. --- .../web/static/src/js/views/basic/basic_model.js | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'addons/web/static/src/js/views/basic/basic_model.js') 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 a664344ddf5..333a74cd238 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -1511,6 +1511,56 @@ var BasicModel = AbstractModel.extend({ return m2os; }); }, + /** + * Fetches the number of records associated to the domain the value of the + * given field represents. + * + * @param {Object} record - an element from the localData + * @param {Object} fieldName - the name of the field + * @returns {Deferred} + * The deferred is resolved with the fetched special data. If this + * data is the same as the previously fetched one (for the given + * parameters), no RPC is done and the deferred is resolved with + * the undefined value. + */ + _fetchSpecialDomain: function (record, fieldName) { + var context = record.getContext({fieldName: fieldName}); + + var domainModel = record.fieldAttrs[fieldName].options.model; + if (record.data.hasOwnProperty(domainModel)) { + domainModel = record._changes && record._changes[domainModel] || record.data[domainModel]; + } + var domainValue = record._changes && record._changes[fieldName] || record.data[fieldName]; + + // avoid rpc if not necessary + var hasChanged = this._saveSpecialDataCache(record, fieldName, { + context: context, + domainModel: domainModel, + domainValue: domainValue, + }); + if (!hasChanged) { + return $.when(); + } + + var def = $.Deferred(); + + this._rpc(domainModel, "search_count") + .args([domainValue]) + .withContext(context) + .exec() + .then(_.identity, function (error, e) { + e.preventDefault(); // prevent traceback (the search_count might be intended to break) + return false; + }) + .always(function (nbRecords) { + def.resolve({ + model: domainModel, + nbRecords: nbRecords, + }); + }); + + return def; + }, /** * Fetch all data in a ungrouped list * -- cgit v1.2.3