From 9369afdada7b6168be7bd48abd44f006e8182b4c Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Tue, 30 May 2017 10:20:32 +0200 Subject: [PATCH] [FIX] web: Form: crash when clicking on buttons in dialogs In view dialogs (e.g. click on the external button of a many2one in edit mode), the attribute $buttons of the form controller is undefined (the buttons are rendered by the dialog directly). The code that (de)activate the buttons to prevent concurrent clicks made the assumption that $buttons exist, so it crashed when it doesn't. --- .../src/js/views/form/form_controller.js | 8 ++- addons/web/static/tests/views/form_tests.js | 67 ++++++++++++++++++- 2 files changed, 72 insertions(+), 3 deletions(-) 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 e908036278e..1833d263082 100644 --- a/addons/web/static/src/js/views/form/form_controller.js +++ b/addons/web/static/src/js/views/form/form_controller.js @@ -74,7 +74,9 @@ var FormController = BasicController.extend({ * */ disableButtons: function () { - this.$buttons.find('button').attr('disabled', true); + if (this.$buttons) { + this.$buttons.find('button').attr('disabled', true); + } this.renderer.disableButtons(); }, /** @@ -82,7 +84,9 @@ var FormController = BasicController.extend({ * */ enableButtons: function () { - this.$buttons.find('button').removeAttr('disabled'); + if (this.$buttons) { + this.$buttons.find('button').removeAttr('disabled'); + } this.renderer.enableButtons(); }, /** diff --git a/addons/web/static/tests/views/form_tests.js b/addons/web/static/tests/views/form_tests.js index ac83de472f1..14e7c08517d 100644 --- a/addons/web/static/tests/views/form_tests.js +++ b/addons/web/static/tests/views/form_tests.js @@ -4511,7 +4511,7 @@ QUnit.module('Views', { execute_action: function (event) { return def.then(function() { event.data.on_success(); - }) + }); } }, }); @@ -4533,6 +4533,71 @@ QUnit.module('Views', { form.destroy(); }); + QUnit.test('buttons are disabled until action is resolved (in dialogs)', function (assert) { + assert.expect(3); + + var def = $.Deferred(); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '
', + archs: { + 'partner,false,form': '
' + + '' + + '
' + + '' + + '
' + + '' + + '' + + '' + + '
' + + '
', + }, + res_id: 1, + intercepts: { + execute_action: function (event) { + return def.then(function() { + event.data.on_success(); + }); + } + }, + mockRPC: function (route, args) { + if (args.method === 'get_formview_id') { + return $.when(false); + } + return this._super.apply(this, arguments); + }, + viewOptions: { + mode: 'edit', + }, + }); + + form.$('.o_external_button').click(); + + assert.notOk($('.modal .oe_button_box button').attr('disabled'), + "stat buttons should be enabled"); + + $('.modal .oe_button_box button').click(); + + assert.ok($('.modal .oe_button_box button').attr('disabled'), + "stat buttons should be disabled"); + + def.resolve(); + + assert.notOk($('.modal .oe_button_box button').attr('disabled'), + "stat buttons should be enabled"); + + form.destroy(); + }); + QUnit.test('multiple clicks on save should reload only once', function (assert) { assert.expect(4);