Advertisement
Guest User

Caching AJAX

a guest
Jun 6th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. apiRequest : function(requestURL, requestHandler){
  2.             /*Sends an AJAX request to the specified requestURL and uses the requestHandler to respond
  3.              *The requestHandler recieves the api request in a json as a parameter and may be used as such
  4.              *All API requests use GET requests
  5.              */
  6.             if(this.supports_storage()){
  7.                 var apiUrl = requestURL.removeParams();
  8.                 if(typeof this.cachedURL(apiUrl) == "undefined" || !this.cachedURL(apiUrl)){
  9.                     console.log("fetching data");
  10.                     var xmlHttp = null;
  11.                     xmlHttp = new XMLHttpRequest();
  12.                     xmlHttp.onreadystatechange = function(){
  13.                         //Call the handler
  14.                         if(typeof xmlHttp.responseText != "undefined"){
  15.                             if(xmlHttp.readyState==4 && xmlHttp.status==200){
  16.                                 var result = JSON.parse(xmlHttp.responseText);
  17.                                 console.log(typeof this);
  18.                                 requestHandler(result);
  19.                                 //Add resulting json from api to local storage
  20.                                 localStorage.setItem(apiUrl,JSON.stringify(result))    
  21.                                 //Mark as cached   
  22.                                 if(typeof fins.cachedURLs == "undefined"){
  23.                                     fins.cachedURLs = {};
  24.                                 }
  25.                                 fins.cachedURLs[apiUrl] = true;
  26.                             }
  27.                         }
  28.                     };
  29.                     xmlHttp.open( "GET", requestURL, true );
  30.                     xmlHttp.send( null );  
  31.                    
  32.                 }else{
  33.                     //The result is cached so we should go ahead and give that to the function
  34.                     console.log("using cache");
  35.                     requestHandler(JSON.parse(localStorage.getItem(apiUrl)))    ;
  36.                 }
  37.             }else{
  38.                 throw new Error("Local browser storage no supported, use more modern browser");
  39.             }
  40.         },
  41.  
  42.         apiSendData : function(sendURL,requestHandler){
  43.             var xmlHttp = null;
  44.             xmlHttp = new XMLHttpRequest();
  45.             xmlHttp.onreadystatechange = function(){
  46.                 //Convert the response to JSON first.
  47.                 if(typeof xmlHttp.responseText != "undefined"){
  48.                     requestHandler(JSON.parse(xmlHttp.responseText));
  49.                 }
  50.             };
  51.             xmlHttp.open( "GET", sendURL, true );
  52.             xmlHttp.send( null );      
  53.             //Mark the url that maps to retrieving this data
  54.             if(typeof apiURLMaps[sendURL] == "undefined"){
  55.                 throw new Error("Invalid API request in fins:apiSendData, no mapping exists");
  56.             }
  57.             //Mark the corresponding retrievel request for the sending action as dirty and in need of being recached
  58.             cachedURLs[apiURLMaps[sendURL]] = false;
  59.  
  60.         },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement