Skip to content

Instantly share code, notes, and snippets.

@myjpa
Last active November 26, 2020 07:18
Show Gist options
  • Save myjpa/10313774 to your computer and use it in GitHub Desktop.
Save myjpa/10313774 to your computer and use it in GitHub Desktop.
Debounced Ember TextField
# monkey patching Ember.TextField so that you can add delay=500 to make the
# TextField to update the binding value in debounced manner with delay of 500ms.
# That is it will only update the value if there is a unsubmitted change and
# no user input for the specified delay period.
# While no delay spcified, it's just the plain Ember.TextField
# usage in handlebars:
# {{view Ember.TextField valueBinding="someValue" delay=500}}
Ember.TextField.reopen {
delay: null # delayed period for updating the binding value in ms
_debouncedSet: (key,value,delay)->Ember.run.debounce(this, Ember.set, this, key, value, delay)
_elementValueDidChange: () ->
if this.get('delay')
this._debouncedSet('value', this.$().val(), this.get('delay'))
else
Ember.set(this, 'value', this.$().val()) # this is the original implementation in ember
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment