summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/views/basic/basic_view.js
diff options
context:
space:
mode:
authorAaron Bohy <aab@odoo.com>2019-11-07 07:51:51 +0000
committerAaron Bohy <aab@odoo.com>2020-03-09 14:14:39 +0000
commita9cebd5acbba40ae63aa9f986d9bab39b2112f92 (patch)
tree9e12efe671c7110e6d060919bf024ba3a30ac302 /addons/web/static/src/js/views/basic/basic_view.js
parentf129dff5a0094b07bba2f8886513cded0a2d21c6 (diff)
[FIX] web: handle nested one2manys and onchange
Let's assume the following scenario in a form view: - have a one2many field, say fieldA, displayed as a list, containing another one2many, fieldB, (no widget, thus displaying 'n record(s)') - the one2many list is not editable, so there is a sub form view, displaying fieldB as a list, and in this list some random field, say fieldC, is displayed - have a random field on the main form view, with an onchange to populate the one2many - set that random field s.t. the onchange returns something like fieldA: [[5], [0, 0, {fieldB: [[0, 0, {fieldC: 'value'}]]}]] - there is now a record in fieldA's list, displaying '1 record' as value for fieldB - click on that record to open it in a form view (dialog) - in the dialog, in fieldB's list, we expect to have a single row displaying 'value' as value for fieldC Before this rev., it crashed when opening the record in the dialog, whether the form view was inline or not. The crash occured because fieldB was already in the list view, so datapoints already existed for it (a list datapoint, and record datapoints for records in the relation, in our example one record datapoint). However, those datapoints didn't have the fieldsInfo of the form view. When opening the form view, we added the fieldsInfo of the form to the datapoint of the record we opened, but we didn't recursively apply the fieldsInfo to its children. As a consequence, when rendering the list view for fieldB in the dialog, we haven't the information about fieldC, and it crashed. Note that if fieldB wasn't present in fieldA's list view, it worked fine because datapoints didn't exist before we opened the record in the dialog. Task 2120235
Diffstat (limited to 'addons/web/static/src/js/views/basic/basic_view.js')
-rw-r--r--addons/web/static/src/js/views/basic/basic_view.js80
1 files changed, 36 insertions, 44 deletions
diff --git a/addons/web/static/src/js/views/basic/basic_view.js b/addons/web/static/src/js/views/basic/basic_view.js
index 4566d0b9471..6668392b823 100644
--- a/addons/web/static/src/js/views/basic/basic_view.js
+++ b/addons/web/static/src/js/views/basic/basic_view.js
@@ -96,14 +96,15 @@ var BasicView = AbstractView.extend({
* @private
* @returns {Promise}
*/
- _loadData: function (model) {
+ _loadData: async function (model) {
if (this.recordID) {
// Add the fieldsInfo of the current view to the given recordID,
// as it will be shared between two views, and it must be able to
// handle changes on fields that are only on this view.
- model.addFieldsInfo(this.recordID, {
+ await model.addFieldsInfo(this.recordID, {
fields: this.fields,
- fieldsInfo: this.fieldsInfo,
+ fieldInfo: this.fieldsInfo[this.viewType],
+ viewType: this.viewType,
});
var record = model.get(this.recordID);
@@ -119,7 +120,7 @@ var BasicView = AbstractView.extend({
// in the form view (e.g. if field is a many2many list in the form
// view, or if it is displayed by a widget requiring specialData).
// So when this happens, F is added to the list of fieldNames to fetch.
- _.each(viewFields, function (name) {
+ _.each(viewFields, (name) => {
if (!_.contains(fieldNames, name)) {
var fieldType = record.fields[name].type;
var fieldInfo = fieldsInfo[name];
@@ -137,30 +138,28 @@ var BasicView = AbstractView.extend({
// X2Many case: field is an x2many displayed as a list or
// kanban view, but the related fields haven't been loaded yet.
if ((fieldType === 'one2many' || fieldType === 'many2many')) {
- if (!('fieldsInfo' in record.data[name])) {
+ var x2mFieldInfo = record.fieldsInfo[this.viewType][name];
+ var viewType = x2mFieldInfo.viewType || x2mFieldInfo.mode;
+ var knownFields = Object.keys(record.data[name].fieldsInfo[record.data[name].viewType] || {});
+ var newFields = Object.keys(record.data[name].fieldsInfo[viewType]);
+ if (_.difference(newFields, knownFields).length) {
fieldNames.push(name);
- } else {
+ }
+
+ if (record.data[name].viewType === 'default') {
+ // Use case: x2many (tags) in x2many list views
+ // When opening the x2many record form view, the
+ // x2many will be reloaded but it may not have
+ // the same fields (ex: tags in list and list in
+ // form) so we need to merge the fieldsInfo to
+ // avoid losing the initial fields (display_name)
var fieldViews = fieldInfo.views || fieldInfo.fieldsInfo || {};
- var fieldViewTypes = Object.keys(fieldViews);
- var recordViewTypes = Object.keys(record.data[name].fieldsInfo);
- if (_.difference(fieldViewTypes, recordViewTypes).length) {
- fieldNames.push(name);
- }
-
- if (record.data[name].viewType === 'default') {
- // Use case: x2many (tags) in x2many list views
- // When opening the x2many record form view, the
- // x2many will be reloaded but it may not have
- // the same fields (ex: tags in list and list in
- // form) so we need to merge the fieldsInfo to
- // avoid losing the initial fields (display_name)
- var defaultFieldInfo = record.data[name].fieldsInfo.default;
- _.each(fieldViews, function (fieldView) {
- _.each(fieldView.fieldsInfo, function (x2mFieldInfo) {
- _.defaults(x2mFieldInfo, defaultFieldInfo);
- });
+ var defaultFieldInfo = record.data[name].fieldsInfo.default;
+ _.each(fieldViews, function (fieldView) {
+ _.each(fieldView.fieldsInfo, function (x2mFieldInfo) {
+ _.defaults(x2mFieldInfo, defaultFieldInfo);
});
- }
+ });
}
}
// Many2one: context is not the same between the different views
@@ -176,25 +175,18 @@ var BasicView = AbstractView.extend({
var def;
if (fieldNames.length) {
- // Some fields in the new view weren't in the previous one, so
- // we might have stored changes for them (e.g. coming from
- // onchange RPCs), that we haven't been able to process earlier
- // (because those fields were unknow at that time). So we ask
- // the model to process them.
- def = model.applyRawChanges(record.id, viewType).then(function () {
- if (model.isNew(record.id)) {
- return model.applyDefaultValues(record.id, {}, {
- fieldNames: fieldNames,
- viewType: viewType,
- });
- } else {
- return model.reload(record.id, {
- fieldNames: fieldNames,
- keepChanges: true,
- viewType: viewType,
- });
- }
- });
+ if (model.isNew(record.id)) {
+ def = model.applyDefaultValues(record.id, {}, {
+ fieldNames: fieldNames,
+ viewType: viewType,
+ });
+ } else {
+ def = model.reload(record.id, {
+ fieldNames: fieldNames,
+ keepChanges: true,
+ viewType: viewType,
+ });
+ }
}
return Promise.resolve(def).then(function () {
return model.get(record.id);