I recently coded a lot in AngularJS. After some time, he began to feel comfortable with him, and also became really productive. But, unfortunately, there is one thing that I do not understand:
As part of my project, I need to get data through the $ http.get and RESTful API server. Here I started to stumble first. After fulfilling the promise ($ q.defer, etc. Etc.) In the functions that process the data needed to continue, I thought I defeated the problem.
But in this code:
$scope.getObservationsByLocations = function() {
var promise = $q.defer();
var locationCount = 0;
angular.forEach($scope.analysisData, function(loc) {
$http.get($scope.api + 'Device?_format=json', {
params: {
location: loc.location.id
}
}).then(function (resultDevices) {
var data = angular.fromJson(resultDevices);
promise.resolve(data);
angular.forEach(angular.fromJson(resultDevices).data.entry.map(function (dev) {
http.get($scope.api + 'Observation?_format=json', {
params: {
device: dev.resource.id
}
}).then(function (resultObservations) {
var observations = angular.fromJson(resultObservations);
angular.forEach(observations.data.entry.map(function(obs) {
$scope.analysisData[locationCount].observations.push({observation: obs.resource});
}));
})
}))
});
locationCount++
});
return promise.promise
};
I cannot understand in what order the commands are executed. Since I use the Webstorm IDE and its debugging function, it would be more accurate to say that I do not know why the commands are executed in the order that I do not understand.
, , forEach, , , $http.get .then's. locationCount ++ , ( .then()).
? AngularJS?
, ?
/: i.e https://www.hl7.org/fhir/2015May/location.html#5.15.3