Simplify OpenSeadragon.makeAjaxRequest

* Since async is always true – and browsers are starting to deprecate
  synchronous XHR – we were able to prune considerable amount of code
* Add an error callback to match the existing success callback
This commit is contained in:
Chris Adams 2013-06-26 16:27:52 -04:00
parent bb1dd78979
commit 496b7d757b

View File

@ -1306,64 +1306,39 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
* @function * @function
* @name OpenSeadragon.makeAjaxRequest * @name OpenSeadragon.makeAjaxRequest
* @param {String} url - the url to request * @param {String} url - the url to request
* @param {Function} [callback] - a function to call when complete * @param {Function} onSuccess - a function to call on a successful response
* @param {Function} onError - a function to call on when an error occurs
* @throws {Error} * @throws {Error}
*/ */
makeAjaxRequest: function( url, callback ) { makeAjaxRequest: function( url, onSuccess, onError ) {
var request = $.createAjaxRequest();
var async = true, request.onreadystatechange = function() {
request = $.createAjaxRequest(), // 4 = DONE (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#Properties)
options; if ( request.readyState == 4) {
request.onreadystatechange = function(){};
if (request.status == 200) {
if( $.isPlainObject( url ) ){ onSuccess( request );
options.async = options.async || async; } else {
}else{ onError( request );
options = {
url: url,
async: $.isFunction( callback ),
success: callback,
error: null
};
}
if ( options.async ) {
/** @ignore */
request.onreadystatechange = function() {
// 4 = DONE (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#Properties)
if ( request.readyState == 4) {
request.onreadystatechange = function(){};
options.success( request );
} }
}; }
} };
try { try {
request.open( "GET", options.url, options.async ); request.open( "GET", url, true );
request.send( null ); request.send( null );
} catch (e) { } catch (e) {
$.console.log( $.console.log("%s while making AJAX request: %s", e.name, e.message);
"%s while making AJAX request: %s",
e.name,
e.message
);
request.onreadystatechange = function(){}; request.onreadystatechange = function(){};
request = null; request = null;
if ( options.error && $.isFunction( options.error ) ) { onError(request, e);
options.error( request );
}
} }
if( !options.async && $.isFunction( options.success ) ){
options.success( request );
}
return options.async ? null : request;
}, },
/** /**
* Taken from jQuery 1.6.1 * Taken from jQuery 1.6.1
* @function * @function