[IMP] web: rejected click handlers should not close the dialog

Prior to this commit, there was no clean way to have a button
triggering asynchronous work (like asking user for confirmaton
for example) and close or not close the modal according to the
user choice. Your options were:

1. Swallow the pill and just accept that the modal will close
   even if the user chose not to undertake the action.
2. Set the `close` parameter of the button to false and handle
   the conditional closing by yourself from outside the dialog.

This commit introduces a way to tell the form view dialog that
the action has not been processed correctly and that the modal
should not be closed by returning a rejected promise. Prior
behavior can still be obtained (close no matter the result)
by having the handler not returning a promise at all.

closes odoo/odoo#33837

Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
David Monjoie
2019-12-17 07:21:39 +00:00
committed by Aaron Bohy
parent b0d2cd0041
commit bce6aa351b
2 changed files with 45 additions and 1 deletions
+1 -1
View File
@@ -304,7 +304,7 @@ var Dialog = Widget.extend({
}
if (buttonData.close) {
self.onForceClose = false;
Promise.resolve(def).then(self.close.bind(self)).guardedCatch(self.close.bind(self));
Promise.resolve(def).then(self.close.bind(self));
}
});
if (self.technical) {
@@ -547,6 +547,50 @@ QUnit.module('Views', {
form.destroy();
});
QUnit.test('formviewdialog is not closed when button handlers return a rejected promise', async function (assert) {
assert.expect(3);
this.data.partner.fields.poney_ids = { string: "Poneys", type: "one2many", relation: 'partner' };
this.data.partner.records[0].poney_ids = [];
var reject = true;
var parent = createParent({
data: this.data,
archs: {
'partner,false,form':
'<form string="Partner">' +
'<field name="poney_ids"><tree><field name="display_name"/></tree></field>' +
'</form>',
},
});
new dialogs.FormViewDialog(parent, {
res_model: 'partner',
res_id: 1,
buttons: [{
text: 'Click me !',
classes: "btn-secondary o_form_button_magic",
close: true,
click: function () {
return reject ? Promise.reject() : Promise.resolve();
},
}],
}).open();
await testUtils.nextTick();
assert.strictEqual($('.modal').length, 1, "should have a modal displayed");
await testUtils.dom.click($('.modal .o_form_button_magic'));
assert.strictEqual($('.modal').length, 1, "modal should still be opened");
reject = false;
await testUtils.dom.click($('.modal .o_form_button_magic'));
assert.strictEqual($('.modal').length, 0, "modal should be closed");
parent.destroy();
});
});
});