I've encountered a recurring scenario while developing Angular apps, especially when utilizing a Drupal API. I'm seeking advice on best practices for handling this specific issue.
Currently, I am working on an app that interacts with a Drupal API. The API exposes certain resources but only provides references to additional resources in some cases. To fully utilize these resources in my views, I need to make multiple calls and combine the data into one final object.
For example, when retrieving an article from the API, it also includes a list of comments with basic information. However, I need to retrieve detailed comment data by making separate calls for each comment and combining it with the article data for display.
In my view, I use ng-repeat to iterate over the comments array like so:
<div class="" ng-repeat="note in row.comments">
</div>
My goal is to append the extended comment information to each item in the comments array rather than simply adding it at the end of the array. This poses a challenge in structuring the data correctly.
Here is a snippet of my current controller code:
OnenodeFactory.query({id: $stateParams.nid, 'deep-load-refs': 1}, function(data) {
$scope.row = data;
if (data.comments.length >= 1) {
$scope.iscomment = true;
var i;
for(i = 0; i < data.comments.length; i++){
UserFactory.query({ name: $scope.row.comments[i].comment.name}, function (usr) {
$scope.row.comments.push(usr);
});
}
} else {
$scope.iscomment = false;
}
});
This implementation results in new items being added within the comments array instead of appending the data as intended.
I often encounter similar challenges where APIs provide reference data necessitating additional calls to construct comprehensive data sets. Any guidance on addressing this overarching issue would be greatly appreciated.
While I am familiar with promises and parallel loading of data, the objective here is to consolidate all data sources for seamless integration with NG-repeat. Open to suggestions and insights on alternative approaches to streamline this process.