I'm writing an Angular.js service to pull some JSON
feeds. Initially, the service does not know which/how many resources to request; they are dependent on ids returned by a another request.
I'm having a headache chaining the $http
service requests together. Is there a commonly used pattern to do this?
I've tried the Array.reduce
technique suggested on another thread, but had problems syncing the ids and the requested data.
This is what I have so far. Does anyone have any suggestions?
aService.factory('dummy', function($http){
// Dummy resources.
var resources = [
'http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js',
'http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js',
'http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'
];
return {
data: function(callback){
var jquerySources = []
var promiseChain = $http({method: 'GET', url: resources[0]});
var l = resources.length
for (var i = 1; i < l; i++ ){
promiseChain.then(function(data){
jquerySources.push({
url: resources[i],
source: data
});
promise_chain = $http({method: 'GET', url: resources[i]});
if (i === l){
return callback(jquerySources);
}
});
}
}
}
});
Thankyou.