summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/views/basic
diff options
context:
space:
mode:
authorqsm-odoo <qsm@odoo.com>2017-03-13 16:42:48 +0100
committerAaron Bohy <aab@odoo.com>2017-04-11 19:44:38 +0200
commit07c581975c7ec4fa5589969695eb7beb49d99733 (patch)
tree58c8abb707a275fb069e6c1aa846b4cf967553d6 /addons/web/static/src/js/views/basic
parenteb5d65cda78802a5c4287c3fcce4ed5658491371 (diff)
[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.
Diffstat (limited to 'addons/web/static/src/js/views/basic')
-rw-r--r--addons/web/static/src/js/views/basic/basic_model.js50
1 files changed, 50 insertions, 0 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 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
@@ -1512,6 +1512,56 @@ var BasicModel = AbstractModel.extend({
});
},
/**
+ * 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<any>}
+ * 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
*
* @param {Object} list a valid resource object