Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple AngularJS get requests into one model
    primarykey
    data
    text
    <p>Is there a way to call an unknown amount of API calls to a URL via the <code>get()</code> function in AngularJS and add those all into a model (<code>$scope</code> variable). What I've done thus far is the following:</p> <pre><code>if(theUIDS != "") { var myDropbox = []; for(i = 0; i &lt; 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; } </code></pre> <p>I check if there are any UID's and for each one I create a <code>temp</code> object which is populated with a <code>uid</code>, then an <code>accountInfo</code> object, and then a <code>files</code> object. After I set up my <code>temp</code> object, I push it onto the <code>myDropbox</code> array. Once the loop has finished, I set the dropboxAccounts model to the <code>myDropbox</code> variable in <code>$scope</code>. 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:</p> <pre><code>{"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"}] } </code></pre> <p>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 <code>myDropbox</code> to the <code>$scope</code> variable?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload