Created
January 16, 2013 06:43
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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