Skip to content

Instantly share code, notes, and snippets.

@monde
Created January 16, 2013 06:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monde/4545136 to your computer and use it in GitHub Desktop.
Save monde/4545136 to your computer and use it in GitHub Desktop.
An Ember.js TextField and that has been extended to react to a change event after a certain amount of time has passed without any other events having occurred within the TextField.
App.DelayedTextField = Ember.TextField.extend({
// FIXME
// double check that init in Ember.TextSupport / Ember.TextSupport
// isn't handling this.on("paste"...) and the internal variable is still
// this._elementValueDidChange
init: function() {
this.on("propertychange", this, this._elementValueDidChange);
this.on("keyup", this, this._elementValueDidChange);
this.on("input", this, this._elementValueDidChange);
this.on("paste", this, this._elementValueDidChange);
this.on("change", this, this._elementValueDidChange);
this._super();
this.set("_previousValue", this.get('value'));
},
delay: (function() {
var timer = 0;
return function(callback, ms) {
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})(),
valueDidChange: function() {
var that = this;
this.delay(function() {
if (that.get('value') !== that.get('_previousValue')) {
that.nowDoSomething();
}
that.set('_previousValue', that.get('value'));
}, 800 );
}.observes('value'),
nowDoSomething: function () {
// Now, Do Something
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment