From 9100e675dfe7ae60ea3c8f1a5ef1e9a78a4ab4c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sun, 2 Apr 2017 21:04:31 +0200 Subject: [FIX] web: add support for discarding lines in o2m This is not a complete work, but it introduces the basic infrastructure to properly edit various more sophisticated editing behaviours. The main focus of this commit is to handle discarding invalid lines in one2many, potentially asking the user for confirmation. So, the relevant methods have to be modified to return some deferred, because the operation is now asynchronous. --- .../web/static/src/js/views/basic/basic_model.js | 28 ++++++++++++++++++++++ 1 file changed, 28 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 0b5de486bb9..1fe0e52130b 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -544,6 +544,22 @@ var BasicModel = AbstractModel.extend({ return result.id; }); }, + /** + * In some case, we may need to remove an element from a list, without going + * through the notifyChanges machinery. The motivation for this is when the + * user click on 'Add an item' in a field one2many with a required field, + * then clicks somewhere else. The new line need to be discarded, but we + * don't want to trigger a real notifyChanges (no need for that, and also, + * we don't want to rerender the UI). + * + * @param {string} elementID some valid element id. It is necessary that the + * corresponding element has a parent. + */ + removeLine: function (elementID) { + var record = this.localData[elementID]; + var parent = this.localData[record.parentID]; + parent._changes = _.without(parent._changes, elementID); + }, /** * Save a local resource, if needed. This is a complicated operation, * - it needs to check all changes, @@ -1652,6 +1668,11 @@ var BasicModel = AbstractModel.extend({ removedIds = _.difference(list.res_ids, relIds); addedIds = _.difference(relIds, list.res_ids); keptIds = _.intersection(list.res_ids, relIds); + + // the didChange variable keep track of the fact that at + // least one id was updated + var didChange = false; + for (i = 0; i < keptIds.length; i++) { relRecord = _.findWhere(relData, {res_id: keptIds[i]}); var command; @@ -1659,11 +1680,18 @@ var BasicModel = AbstractModel.extend({ var r = this.get(relRecord.id, {raw: true}); var changes = _.pick(r.data, Object.keys(relRecord._changes)); command = x2ManyCommands.update(relRecord.res_id, changes); + didChange = true; } else { command = x2ManyCommands.link_to(keptIds[i]); } commands[fieldName].push(command); } + if (!didChange && addedIds.length === 0 && removedIds.length === 0) { + // in this situation, we have no changed ids, no added + // ids and no removed ids, so we can safely ignore the + // last changes + commands[fieldName] = null; + } for (i = 0; i < addedIds.length; i++) { relRecord = _.findWhere(relData, {res_id: addedIds[i]}); relRecord = this.get(relRecord.id, {raw: true}); -- cgit v1.2.3