refactors Discourse.debouncePromise

This commit is contained in:
Wojciech Zawistowski
2013-10-20 10:38:26 +02:00
parent aef4227073
commit 4c4a2f220e
2 changed files with 14 additions and 16 deletions

View File

@@ -47,25 +47,20 @@ Discourse.debounce = function(func, wait) {
@param {Number} wait how long to wait
**/
Discourse.debouncePromise = function(func, wait) {
var timeout = null;
var args = null;
var context = null;
var self, args, promise;
var later = function() {
func.apply(self, args).then(function (funcResult) {
promise.resolve(funcResult);
});
};
return function() {
context = this;
var promise = Ember.Deferred.create();
self = this;
args = arguments;
promise = Ember.Deferred.create();
if (!timeout) {
timeout = Em.run.later(function () {
timeout = null;
func.apply(context, args).then(function (y) {
promise.resolve(y);
});
}, wait);
}
Ember.run.debounce(null, later, wait);
return promise;
};
};