I am having an array object of "users" that stores all the profile information of these users (such as name and email) along with their user.uid.
Another object "friends" stores for each user.uid which friends the user but with only the friend.uid.
Now when I retrieve all the friends of user.uid, I want somehow to link the profiles of this friend to the display. This obviously will result in problems with asynchronous loading.
I am using Firebase and wish if someone could give me examples on how they solved it.
I have tried the following, but it does not work (shows that the length is 0 after $loaded() even though it loaded all the users in a list:
Controller
FriendsService.getFriends(user.uid).$loaded(
function(Friends){
$scope.Friends = Friends;
// Friends is here still undefined???
angular.forEach(Friends, function(iterator){
FriendsService.getUserData(iterator.friendID).$loaded(function(output){
$scope.FriendsData.push(output)
})
})
},
function(error){
window.alert("Ooooops: " + error)
})
Services
var getFriends = function(userID) {
return fbutil.syncObject("friendsofusers/" + userID + "/friends/")
// equivalent to:
// var ref = new Firebase("https://yourapp.firebaseio.com/friendsofusers/" + userID + "/friends/");
// var sync = $firebase(ref)
// return sync.$asObject();
};
var getUserData = function(userID) {
return fbutil.syncObject("users/" + userID);
};