[FIX] web: properly handles dirty data in form views
Before this commit, we only consider the BasicModel point of view to determine if a record is dirty. It is good when we want to determine if it is actually necessary to save the data, but not correct when we want to know if some changes can be discarded. With this commit, the form controller keeps track of its 'dirty' state, coming from user interactions. It uses this flag to determine if changes can be discarded.
This commit is contained in:
committed by
Aaron Bohy
parent
4d12ec87bc
commit
1a0702f1e2
@@ -33,6 +33,7 @@ return AbstractController.extend(FieldManagerMixin, {
|
||||
this.hasButtons = params.hasButtons;
|
||||
FieldManagerMixin.init.call(this, this.model);
|
||||
this.handle = params.initialState.id;
|
||||
this.isDirty = false;
|
||||
},
|
||||
/**
|
||||
* @override
|
||||
@@ -148,6 +149,18 @@ return AbstractController.extend(FieldManagerMixin, {
|
||||
// Handlers
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* We need to keep manually track of the isDirty property. Note that it is
|
||||
* not the same as the one from the basic model. A new record from the
|
||||
* basic model is considered dirty, but from the controller point of view,
|
||||
* there was no user interaction and should not be dirty.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_onFieldChanged: function () {
|
||||
this.isDirty = true;
|
||||
FieldManagerMixin._onFieldChanged.apply(this, arguments);
|
||||
},
|
||||
/**
|
||||
* When a reload event triggers up, we need to reload the full view.
|
||||
* For example, after a form view dialog saved some data.
|
||||
|
||||
@@ -267,7 +267,7 @@ var FormController = BasicController.extend({
|
||||
* can be discarded. If it has been rejected, then we cannot discard.
|
||||
*/
|
||||
_canBeDiscarded: function () {
|
||||
if (!this.model.isDirty(this.handle)) {
|
||||
if (!this.isDirty) {
|
||||
return $.when();
|
||||
}
|
||||
var message = _t("The record has been modified, your changes will be discarded. Are you sure you want to leave this page ?");
|
||||
@@ -452,6 +452,7 @@ var FormController = BasicController.extend({
|
||||
var self = this;
|
||||
this.model.duplicateRecord(this.handle)
|
||||
.then(function (handle) {
|
||||
self.isDirty = false;
|
||||
self.handle = handle;
|
||||
self._updateEnv();
|
||||
self._toEditMode();
|
||||
|
||||
@@ -1228,6 +1228,58 @@ QUnit.module('Views', {
|
||||
form.destroy();
|
||||
});
|
||||
|
||||
QUnit.test('discard changes on a new (non dirty, except for defaults) form view', function (assert) {
|
||||
assert.expect(2);
|
||||
|
||||
this.data.partner.fields.foo.default = "ABC";
|
||||
|
||||
var form = createView({
|
||||
View: FormView,
|
||||
model: 'partner',
|
||||
data: this.data,
|
||||
arch: '<form string="Partners"><field name="foo"></field></form>',
|
||||
});
|
||||
|
||||
// switch to edit mode and edit the foo field
|
||||
form.$buttons.find('.o_form_button_edit').click();
|
||||
assert.strictEqual(form.$('.o_form_input').val(), 'ABC', 'input should contain ABC');
|
||||
|
||||
form.$buttons.find('.o_form_button_cancel').click();
|
||||
|
||||
assert.strictEqual($('.modal').length, 0,
|
||||
'there should not be a confirm modal');
|
||||
|
||||
form.destroy();
|
||||
});
|
||||
|
||||
QUnit.test('discard changes on a duplicated record', function (assert) {
|
||||
assert.expect(2);
|
||||
|
||||
var form = createView({
|
||||
View: FormView,
|
||||
model: 'partner',
|
||||
data: this.data,
|
||||
arch: '<form string="Partners"><field name="foo"></field></form>',
|
||||
res_id: 1,
|
||||
viewOptions: {sidebar: true},
|
||||
});
|
||||
|
||||
form.$buttons.find('.o_form_button_edit').click();
|
||||
form.$('input').val("tralala").trigger('input');
|
||||
form.$buttons.find('.o_form_button_save').click();
|
||||
|
||||
form.sidebar.$('a:contains(Duplicate)').click();
|
||||
|
||||
assert.strictEqual(form.$('.o_form_input').val(), 'tralala', 'input should contain ABC');
|
||||
|
||||
form.$buttons.find('.o_form_button_cancel').click();
|
||||
|
||||
assert.strictEqual($('.modal').length, 0,
|
||||
'there should not be a confirm modal');
|
||||
|
||||
form.destroy();
|
||||
});
|
||||
|
||||
QUnit.test('switching to another record from a dirty one', function (assert) {
|
||||
assert.expect(11);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user