From 3f4b422148bba43501c42ee6a2fd9b37147f3509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Fri, 31 Mar 2017 14:33:53 +0200 Subject: [PATCH] [FIX] web: don't ask twice confirmation to discard Before this commit, the web client displayed a confirm dialog when opening the app switcher (enterprise version) if a record is dirty, then again when doing the next action. With this commit, we make sure that the second time we ask confirmation to discard changes, no dialog will be opened. --- .../src/js/views/form/form_controller.js | 6 +++- addons/web/static/test/views/form_tests.js | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/js/views/form/form_controller.js b/addons/web/static/src/js/views/form/form_controller.js index 8c429d00fa2..3b317b761cf 100644 --- a/addons/web/static/src/js/views/form/form_controller.js +++ b/addons/web/static/src/js/views/form/form_controller.js @@ -54,6 +54,7 @@ var FormController = BasicController.extend({ * can be discarded. If it is rejected, then we cannot discard. */ canBeDiscarded: function () { + var self = this; if (!this.isDirty) { return $.when(); } @@ -62,7 +63,10 @@ var FormController = BasicController.extend({ var options = { title: _t("Warning"), confirm_callback: function () { - this.on('closed', null, def.resolve.bind(def)); + this.on('closed', null, function () { + self.isDirty = false; + def.resolve(); + }); }, cancel_callback: def.reject.bind(def) }; diff --git a/addons/web/static/test/views/form_tests.js b/addons/web/static/test/views/form_tests.js index 3140310cc82..49c563fc9cc 100644 --- a/addons/web/static/test/views/form_tests.js +++ b/addons/web/static/test/views/form_tests.js @@ -1460,6 +1460,39 @@ QUnit.module('Views', { form.destroy(); }); + QUnit.test('handling dirty state: canBeDiscarded should be idempotent', function (assert) { + assert.expect(3); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '
', + res_id: 1, + }); + + // switch to edit mode + form.$buttons.find('.o_form_button_edit').click(); + assert.strictEqual(form.$('.o_form_input').val(), 'yop', 'input should contain yop'); + + // edit the foo field to make it dirty + form.$('.o_form_input').val('new value').trigger('input'); + + // discard changes once + form.canBeDiscarded(); + assert.strictEqual($('.modal').length, 1, 'a confirm modal should be displayed'); + $('.modal .modal-footer .btn-primary').click(); // click on confirm + + // discard changes a second time + form.canBeDiscarded(); + assert.strictEqual($('.modal').length, 0, 'no confirm modal should be displayed'); + + form.destroy(); + }); + + QUnit.test('restore local state when switching to another record', function (assert) { assert.expect(4);