(js) Simplify promises handling in User.$filter

This commit is contained in:
Francis Lachapelle 2015-07-07 16:34:58 -04:00
parent fe20cc220f
commit dd5b325864

View file

@ -41,14 +41,12 @@
* @return a promise of an array of matching User objects * @return a promise of an array of matching User objects
*/ */
User.$filter = function(search, excludedUsers) { User.$filter = function(search, excludedUsers) {
var deferred = User.$q.defer(), var param = {search: search};
param = {search: search};
if (!search) { if (!search) {
// No query specified // No query specified
User.$users = []; User.$users = [];
deferred.resolve(User.$users); return User.$q.when(User.$users);
return deferred.promise;
} }
if (angular.isUndefined(User.$users)) { if (angular.isUndefined(User.$users)) {
// First session query // First session query
@ -56,12 +54,11 @@
} }
else if (User.$query == search) { else if (User.$query == search) {
// Query hasn't changed // Query hasn't changed
deferred.resolve(User.$users); return User.$q.when(User.$users);
return deferred.promise;
} }
User.$query = search; User.$query = search;
User.$$resource.fetch(null, 'usersSearch', param).then(function(response) { return User.$$resource.fetch(null, 'usersSearch', param).then(function(response) {
var results, index, user; var results, index, user;
if (excludedUsers) { if (excludedUsers) {
// Remove excluded users from response // Remove excluded users from response
@ -93,10 +90,9 @@
User.$users.splice(index, 1); User.$users.splice(index, 1);
} }
} }
deferred.resolve(User.$users); User.$log.debug(User.$users);
}, deferred.reject); return User.$users;
});
return deferred.promise;
}; };
/** /**
@ -113,6 +109,10 @@
this.$$image = this.image || User.$gravatar(this.c_email); this.$$image = this.image || User.$gravatar(this.c_email);
}; };
User.prototype.toString = function() {
return '[User ' + this.c_email + ']';
};
/** /**
* @function $shortFormat * @function $shortFormat
* @memberof User.prototype * @memberof User.prototype