Apply JQuery Core Style Guidelines to ajax.js and ajax/*.js,

This commit is contained in:
jaubourg 2011-01-23 05:51:41 +01:00
parent 30082d9eba
commit dd5bf42122
4 changed files with 182 additions and 199 deletions

View File

@ -20,20 +20,22 @@ var r20 = /%20/g,
// Keep a copy of the old load method
_load = jQuery.fn.load,
// Prefilters
// 1) They are useful to introduce custom dataTypes (see transport/jsonp for an example)
// 2) These are called:
// * BEFORE asking for a transport
// * AFTER param serialization (s.data is a string if s.processData is true)
// 3) key is the dataType
// 4) the catchall symbol "*" can be used
// 5) execution will start with transport dataType and THEN continue down to "*" if needed
/* Prefilters
* 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example)
* 2) These are called:
* - BEFORE asking for a transport
* - AFTER param serialization (s.data is a string if s.processData is true)
* 3) key is the dataType
* 4) the catchall symbol "*" can be used
* 5) execution will start with transport dataType and THEN continue down to "*" if needed
*/
prefilters = {},
// Transports bindings
// 1) key is the dataType
// 2) the catchall symbol "*" can be used
// 3) selection will start with transport dataType and THEN go to "*" if needed
/* Transports bindings
* 1) key is the dataType
* 2) the catchall symbol "*" can be used
* 3) selection will start with transport dataType and THEN go to "*" if needed
*/
transports = {};
jQuery.fn.extend({
@ -149,7 +151,7 @@ jQuery.each( "ajaxStart ajaxStop ajaxComplete ajaxError ajaxSuccess ajaxSend".sp
jQuery.each( [ "get", "post" ], function( i, method ) {
jQuery[ method ] = function( url, data, callback, type ) {
// shift arguments if data argument was omited
// shift arguments if data argument was omitted
if ( jQuery.isFunction( data ) ) {
type = type || callback;
callback = data;
@ -513,7 +515,7 @@ jQuery.extend({
ret = s.url.replace( rts, "$1_=" + ts );
// if nothing was replaced, add timestamp to the end
s.url = ret + ( (ret == s.url ) ? ( rquery.test( s.url ) ? "&" : "?" ) + "_=" + ts : "");
s.url = ret + ( (ret === s.url ) ? ( rquery.test( s.url ) ? "&" : "?" ) + "_=" + ts : "" );
}
}
@ -544,10 +546,8 @@ jQuery.extend({
// Allow custom headers/mimetypes and early abort
if ( s.beforeSend && ( s.beforeSend.call( callbackContext, jXHR, s ) === false || state === 2 ) ) {
// Abort if not done already
done( 0, "abort" );
// Return false
jXHR = false;
@ -565,15 +565,12 @@ jQuery.extend({
if ( !transport ) {
done( 0, "notransport" );
} else {
// Set state as sending
state = jXHR.readyState = 1;
// Send global event
if ( s.global ) {
globalEventContext.trigger( "ajaxSend", [ jXHR, s ] );
}
// Timeout
if ( s.async && s.timeout > 0 ) {
timeoutTimer = setTimeout( function(){
@ -595,7 +592,6 @@ jQuery.extend({
}
}
}
return jXHR;
},
@ -776,10 +772,11 @@ function prefiltersOrTransports( structure , arg1 , arg2 , type /* internal */ )
}
}
// Handles responses to an ajax request:
// - sets all responseXXX fields accordingly
// - finds the right dataType (mediating between content-type and expecting dataType)
// - returns the corresponding response
/* Handles responses to an ajax request:
* - sets all responseXXX fields accordingly
* - finds the right dataType (mediates between content-type and expected dataType)
* - returns the corresponding response
*/
function ajaxHandleResponses( s, jXHR, responses ) {
var contents = s.contents,
@ -915,7 +912,6 @@ function ajaxConvert( s , response ) {
}
}
}
return response;
}

View File

@ -63,7 +63,9 @@ jQuery.ajaxPrefilter("json jsonp", function(s, originalSettings, dataIsString) {
}
} else {
// else, more memory leak avoidance
try{ delete window[ jsonpCallback ]; } catch(e){}
try{
delete window[ jsonpCallback ];
} catch( e ) {}
}
}, s.complete ];

View File

@ -2,15 +2,12 @@
// Install script dataType
jQuery.ajaxSetup({
accepts: {
script: "text/javascript, application/javascript"
},
contents: {
script: /javascript/
},
converters: {
"text script": jQuery.globalEval
}
@ -18,11 +15,9 @@ jQuery.ajaxSetup({
// Handle cache's special case and global
jQuery.ajaxPrefilter( "script", function(s) {
if ( s.cache === undefined ) {
s.cache = false;
}
if ( s.crossDomain ) {
s.type = "GET";
s.global = false;
@ -66,7 +61,7 @@ jQuery.ajaxTransport("script", function(s) {
}
// Dereference the script
script = 0;
script = undefined;
// Callback if not abort
if ( !isAbort ) {

View File

@ -46,22 +46,21 @@ try {
jQuery.support.ajax = !!testXHR;
// Does this browser support crossDomain XHR requests
jQuery.support.cors = testXHR && "withCredentials" in testXHR;
jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
// No need for the temporary xhr anymore
testXHR = undefined;
// Create transport if the browser can provide an xhr
if ( jQuery.support.ajax ) {
jQuery.ajaxTransport( function( s ) {
jQuery.ajaxTransport(function( s ) {
// Cross domain only allowed if supported through XMLHttpRequest
if ( !s.crossDomain || jQuery.support.cors ) {
var callback;
return {
send: function( headers, complete ) {
// #5280: we need to abort on unload or IE will keep connections alive
@ -103,11 +102,9 @@ if ( jQuery.support.ajax ) {
// Need an extra try/catch for cross domain requests in Firefox 3
try {
jQuery.each( headers, function( key, value ) {
xhr.setRequestHeader( key, value );
} );
} catch( _ ) {}
// Do send the request
@ -135,13 +132,11 @@ if ( jQuery.support.ajax ) {
// If it's an abort
if ( isAbort ) {
// Abort it manually if needed
if ( xhr.readyState !== 4 ) {
xhr.abort();
}
} else {
// Get info
var status = xhr.status,
statusText,
@ -155,38 +150,36 @@ if ( jQuery.support.ajax ) {
}
responses.text = xhr.responseText;
try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests
// Firefox throws an exception when accessing
// statusText for faulty cross-domain requests
try {
statusText = xhr.statusText;
} catch( e ) {
statusText = ""; // We normalize with Webkit giving an empty statusText
// We normalize with Webkit giving an empty statusText
statusText = "";
}
// Filter status for non standard behaviours
// (so many they seem to be the actual "standard")
status =
// Opera returns 0 when it should be 304
// Webkit returns 0 for failing cross-domain no matter the real status
status === 0 ?
(
! s.crossDomain || statusText ? // Webkit, Firefox: filter out faulty cross-domain requests
// Webkit, Firefox: filter out faulty cross-domain requests
!s.crossDomain || statusText ?
(
responseHeaders ? // Opera: filter out real aborts #6060
304
:
// Opera: filter out real aborts #6060
responseHeaders ?
304 :
0
)
:
302 // We assume 302 but could be anything cross-domain related
)
:
) :
// We assume 302 but could be anything cross-domain related
302
) :
(
status == 1223 ? // IE sometimes returns 1223 when it should be 204 (see #1450)
204
:
// IE sometimes returns 1223 when it should be 204 (see #1450)
status == 1223 ?
204 :
status
);
@ -196,15 +189,12 @@ if ( jQuery.support.ajax ) {
}
};
// if we're in sync mode
// or it's in cache and has been retrieved directly (IE6 & IE7)
// if we're in sync mode or it's in cache
// and has been retrieved directly (IE6 & IE7)
// we need to manually fire the callback
if ( !s.async || xhr.readyState === 4 ) {
callback();
} else {
// Add to list of active xhrs
handle = xhrId++;
xhrs[ handle ] = xhr;