Created
August 5, 2014 22:48
-
-
Save BenHall/a311811265a1a996f8d7 to your computer and use it in GitHub Desktop.
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
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