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);