Is there a way to call an unknown amount of API calls to a URL via the get()
function in AngularJS and add those all into a model ($scope
variable). What I've done thus far is the following:
if(theUIDS != "") {
var myDropbox = [];
for(i = 0; i < theUIDS.length; i++) {
var temp = {};
temp.uid = theUIDS[i];
$http({ url: '/dropbox/accounts/get', method: 'GET', params: { uid: theUIDS[i] }}).success(function(acctData) {
temp.accountInfo = acctData;
});
$http({ url: '/dropbox/files/get', method: 'GET', params: { uid: theUIDS[i] }}).success(function(fileData) {
temp.files = fileData;
});
myDropbox.push(temp);
}
$scope.dropboxAccounts = myDropbox;
}
I check if there are any UID's and for each one I create a temp
object which is populated with a uid
, then an accountInfo
object, and then a files
object. After I set up my temp
object, I push it onto the myDropbox
array. Once the loop has finished, I set the dropboxAccounts model to the myDropbox
variable in $scope
. I'm new to Angular, but I'm pretty sure this is at least the right idea. Luckily I'm getting the following data in correct order:
{"uid":"332879"}
{"uid":"155478421",
"accountInfo":{
"country":"US",
"display_name":"Patrick Cason",
"name":"Second Dropbox",
"quota_normal":1174504,
"quota_shared":0,
"quota_total":2147483648,
"referral_link":"https://www.dropbox.com/referrals/NTE1NTQ3ODQyMTk?src=app9-203957",
"uid":155478421},
"files":[{
"created_at":"2013-04-17T15:13:46Z",
"directory":true,
"dropbox_user_id":26,
"fileType":"Folder",
"id":198,
"name":"Photos",
"path":"/Photos",
"rev":"10edb44f9",
"size":"-",
"updated_at":"2013-04-17T15:13:46Z"}]
}
The strange thing is that only one of my UID's gets updated. I know that the loop is correctly going through because I have two UID's and if I alert at the beginning the loop I get two loops. The reason I think the second isn't being populated is because the push statement isn't waiting for both of the promises to go through. How can I ensure that I wait for each of the AJAX calls to finish before assigning myDropbox
to the $scope
variable?