Advertisement
Guest User

Multiple API Calls

a guest
Jan 17th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. login: function(account_name, username, password){
  2.  
  3.   var xmlhttp;
  4.  
  5.   if (window.XMLHttpRequest) {
  6.       // code for IE7+, Firefox, Chrome, Opera, Safari
  7.       xmlhttp = new XMLHttpRequest();
  8.   } else {
  9.       // code for IE6, IE5
  10.       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  11.   }
  12.  
  13.   xmlhttp.onreadystatechange = function() {
  14.       if (xmlhttp.readyState == 4 ) {
  15.          if(xmlhttp.status == 200){
  16.  
  17.             var response = JSON.parse(xmlhttp.responseText);
  18.            
  19.             // Error handling if the status is incorrect.          
  20.             if(response.status !== true){
  21.                 // Shows an error message here ...
  22.             }
  23.             else{
  24.              
  25.               // Login successful so lets parse the shizzle.
  26.               var user = response.user;
  27.  
  28.               // Stores the Users details here ...
  29.  
  30.            
  31.               // WHERE I FALL DOWN :( :D
  32.               // Now we need to load things from the api there are numerous things to load from the API
  33.               // The first being the sites so this gets called first.
  34.               // However when this is loaded I then need to then make another api call to load further resources.
  35.               // What is the best way todo this?
  36.  
  37.               // Currently my thoughts are declare a function here to perform the next api call here.
  38.               function success(){
  39.                 // Do the next api call maybe?
  40.                 app.getVehicles(success, failure);
  41.               }
  42.  
  43.               function failure(){
  44.                 // Show some error message here.
  45.               }
  46.  
  47.               // Start the process here
  48.               app.getSites(success, failure);
  49.  
  50.             }
  51.          }
  52.          else if(xmlhttp.status == 400) {
  53.             // Shows an error message here ...
  54.          }
  55.          else {
  56.             // Shows an error message here ...
  57.          }
  58.       }
  59.   }
  60.  
  61.   // Build the params and configure the rest of the call.
  62.   var params = '&account_name=' + account_name + '&email=' + username + '&password=' + password;
  63.   xmlhttp.open("POST", app.apiUrl + "login", true);
  64.   xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  65.   xmlhttp.send(params);
  66.  
  67. },
  68.  
  69. getSites: function(successFunction, failureFunction(){
  70.     // Perform Ajax request here then if success full call the success function which could call the next resource on failure call the failure function.
  71. },
  72.  
  73. getVehicles: function(successFunction, failureFunction(){
  74.     // Perform Ajax request here then if success full call the success function which could call the next resource or if the last resource to load check if loaded and then on failure call the failure function.
  75. },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement