[FIX] web: BasicModel: discard changes when reloading

... from a new record.

Steps to reproduce the issue:
  - go to a multi-record view (e.g. list) of basically any model
  - click on 'Create' -> it opens a form view in edit mode
  - click on 'Discard' -> it goes back to the list
  - click on an existing record -> it should open the selected
    record in readonly.
However, it didn't display the data of the selected record, but
the default data of a new record (the result of a default_get).

This was because the changes weren't properly discarded when
reloading from a new record (it rather restored the record to its
initial state, i.e. to the result of the default_get).
This commit is contained in:
Aaron Bohy
2017-05-17 08:10:49 +02:00
parent 5afe894f44
commit 0f9e986c46
2 changed files with 35 additions and 2 deletions
@@ -228,7 +228,7 @@ var BasicModel = AbstractModel.extend({
options = options || {};
var element = this.localData[id];
var isNew = this.isNew(id);
var rollback = options.rollback || isNew;
var rollback = 'rollback' in options ? options.rollback : isNew;
this._visitChildren(element, function (elem) {
if (rollback && elem._savePoint) {
if (elem._savePoint instanceof Array) {
@@ -644,7 +644,7 @@ var BasicModel = AbstractModel.extend({
return this._makeDefaultRecord(element.model, params);
}
if (!options.keepChanges) {
this.discardChanges(id);
this.discardChanges(id, {rollback: false});
}
}
@@ -1371,4 +1371,37 @@ QUnit.module('Views', {
model.destroy();
});
QUnit.test('changes are discarded when reloading from a new record', function (assert) {
// practical use case: click on 'Create' to open a form view in edit
// mode (new record), click on 'Discard', then open an existing record
assert.expect(2);
this.data.partner.fields.foo.default = 'default';
var model = createModel({
Model: BasicModel,
data: this.data,
});
// load a new record (default_get)
var params = _.extend(this.params, {
res_id: undefined,
type: 'record',
fieldNames: ['foo'],
});
model.load(params).then(function (resultID) {
var record = model.get(resultID);
assert.strictEqual(record.data.foo, 'default',
"should be the default value");
// reload with id 2
model.reload(record.id, {currentId: 2}).then(function (resultID) {
var record = model.get(resultID);
assert.strictEqual(record.data.foo, 'gnap',
"should be the value of record 2");
});
});
model.destroy();
});
});});