From f68462198e4e7917ce32b16db7874d697ef442af Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Mon, 3 Apr 2017 16:38:11 +0200 Subject: [REF] web: don't debounce `DebouncedField._onInput` directly The `DebouncedField` class has a `_onInput` method which is called when the user is typing text and which is debounced so that its action (setting the field's value) is not recalled too quickly. The problem is that if we have to do other actions which should not wait for the debouncing (or wait for another debouncing) when the user is typing text, this would imply to namespace the "input" event to add another handler, which is not very elegant. Instead, this commit changes the `DebouncedField` so that the `_onInput` handler is not debounced but instead calls a private method which is debounced (`_doDebouncedAction`). --- addons/web/static/src/js/fields/abstract_field.js | 8 +++++--- addons/web/static/src/js/fields/basic_fields.js | 20 +++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) (limited to 'addons/web/static/src/js/fields') diff --git a/addons/web/static/src/js/fields/abstract_field.js b/addons/web/static/src/js/fields/abstract_field.js index 79583894fdc..93d7c8418e2 100644 --- a/addons/web/static/src/js/fields/abstract_field.js +++ b/addons/web/static/src/js/fields/abstract_field.js @@ -39,9 +39,11 @@ var AbstractField = Widget.extend({ events: { 'keydown': '_onKeydown', }, - // for field widgets that may have a large number of field changes quickly, - // it could be a good idea to debounce the changes. In that case, this is - // the suggested value. For example, see InputField. + /** + * for field widgets that may have a large number of field changes quickly, + * it could be a good idea to debounce the changes. In that case, this is + * the suggested value. For example, @see DebouncedField. + */ DEBOUNCE: 1000, /** * if this flag is set to true, the rest of the web client will assume that diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index 1b96d585ac7..e6e5e84577e 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -45,7 +45,7 @@ var DebouncedField = AbstractField.extend({ this._super.apply(this, arguments); if (this.DEBOUNCE && this.mode === 'edit') { - this._onInput = _.debounce(this._onInput.bind(this), this.DEBOUNCE); + this._doDebouncedAction = _.debounce(this._doDebouncedAction.bind(this), this.DEBOUNCE); } }, @@ -63,6 +63,15 @@ var DebouncedField = AbstractField.extend({ */ _getValue: function () { }, + /** + * Notifies the outside world of the new value (checked from the DOM). + * This method is debounced so that onchanges are not triggered too quickly. + * + * @private + */ + _doDebouncedAction: function () { + this._setValue(this._getValue()); + }, //-------------------------------------------------------------------------- // Handlers @@ -70,7 +79,7 @@ var DebouncedField = AbstractField.extend({ /** * We immediately notify the outside world when this field confirms its - * changes + * changes. * * @private */ @@ -78,13 +87,14 @@ var DebouncedField = AbstractField.extend({ this._setValue(this._getValue()); }, /** - * We notify the outside world for each change in this field. It does not - * necessarily mean that onchanges will be triggered instantly. + * Called when the user is typing text -> By default this only calls a + * debounced method to notify the outside world of the changes. + * @see _doDebouncedAction * * @private */ _onInput: function () { - this._setValue(this._getValue()); + this._doDebouncedAction(); }, }); -- cgit v1.2.3