Skip to content

Instantly share code, notes, and snippets.

@kokarn
Last active August 29, 2015 14:14
Show Gist options
  • Save kokarn/65941e59afdc0818280c to your computer and use it in GitHub Desktop.
Save kokarn/65941e59afdc0818280c to your computer and use it in GitHub Desktop.
var loadData : function( urlEndpoint, callback ){
var fullPath = this.baseUrl + urlEndpoint,
encodedString = utf8ToBase64( fullPath ),
$deferred;
if( typeof fbiApiHelper.cacheData[ encodedString ] !== 'undefined' ){
if( typeof fbiApiHelper.cacheData[ encodedString ].readyState !== 'undefined' ){
// XHR object, do as ususal
fbiApiHelper.cacheData[ encodedString ].done( function( response ){
callback.call( this, response );
} );
return fbiApiHelper.cacheData[ encodedString ];
} else {
// Not an XHR object, data is loaded
callback.call( this, fbiApiHelper.cacheData[ encodedString ] );
$deferred = $.Deferred();
$deferred.resolve( fbiApiHelper.cacheData[ encodedString ] );
console.log( 'Loaded ' + fullPath + ' from cache' );
return $deferred.promise();
}
}
var xhr = $.ajax({
url : fullPath,
method : 'GET',
dataType : 'jsonp',
jsonp: '_jsonp'
});
fbiApiHelper.cacheData[ encodedString ] = xhr;
xhr.done( function( response ){
fbiApiHelper.cacheData[ encodedString ] = response;
callback.call( this, response );
} );
xhr.fail( function( jqXHR, textStatus ) {
console.log( 'Request failed: ' + textStatus );
});
return xhr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment