From d0f321d6cc198045ff796865d94ddd15bd6d0b99 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Wed, 5 Apr 2017 11:32:56 +0200 Subject: [PATCH] [FIX] web: Form: don't discard onchange when editing a o2m Assume that there is an onchange on a form view that upates the records of a one2many field. Before this rev., if the user edited one of those one2many records (in a dialog) after the onchange, the onchange values were lost. --- .../static/src/js/views/basic/basic_model.js | 6 +- .../static/src/js/views/basic/basic_view.js | 1 + addons/web/static/test/views/form_tests.js | 62 +++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js index 47d5cee5c9c..c71ac81e0d9 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -504,6 +504,8 @@ var BasicModel = AbstractModel.extend({ * * @param {string} id local id for a resource * @param {Object} [options] + * @param {boolean} [options.keepChanges=false] if true, doesn't discard the + * changes on the record before reloading it * @returns {Deferred -> string} resolves to the id of the resource */ reload: function (id, options) { @@ -520,7 +522,9 @@ var BasicModel = AbstractModel.extend({ }; return this._makeDefaultRecord(element.model, params); } - this.discardChanges(id); + if (!options.keepChanges) { + this.discardChanges(id); + } } if (options.context !== undefined) { diff --git a/addons/web/static/src/js/views/basic/basic_view.js b/addons/web/static/src/js/views/basic/basic_view.js index 46ecb5d7e38..d850b2b7a2f 100644 --- a/addons/web/static/src/js/views/basic/basic_view.js +++ b/addons/web/static/src/js/views/basic/basic_view.js @@ -66,6 +66,7 @@ var BasicView = AbstractView.extend({ if (fieldNames.length && !this.model.isNew(record.id)) { return this.model.reload(this.recordID, { fieldNames: fieldNames, + keepChanges: true, viewType: viewType, }); } else { diff --git a/addons/web/static/test/views/form_tests.js b/addons/web/static/test/views/form_tests.js index a202ba93995..145060cf97f 100644 --- a/addons/web/static/test/views/form_tests.js +++ b/addons/web/static/test/views/form_tests.js @@ -2341,6 +2341,68 @@ QUnit.module('Views', { form.destroy(); }); + QUnit.test('onchange value are not discarded on o2m edition', function (assert) { + assert.expect(3); + + this.data.partner.records[1].p = [4]; + this.data.partner.onchanges = { + foo: function () {}, + }; + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
' + + '', + res_id: 2, + mockRPC: function (route, args) { + if (args.method === 'onchange') { + return $.when({ + value: { + p: [[5], [1, 4, {foo: 'foo changed'}]], + }, + }); + } + if (args.method === 'write') { + assert.deepEqual(args.args[1].p, [[1, 4, { + foo: 'foo changed', + }]], "should only write value of known fields"); + } + return this._super.apply(this, arguments); + }, + }); + + form.$buttons.find('.o_form_button_edit').click(); + + assert.strictEqual(form.$('.o_data_row td:first').text(), 'My little Foo Value', + "onchange should have been correctly applied on field in o2m list"); + + form.$('.o_form_input:first').val('trigger an onchange').trigger('input'); + + assert.strictEqual(form.$('.o_data_row td:first').text(), 'foo changed', + "onchange should have been correctly applied on field in o2m list"); + + form.$('.o_data_row').click(); // edit the o2m in the dialog + assert.strictEqual($('.modal .o_form_field').val(), 'foo changed', + "the onchange value hasn't been discarded when opening the o2m"); + + form.destroy(); + }); + QUnit.test('navigation with tab key in form view', function (assert) { assert.expect(2);