diff options
| author | kujiu <kujiu@users.noreply.github.com> | 2017-07-25 13:43:37 +0200 |
|---|---|---|
| committer | ged-odoo <ged@odoo.com> | 2017-07-25 13:43:37 +0200 |
| commit | 2771b23a35c27165b54904a9e433202a5cd226af (patch) | |
| tree | 0f876d657f17b18f80400bb504853b3fa3f33d0f /addons/web/static/src | |
| parent | 044b8b1240e6b294d26df10fa4518b97ae4879f0 (diff) | |
[FIX] web: do not delete records for one2many with widget=many2many
Some one2many fields are called with a many2many widget to unlink
records instead of delete them. But this behavior was modified with the
new views and the records were deleted anyway.
In this commit, we restore the previous behavior, and create a FORGET
command in basic model
to do that. Note that the widget='many2many' is a hack, and a better way
to accomplish this is to use a new custom option. But this would be a
fix for master (PR #18413)
Diffstat (limited to 'addons/web/static/src')
| -rw-r--r-- | addons/web/static/src/js/fields/relational_fields.js | 8 | ||||
| -rw-r--r-- | addons/web/static/src/js/views/basic/basic_model.js | 27 |
2 files changed, 24 insertions, 11 deletions
diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 89fcb4fe406..1935e5804dd 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -838,8 +838,10 @@ var FieldX2Many = AbstractField.extend({ */ _onDeleteRecord: function (ev) { ev.stopPropagation(); + var shouldForget = this.attrs.widget === 'many2many' || this.field.type === 'many2many'; + var operation = shouldForget ? 'FORGET' : 'DELETE'; this._setValue({ - operation: 'REMOVE', + operation: operation, ids: [ev.data.id], }); }, @@ -1290,7 +1292,7 @@ var FieldMany2ManyBinaryMultiFiles = AbstractField.extend({ var record = _.findWhere(this.value.data, {res_id: fileID}); if (record) { this._setValue({ - operation: 'REMOVE', + operation: 'FORGET', ids: [record.id], }); var metadata = this.metadata[record.id]; @@ -1469,7 +1471,7 @@ var FieldMany2ManyTags = AbstractField.extend({ _removeTag: function (id) { var record = _.findWhere(this.value.data, {res_id: id}); this._setValue({ - operation: 'REMOVE', + operation: 'FORGET', ids: [record.id], }); }, 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 dfc8858664d..8f679207f00 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -1295,9 +1295,12 @@ var BasicModel = AbstractModel.extend({ defs.push(this._applyChange(command.id, command.data)); } break; - case 'REMOVE': + case 'FORGET': + // Unlink the record of list. + list._forceM2MUnlink = true; + case 'DELETE': // filter out existing operations involving the current - // dataPoint, and add a 'REMOVE' operation only if there is + // dataPoint, and add a 'DELETE' or 'FORGET' operation only if there is // no 'ADD' operation for that dataPoint, as it would mean // that the record wasn't in the relation yet var idsToRemove = command.ids; @@ -1309,7 +1312,8 @@ var BasicModel = AbstractModel.extend({ return idInCommands; }); _.each(idsToRemove, function (id) { - list._changes.push({operation: 'REMOVE', id: id}); + var operation = list._forceM2MUnlink ? 'FORGET': 'DELETE'; + list._changes.push({operation: operation, id: id}); }); break; case 'REPLACE_WITH': @@ -1333,7 +1337,7 @@ var BasicModel = AbstractModel.extend({ return self.localData[localId]; }); removedDef = this._applyX2ManyChange(record, fieldName, { - operation: 'REMOVE', + operation: 'DELETE', ids: _.map(removedIds, function (resID) { return _.findWhere(listData, {res_id: resID}).id; }), @@ -1350,7 +1354,7 @@ var BasicModel = AbstractModel.extend({ }, /** * In dataPoints of type list for x2manys, the changes are stored as a list - * of operations (being of type 'ADD', 'REMOVE', 'UPDATE' or 'REMOVE_ALL'). + * of operations (being of type 'ADD', 'DELETE', 'FORGET', UPDATE' or 'REMOVE_ALL'). * This function applies the operation of such a dataPoint without altering * the original dataPoint. It returns a copy of the dataPoint in which the * 'count', 'data' and 'res_ids' keys have been updated. @@ -1384,7 +1388,8 @@ var BasicModel = AbstractModel.extend({ list.res_ids.push(resID); } break; - case 'REMOVE': + case 'FORGET': + case 'DELETE': list.count--; list.res_ids = _.without(list.res_ids, relRecord.res_id); break; @@ -2349,7 +2354,11 @@ var BasicModel = AbstractModel.extend({ } // add delete commands for (i = 0; i < removedIds.length; i++) { - commands[fieldName].push(x2ManyCommands.delete(removedIds[i])); + if (list._forceM2MUnlink) { + commands[fieldName].push(x2ManyCommands.forget(removedIds[i])); + } else { + commands[fieldName].push(x2ManyCommands.delete(removedIds[i])); + } } } } @@ -2602,7 +2611,9 @@ var BasicModel = AbstractModel.extend({ var isValid = true; var element = this.localData[id]; _.each(element._changes, function (command) { - if (command.operation === 'REMOVE' || command.operation === 'REMOVE_ALL') { + if (command.operation === 'DELETE' || + command.operation === 'FORGET' || + command.operation === 'REMOVE_ALL') { return; } var recordData = self.get(command.id, {raw: true}).data; |
