Created
August 19, 2013 02:46
-
-
Save jhdavids8/6265395 to your computer and use it in GitHub Desktop.
Angular service that fetches and stores the users the current, logged-in user is following
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
angular.module("PathgatherApp").factory 'CurrentFollowedUsers', (["CurrentUser", "$q", (CurrentUser, $q) -> | |
followed_users = null | |
followed_users_promise = null | |
{ | |
getFollowedUsers: () -> | |
return CurrentUser.getUser().then (current_user) -> | |
if followed_users? | |
deferred = $q.defer() | |
deferred.resolve(followed_users) | |
return deferred.promise | |
else if followed_users_promise? | |
return followed_users_promise | |
else | |
# This is the first request for this data | |
followed_users_promise = current_user.getList('following') | |
followed_users_promise.then (response) -> | |
followed_users = response | |
return followed_users_promise | |
followingUser: (user) -> | |
return this.getFollowedUsers().then -> | |
_.findWhere(followed_users, {id: user.id}) | |
} | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment