From 86af036ea1175f4a9a5fbca806ab49665fdbc4b0 Mon Sep 17 00:00:00 2001 From: fwi-odoo Date: Fri, 14 Apr 2017 13:02:21 +0200 Subject: [FIX] web: fix various bugs when fetching x2manys in form views - When an one2many is displayed in a list view, the field data are not fully loaded since only the number of record is displayed. Then when the related form view is opened the one2many fields displayed need to be loaded. - Optimized the fetch of x2manys without doing an useless rpc if we don't need all the fields but only the relation records ids and adapt the tests for this optimisation. - Fix wrong inheritance for kanbanmany2many_tags --- .../web/static/src/js/views/basic/basic_model.js | 76 +++++++++++++--------- 1 file changed, 46 insertions(+), 30 deletions(-) (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 b3777a05efb..4b39025b35d 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -1619,40 +1619,50 @@ var BasicModel = AbstractModel.extend({ if (!ids.length || fieldInfo.__no_fetch) { return $.when(); } - + var def; + var fieldNames = _.keys(fieldInfo.relatedFields); // step 2: fetch data from server - return this._rpc({ + // if we want specific fields + // if not we return an array of objects with the id + // to avoid fetching all the relation fields and an useless rpc + if (fieldNames.length) { + def = this._rpc({ model: field.relation, method: 'read', - args: [ids, _.keys(fieldInfo.relatedFields)], + args: [ids, fieldNames], context: {}, // FIXME - }) - .then(function (results) { - // step 3: assign values to correct datapoints - var dataPoints = _.map(results, function (result) { - return self._makeDataPoint({ - modelName: field.relation, - data: result, - fields: fields, - fieldsInfo: fieldsInfo, - viewType: viewType, - }); + }); + } else { + def = $.when(_.map(ids, function (id) { + return {id:id}; + })); + } + return def.then(function (results) { + // step 3: assign values to correct datapoints + var dataPoints = _.map(results, function (result) { + return self._makeDataPoint({ + modelName: field.relation, + data: result, + fields: fields, + fieldsInfo: fieldsInfo, + viewType: viewType, }); + }); - _.each(list.data, function (dataPoint) { - var record = self.localData[dataPoint]; - var m2mList = self.localData[record.data[fieldName]]; + _.each(list.data, function (dataPoint) { + var record = self.localData[dataPoint]; + var m2mList = self.localData[record.data[fieldName]]; - m2mList.data = []; - _.each(m2mList.res_ids, function (res_id) { - var dataPoint = _.find(dataPoints, function (d) { - return d.res_id === res_id; - }); - m2mList.data.push(dataPoint.id); - m2mList.count++; + m2mList.data = []; + _.each(m2mList.res_ids, function (res_id) { + var dataPoint = _.find(dataPoints, function (d) { + return d.res_id === res_id; }); + m2mList.data.push(dataPoint.id); + m2mList.count++; }); }); + }); }, /** * batch request for x2ms for datapoint of type list @@ -2444,12 +2454,18 @@ var BasicModel = AbstractModel.extend({ } } if (missingIds.length) { - def = this._rpc({ - model: list.model, - method: 'read', - args: [missingIds, fieldNames], - context: {} // FIXME - }); + if (fieldNames.length) { + def = this._rpc({ + model: list.model, + method: 'read', + args: [missingIds, fieldNames], + context: {} // FIXME + }); + } else { + def = $.when(_.map(missingIds, function (id) { + return {id:id}; + })); + } } else { def = $.when(); } -- cgit v1.2.3