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

View file

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

View file

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

View file

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