[FIX] web: BasicModel: onchange on X2M: handle command 6

Before this rev., when an onchange returned a command 6 for an
x2many field, it was completely ignored by the BasicModel.
This commit is contained in:
Aaron Bohy
2018-02-16 14:28:50 +01:00
parent cf03101447
commit ff019aa52f
2 changed files with 64 additions and 13 deletions
@@ -1452,22 +1452,15 @@ var BasicModel = AbstractModel.extend({
defs.push(self._applyOnChange(command[2], rec));
} else if (command[0] === 4) {
// LINK TO
rec = self.localData[list._cache[command[1]]];
if (rec) {
// modifications done on a record are discarded if
// the onchange uses a LINK TO
self.discardChanges(rec.id);
}
// the dataPoint id will be set when the record will be
// fetched (for now, this dataPoint may not exist yet)
list._changes.push({
operation: 'ADD',
id: rec ? rec.id : null,
resID: command[1],
});
linkRecord(list, command[1]);
} else if (command[0] === 5) {
// DELETE ALL
list._changes = [{operation: 'REMOVE_ALL'}];
} else if (command[0] === 6) {
list._changes = [{operation: 'REMOVE_ALL'}];
_.each(command[2], function (resID) {
linkRecord(list, resID);
});
}
});
var def = self._readUngroupedList(list).then(function () {
@@ -1484,6 +1477,25 @@ var BasicModel = AbstractModel.extend({
}
});
return $.when.apply($, defs);
// inner function that adds a record (based on its res_id) to a list
// dataPoint (used for onchanges that return commands 4 (LINK TO) or
// commands 6 (REPLACE WITH))
function linkRecord (list, resID) {
rec = self.localData[list._cache[resID]];
if (rec) {
// modifications done on a record are discarded if the onchange
// uses a LINK TO or a REPLACE WITH
self.discardChanges(rec.id);
}
// the dataPoint id will be set when the record will be fetched (for
// now, this dataPoint may not exist yet)
list._changes.push({
operation: 'ADD',
id: rec ? rec.id : null,
resID: resID,
});
}
},
/**
* When an operation is applied to a x2many field, the field widgets
@@ -2906,6 +2906,45 @@ QUnit.module('relational_fields', {
form.destroy();
});
QUnit.test('onchange returning a command 6 for an x2many', function (assert) {
assert.expect(2);
this.data.partner.onchanges = {
foo: function (obj) {
obj.turtles = [[6, false, [1, 2, 3]]];
},
};
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch:'<form>' +
'<field name="foo"/>' +
'<field name="turtles">' +
'<tree>' +
'<field name="turtle_foo"/>' +
'</tree>' +
'</field>' +
'</form>',
res_id: 1,
viewOptions: {
mode: 'edit',
},
});
assert.strictEqual(form.$('.o_data_row').length, 1,
"there should be one record in the relation");
// change the value of foo to trigger the onchange
form.$('.o_field_widget[name=foo]').val('some value').trigger('input');
assert.strictEqual(form.$('.o_data_row').length, 3,
"there should be three records in the relation");
form.destroy();
});
QUnit.test('x2many fields inside x2manys are fetched after an onchange', function (assert) {
assert.expect(6);