summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/views/basic/basic_model.js
diff options
context:
space:
mode:
authorAlexandre Kühn <aku@odoo.com>2018-04-04 10:09:33 +0200
committerAlexandre Kühn <aku@odoo.com>2018-04-05 17:14:57 +0200
commit9a9da5763b38a3bce54e9c4870af0032e38cacc3 (patch)
tree4743e82dc1c9c3ec545ee213a66b8eeaa90153b6 /addons/web/static/src/js/views/basic/basic_model.js
parentf8ade1c96d00c5350a10dead129a36d713e2b9a0 (diff)
[FIX] web: no record abandon on discard from default_get/onchange
Before this commit, when some records were created from an `onchange`, we have the following weird behaviour: Suppose that this record has some required fields that are empty. When we click on this record, then somewhere else, the record disappears from the list. This is because the records contains invalid fields, and it is wrongfully assuming that the record to be discarded comes from "Add an item". A previous commit [1] fixed a bit this issue with `default_get`, but since it relied on a fragile heuristic, we decide to apply the correct fix with this commit. [1] https://github.com/odoo/odoo/commit/b09f0c99be2b13d33986414485556d87da186d65
Diffstat (limited to 'addons/web/static/src/js/views/basic/basic_model.js')
-rw-r--r--addons/web/static/src/js/views/basic/basic_model.js29
1 files changed, 14 insertions, 15 deletions
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 d7d91a65405..979f8ae3fec 100644
--- a/addons/web/static/src/js/views/basic/basic_model.js
+++ b/addons/web/static/src/js/views/basic/basic_model.js
@@ -569,10 +569,15 @@ var BasicModel = AbstractModel.extend({
return _t("New");
},
/**
- * Returns true if a record can be abandoned from a list datapoint.
+ * Returns true if a record can be abandoned.
*
- * A record cannot be abandonned if it has been registered as "added"
- * in the parent's savepoint, otherwise it can be abandonned.
+ * A record can be abandonned if it is a new record, except if
+ * this datapoint has been specifically tagged as "do not abandon".
+ *
+ * Example:
+ *
+ * - Discard record from "Add an item" => "New" record => abandon
+ * - Discard record from `default_get`/`onchange` => do not abandon
*
* This is useful when discarding changes on this record, as it means that
* we must keep the record even if some fields are invalids (e.g. required
@@ -582,15 +587,7 @@ var BasicModel = AbstractModel.extend({
* @returns {boolean}
*/
canBeAbandoned: function (id) {
- var data = this.localData[id];
- var parent = this.localData[data.parentID];
- var abandonable = true;
- if (parent) {
- abandonable = !_.some(parent._savePoint, function (entry) {
- return entry.operation === 'ADD' && entry.id === id;
- });
- }
- return abandonable;
+ return !this.localData[id]._noAbandon && this.isNew(id);
},
/**
* Returns true if a record is dirty. A record is considered dirty if it has
@@ -951,11 +948,11 @@ var BasicModel = AbstractModel.extend({
* @returns {Deferred}
* Resolved with the list of field names (whose value has been modified)
*/
- save: function (record_id, options) {
+ save: function (recordID, options) {
var self = this;
return this.mutex.exec(function () {
options = options || {};
- var record = self.localData[record_id];
+ var record = self.localData[recordID];
if (options.savePoint) {
self._visitChildren(record, function (rec) {
var newValue = rec._changes || rec.data;
@@ -973,7 +970,7 @@ var BasicModel = AbstractModel.extend({
});
}
var shouldReload = 'reload' in options ? options.reload : true;
- var method = self.isNew(record_id) ? 'create' : 'write';
+ var method = self.isNew(recordID) ? 'create' : 'write';
if (record._changes) {
// id never changes, and should not be written
delete record._changes.id;
@@ -1538,6 +1535,7 @@ var BasicModel = AbstractModel.extend({
list._cache[rec.res_id] = rec.id;
}
+ rec._noAbandon = true;
list._changes.push({operation: 'ADD', id: rec.id});
if (command[0] === 1) {
list._changes.push({operation: 'UPDATE', id: rec.id});
@@ -3574,6 +3572,7 @@ var BasicModel = AbstractModel.extend({
parentID: x2manyList.id,
viewType: viewType,
});
+ r._noAbandon = true;
x2manyList._changes.push({operation: 'ADD', id: r.id});
x2manyList._cache[r.res_id] = r.id;