Skip to content

Instantly share code, notes, and snippets.

@BenHall
Created August 5, 2014 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenHall/a311811265a1a996f8d7 to your computer and use it in GitHub Desktop.
Save BenHall/a311811265a1a996f8d7 to your computer and use it in GitHub Desktop.
getNestedDataBetter: function (){
//create your deferred promise.
var deferred = $q.defer();
//do your thing.
$http.get('parents.json')
.then(function(result){
var parents = result.data;
$http.get('children.json')
.then(function(result) {
var children = result.data;
angular.forEach(parents, function(parent) {
parent.children = [];
angular.forEach(children, function(child) {
if(parent.childIds.indexOf(child.id) >= 0) {
parent.children.push(child);
}
});
});
//at whatever point in your code, you feel your
// code has loaded all necessary data and/or
// resolve your promise.
deferred.resolve(parents);
});
});
//return your promise to the user.
return deferred.promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment