From 9c1c2c55c23655c56dcd04c3d80da59b5bcab177 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Thu, 12 Apr 2018 09:02:59 +0200 Subject: [PATCH] [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 --- .../static/src/js/fields/relational_fields.js | 25 ++++++++-- .../tests/fields/relational_fields_tests.js | 48 ++++++++++++++++++- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index c4984b69441..bc44d6b659a 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -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, diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index 9816a7f69ff..0db8c26a36e 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -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: '
' + + '' + + '' + + '' + + '' + + '', + }); + + 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) {