Last active
August 29, 2015 14:14
-
-
Save kokarn/65941e59afdc0818280c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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