From e7dfa4af775a93b291ccd570c05970f2ac2b9187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 6 Apr 2017 09:59:05 +0200 Subject: [FIX] web: add support for editable=top/bottom in o2m Before this commit, we lost the functionality of editing records top or bottom. However, it as almost ready, but we needed to change the way commands are generated, to respect the actual order in the UI. Also, the mock server needed to be updated, to make sure he also save the new ids in the correct order. --- .../web/static/src/js/views/basic/basic_model.js | 37 ++++++++++++---------- 1 file changed, 21 insertions(+), 16 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 1f8d5ce94c6..c95325dbb87 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -1058,7 +1058,7 @@ var BasicModel = AbstractModel.extend({ return this.notifyChanges(id, command.data); })); } else { - defs.push(this.addDefaultRecord(list.id)); + defs.push(this.addDefaultRecord(list.id, {position: command.position})); } break; case 'UPDATE': @@ -1715,20 +1715,29 @@ var BasicModel = AbstractModel.extend({ var removedIds = _.difference(list.res_ids, relIds); var addedIds = _.difference(relIds, list.res_ids); var keptIds = _.intersection(list.res_ids, relIds); + // the didChange variable keeps track of the fact that at // least one id was updated var didChange = false; - var command, i, relRecord; - for (i = 0; i < keptIds.length; i++) { - relRecord = _.findWhere(relData, {res_id: keptIds[i]}); - if (!_.isEmpty(relRecord._changes)) { - var changes = this._generateChanges(relRecord); - command = x2ManyCommands.update(relRecord.res_id, changes); - didChange = true; - } else { - command = x2ManyCommands.link_to(keptIds[i]); + var command, relRecord; + for (var i = 0; i < relIds.length; i++) { + if (_.contains(keptIds, relIds[i])) { + // this is an id that already existed + relRecord = _.findWhere(relData, {res_id: relIds[i]}); + if (!_.isEmpty(relRecord._changes)) { + var changes = this._generateChanges(relRecord); + command = x2ManyCommands.update(relRecord.res_id, changes); + didChange = true; + } else { + command = x2ManyCommands.link_to(relIds[i]); + } + commands[fieldName].push(command); + } else if (_.contains(addedIds, relIds[i])) { + // this is a new id + relRecord = _.findWhere(relData, {res_id: relIds[i]}); + relRecord = this.get(relRecord.id, {raw: true}); + commands[fieldName].push(x2ManyCommands.create(_.omit(relRecord.data, 'id'))); } - commands[fieldName].push(command); } if (changesOnly && !didChange && addedIds.length === 0 && removedIds.length === 0) { // in this situation, we have no changed ids, no added @@ -1736,11 +1745,7 @@ var BasicModel = AbstractModel.extend({ // last changes commands[fieldName] = []; } - for (i = 0; i < addedIds.length; i++) { - relRecord = _.findWhere(relData, {res_id: addedIds[i]}); - relRecord = this.get(relRecord.id, {raw: true}); - commands[fieldName].push(x2ManyCommands.create(_.omit(relRecord.data, 'id'))); - } + // add delete commands for (i = 0; i < removedIds.length; i++) { commands[fieldName].push(x2ManyCommands.delete(removedIds[i])); } -- cgit v1.2.3