Currently in my AngularJS project, I have created a service with multiple functions that return promises.
The AJAX Service I've Created:
angular.module('yoApp')
.factory('serviceAjax', function serviceAjax($http) {
return{
configDataSets:function(path){
return $http.get(path);
},
fileList: function(site_url,dataSet,varName,region,date){
return $http.get(site_url + "mwf/" + dataSet + "/" +varName + "/" + region + "/" + date + "/filelist/");
},
config: function(site_url,dataSet){
return $http.get(site_url + "mwf/" + dataSet + "/config/");
},
}});
Code in the Controller:
I've already implemented something similar to this, but considering potential chained requests, I believe there might be a more efficient way to handle this:
serviceAjax.config(site_url,$scope.dataset).then function(data){
$scope.config=data.data;
serviceAjax.configDataSets("images/configDatasets.json").then(function(data){
$scope.configDatasets = data.data;
});
});
In order to improve the process, I want to first call config in my controller and then proceed to configDataSets, waiting for each operation to finish. I have explored options like $q and deferred, but struggling to find the best approach. Any guidance would be highly appreciated!