[FIX] web: one2many: add new record with 'Save & New'

Consider the following scenario involving a one2many list (non
editable) inside a form view:
 - click on 'Add an item', a model opens,
 - fill the fields and click on 'Save & Close',
 - reopen the freshly created record,
 - maybe make some changes (this is optional, but makes sense),
 - click on 'Save & New',
 - fill the fields and click on 'Save & Close',
 -> the second created record isn't added to the list.

The problem comes from the fact that when the dialog is opened for
the second time (in this case by clicking on a record), the 'save'
handler registers an 'UPDATE' command. While this is correct for
the 'Save & New' click (as we update an existing record), this
isn't for the second record which doesn't exist yet, and thus
requires an 'ADD' command.

This rev. ensures that the two cases are handled in the 'save'
handler given to the dialog.

OPW 1829723
This commit is contained in:
Aaron Bohy
2018-04-12 14:37:35 +02:00
parent 9353fa1ede
commit 9c1c2c55c2
2 changed files with 67 additions and 6 deletions
@@ -1291,12 +1291,27 @@ var FieldOne2Many = FieldX2Many.extend({
// we don't want interference with the components upstream.
ev.stopPropagation();
var self = this;
var id = ev.data.id;
// trigger an empty 'UPDATE' operation when the user clicks on 'Save' in
// the dialog, to notify the main record that a subrecord of this
// relational field has changed (those changes will be already stored on
// that subrecord, thanks to the 'Save').
var onSaved = this._setValue.bind(this, { operation: 'UPDATE', id: id }, {});
var onSaved = function (record, hasChanged) {
if (!hasChanged) {
return;
}
if (_.some(self.value.data, {id: record.id})) {
// the record already exists in the relation, so trigger an
// empty 'UPDATE' operation when the user clicks on 'Save' in
// the dialog, to notify the main record that a subrecord of
// this relational field has changed (those changes will be
// already stored on that subrecord, thanks to the 'Save').
self._setValue({ operation: 'UPDATE', id: record.id });
} else {
// the record isn't in the relation yet, so add it ; this can
// happen if the user clicks on 'Save & New' in the dialog (the
// opened record will be updated, and other records will be
// created)
self._setValue({ operation: 'ADD', id: record.id });
}
};
this._openFormDialog({
id: id,
on_saved: onSaved,
@@ -4239,7 +4239,6 @@ QUnit.module('relational_fields', {
form.destroy();
});
QUnit.test('one2many list (non editable): edition', function (assert) {
assert.expect(12);
@@ -8736,6 +8735,53 @@ QUnit.module('relational_fields', {
form.destroy();
});
QUnit.test('add a line, edit it and "Save & New"', function (assert) {
assert.expect(5);
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch: '<form string="Partners">' +
'<field name="p">' +
'<tree><field name="display_name"/></tree>' +
'<form><field name="display_name"/></form>' +
'</field>' +
'</form>',
});
assert.strictEqual(form.$('.o_data_row').length, 0,
"there should be no record in the relation");
// add a new record
form.$('.o_field_x2many_list_row_add a').click();
$('.modal .o_field_widget').val('new record').trigger('input');
$('.modal .modal-footer .btn-primary:first').click(); // Save & Close
assert.strictEqual(form.$('.o_data_row .o_data_cell').text(), 'new record',
"should display the new record");
// reopen freshly added record and edit it
form.$('.o_data_row .o_data_cell').click();
$('.modal .o_field_widget').val('new record edited').trigger('input');
// save it, and choose to directly create another record
$('.modal .modal-footer .btn-primary:nth(1)').click(); // Save & New
assert.strictEqual($('.modal').length, 1,
"the model should still be open");
assert.strictEqual($('.modal .o_field_widget').text(), '',
"should have cleared the input");
$('.modal .o_field_widget').val('another new record').trigger('input');
$('.modal .modal-footer .btn-primary:first').click(); // Save & Close
assert.strictEqual(form.$('.o_data_row .o_data_cell').text(),
'new record editedanother new record', "should display the two records");
form.destroy();
});
QUnit.module('FieldMany2Many');
QUnit.test('many2many kanban: edition', function (assert) {