angularjs - Angular chaining requests in resolve -
i have page displaying user , locations user active in. per firebase documentation on flat structure, storing user , location in 2 different nods. want ui-router resolve user , array of locations before loads page.
below code using can't figure out how write return statement location variable wait locations retrieved. (as far can see there no option in angularfire retrieve of locations 1 call thought solution welcome.)
resolve: { user: function(firebase, $firebaseobject, $stateparams){ var user = $firebaseobject(firebase.user.child($stateparams.id)); return user.$loaded().then(function(){ return user; }) }, locations: function(firebase, $firebaseobject, user) { var locations = []; angular.foreach(user.locations, function (value, location) { var retrievedlocation = $firebaseobject(firebase.location.child(location)); retrievedlocation.$loaded().then(function(){ locations.push(retrievedlocation); }) }) } }
you'll have roll own promise $q:
function ($q, firebase, $firebaseobject, user) { var promisearray = []; angular.foreach(user.locations, function (value, location) { var retrievedlocation = $firebaseobject(firebase.location.child(location)); promisearray.push(retrievedlocation.$loaded().then(function(){ return retrievedlocation; }); }) return $q.all(promisearray).then(function(data){ var locations = data; return locations; }); }
note don't need locations variable or .then @ more. return be:
return $q.all(promisearray)
Comments
Post a Comment