I created following controller with 3 differents $http GET calls to a rest-api.
$http({method: 'GET', url: REST_CONFIG.url+'/api/lims/data/runs/list'})
.success(function(data, status, headers, config) {
form.runs = data;
})
.error(function(data, status, headers, config) {
form.runs = [];
});
form.data.analysis = {"analysisName": "","analysisprofile": {"workflows": []},"run": ""};
//Get all Default Workflows
$http({method: 'GET', url: REST_CONFIG.url+'/api/workflows/default/list'})
.success(function(data, status, headers, config) {
form.workflows = data;
})
.error(function(data, status, headers, config) {
form.workflows = [];
});
//Get all databases
$http({method: 'GET', url: REST_CONFIG.url+'/api/list-databases'})
.success(function(data, status, headers, config) {
form.databases = data;
})
.error(function(data, status, headers, config) {
form.databases = [];
});
Sometimes I have the same results from query1 and query2 (query2 have the result from query1). In that case the rest-api do 2 times the query1. My browser say that the http queries are good (3 differents url). This is weird and really annoying. I also tried to do:
//Get all runs
runs = $http({method: 'GET', url: REST_CONFIG.url+'/api/lims/data/runs/list'});
//Get all Default workflows
defaultWorkflows = $http({method: 'GET', url: REST_CONFIG.url+'/api/workflows/default/list'});
//Get all databases
databases = $http({method: 'GET', url: REST_CONFIG.url+'/api/list-databases'});
$q.all([runs, defaultWorkflows, databases]).then(function(values) {
form.runs = values[0].data;
form.workflows = values[1].data;
form.databases = values[2].data;
});
Nothing worked. Is it coming from the rest-api? Or I am doing something wrong?
EDIT Problem solved. The key point was the use of $q with promise and deffer(). This plunkr helped me a lot: http://plnkr.co/edit/NGMp4ycmaCqVOmgohN53?p=preview
I use the following code:
var getInfo = function(){
var promises = [];
var urls = [];
urls.push(REST_CONFIG.url+'/api/lims/data/runs/list');
urls.push(REST_CONFIG.url+'/api/workflows/default/list');
urls.push(REST_CONFIG.url+'/api/list-databases');
angular.forEach(urls, function(url){
var deffered = $q.defer();
$http({method: 'GET', url: url})
.then(function successCallback(response) {
deffered.resolve(response.data);
}, function errorCallback(response) {
deffered.reject();
});
promises.push(deffered.promise);
})
return $q.all(promises);
}
var init = function(){
var promiseInfo = getInfo();
promiseInfo.then(function(datas){
form.runs = datas[0];
form.workflows = datas[1];
form.databases = datas[2];
})
};