diff options
| author | Géry Debongnie <ged@odoo.com> | 2017-05-03 15:18:08 +0200 |
|---|---|---|
| committer | Géry Debongnie <ged@odoo.com> | 2017-05-04 09:32:24 +0200 |
| commit | 91d1fd870123e9a56d64e3482e2ead1536230ab7 (patch) | |
| tree | 563147ae00b5aaba3dc080bde43ac7ad8559ad2a /addons/web/static/src | |
| parent | 7b6c474c203cf2a9c4f8ed7512a944c87cfdea85 (diff) | |
[FIX] web: prevent double onchange in some cases
When we unselect a line in a one2many list, we ask for all fields to
commit their changes (because many fields are debounced, so we may not
know their current last value). This is good, except that it could
trigger an extra field change event, which may in turn trigger a second
onchange.
We solve this issue by improving the _setValue method and delegating the
comparison to a new _isSameValue method.
It is particularly relevant for field types such as integer, which are not
stored as a string.
Diffstat (limited to 'addons/web/static/src')
| -rw-r--r-- | addons/web/static/src/js/fields/abstract_field.js | 19 | ||||
| -rw-r--r-- | addons/web/static/src/js/fields/basic_fields.js | 18 |
2 files changed, 32 insertions, 5 deletions
diff --git a/addons/web/static/src/js/fields/abstract_field.js b/addons/web/static/src/js/fields/abstract_field.js index 757e917dd36..9f977d41392 100644 --- a/addons/web/static/src/js/fields/abstract_field.js +++ b/addons/web/static/src/js/fields/abstract_field.js @@ -260,6 +260,20 @@ var AbstractField = Widget.extend({ return field_utils.format[this.field.type](value, this.field, options); }, /** + * This method check if a value is the same as the current value of the + * field. For example, a fieldDate widget might want to use the moment + * specific value isSame instead of ===. + * + * This method is used by the _setValue method. + * + * @private + * @param {any} value + * @returns {boolean} + */ + _isSameValue: function (value) { + return this.value === value; + }, + /** * convert a string representation to a valid value, depending on the field * type. * @@ -330,7 +344,7 @@ var AbstractField = Widget.extend({ _setValue: function (value) { // we try to avoid doing useless work, if the value given has not // changed. Note that we compare the unparsed values. - if (this.lastSetValue === value || (value !== false && value === this.value)) { + if (this.lastSetValue === value) { return; } this.lastSetValue = value; @@ -341,6 +355,9 @@ var AbstractField = Widget.extend({ this._isValid = false; return; } + if (this._isSameValue(value)) { + return; + } var changes = {}; changes[this.name] = value; this.trigger_up('field_changed', { diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index d1569797731..dd87d893cc0 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -264,7 +264,7 @@ var FieldDate = InputField.extend({ if (this.mode === 'edit') { this.datewidget = this._makeDatePicker(); this.datewidget.on('datetime_changed', this, function () { - var value = this._getDateWidgetValue(); + var value = this._getValue(); if ((!value && this.value) || (value && !value.isSame(this.value))) { this._setValue(value); } @@ -295,10 +295,20 @@ var FieldDate = InputField.extend({ * * @private */ - _getDateWidgetValue: function () { + _getValue: function () { return this.datewidget.get_value(); }, - + /** + * @override + * @private + * @param {Moment|false} value + */ + _isSameValue: function (value) { + if (value === false) { + return this.value === false; + } + return value.isSame(this.value); + }, /** * Instantiates a new DateWidget datepicker. * @@ -343,7 +353,7 @@ var FieldDateTime = FieldDate.extend({ * * @private */ - _getDateWidgetValue: function () { + _getValue: function () { var value = this.datewidget.get_value(); return value && value.add(-this.getSession().tzOffset, 'minutes'); }, |
