[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.
This commit is contained in:
Géry Debongnie
2017-04-11 19:44:38 +02:00
committed by Aaron Bohy
parent 83b9be825c
commit 3f4b422148
2 changed files with 38 additions and 1 deletions
@@ -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)
};
@@ -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: '<form string="Partners">' +
'<field name="foo"></field>' +
'</form>',
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);