[FIX] web: update all fields in onchange in one2many

Before this commit, we did not properly update all fields in a one2many.
I assume that it was because the list renderer crashed if it tried to
display a field that did not exist in the list view, but in that case,
we simply can ignore these fields.
This commit is contained in:
Géry Debongnie
2017-04-11 19:44:38 +02:00
committed by Aaron Bohy
parent cef1b273c0
commit 0f96189b7f
4 changed files with 60 additions and 6 deletions
@@ -746,9 +746,9 @@ var FieldOne2Many = FieldX2Many.extend({
if (event && event.target === this && self.view.arch.tag === 'tree' && this.editable) {
var command = event.data.changes[this.name];
if (command.operation === 'UPDATE') {
var fieldsChanged = _.keys(command.data);
// FIXME: other fields might have change with onchange
this.renderer.confirmChange(record.data[this.name], command.id, fieldsChanged);
var state = record.data[this.name];
var fieldNames = state.getFieldNames();
this.renderer.confirmChange(state, command.id, fieldNames);
return $.when();
}
}
@@ -423,13 +423,19 @@ ListRenderer.include({
* Updates the row of a particular record of the editable list view.
*
* @param {index} index the index of the record whose row needs updating
* @param {string[]} changed_fields names of the fields which changed
* @param {string[]} changedFields names of the fields which changed
* @param {OdooEvent} event the event that triggered the change
*/
_updateRow: function (index, changed_fields, event) {
_updateRow: function (index, changedFields, event) {
var self = this;
var record = this.state.data[index];
_.each(changed_fields, function (field_name) {
// filter out fields that are not present in the view. They may come
// from an onchange, but we need to ignore them.
changedFields = _.filter(changedFields, function (fieldName) {
return _.findWhere(self.columns, {name: fieldName});
});
_.each(changedFields, function (field_name) {
var widget = _.findWhere(self.widgets, {name: field_name});
if (widget && widget.__rowIndex === index) {
widget.reset(record, event);
@@ -62,6 +62,7 @@ var ListRenderer = BasicRenderer.extend({
if (c.attrs.widget === 'handle') {
self.hasHandle = true;
}
c.name = c.attrs.name;
return false;
});
this.rowDecorations = _.chain(this.arch.attrs)
@@ -92,6 +92,7 @@ QUnit.module('relational_fields', {
turtle_bar: {string: "Bar", type: "boolean", default: true},
turtle_int: {string: "int", type: "integer", sortable: true},
turtle_qux: {string: "Qux", type: "float", digits: [16,1], required: true, default: 1.5},
turtle_description: {string: "Description", type: "text"},
turtle_trululu: {string: "Trululu", type: "many2one", relation: 'partner'},
product_id: {string: "Product", type: "many2one", relation: 'product', required: true},
partner_ids: {string: "Partner", type: "many2many", relation: 'partner'},
@@ -1265,6 +1266,52 @@ QUnit.module('relational_fields', {
form.destroy();
});
QUnit.test('one2many list (editable): edition, part 4', function (assert) {
assert.expect(3);
this.data.turtle.onchanges = {
turtle_trululu: function (obj) {
if (obj.turtle_trululu) {
obj.turtle_description = "Some Description";
}
},
};
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch: '<form string="Partners">' +
'<group>' +
'<field name="turtles">' +
'<tree editable="top">' +
'<field name="turtle_trululu"/>' +
'<field name="turtle_description"/>' +
'</tree>' +
'</field>' +
'</group>' +
'</form>',
res_id: 2,
});
// edit mode, then click on Add an item
assert.strictEqual(form.$('tr.o_data_row').length, 0,
"should have 0 data rows");
form.$buttons.find('.o_form_button_edit').click();
form.$('.o_form_field_x2many_list_row_add a').click();
assert.strictEqual(form.$('textarea').val(), "",
"field turtle_description should be empty");
// add a value in the turtle_trululu field to trigger an onchange
var $dropdown = form.$('.o_form_field_many2one[name=turtle_trululu] input')
.autocomplete('widget');
form.$('.o_form_field_many2one[name=turtle_trululu] input').click();
$dropdown.find('a:contains(first record)').mouseenter().click();
assert.strictEqual(form.$('textarea').val(), "Some Description",
"field turtle_description should be set to the result of the onchange");
form.destroy();
});
QUnit.test('onchange in a one2many', function (assert) {
assert.expect(1);