Skip to content

Commit dcf0fa5

Browse files
committed
More changes to get jQuery in line with JSLint.
1 parent 8c8f685 commit dcf0fa5

File tree

9 files changed

+190
-185
lines changed

9 files changed

+190
-185
lines changed

src/ajax.js

+130-126
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,10 @@ jQuery.extend({
203203
etag: {},
204204

205205
ajax: function( origSettings ) {
206-
var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings);
207-
208-
var jsonp, status, data,
209-
callbackContext = origSettings && origSettings.context || s,
210-
type = s.type.toUpperCase();
206+
var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings),
207+
jsonp, status, data, type = s.type.toUpperCase();
208+
209+
s.context = origSettings && origSettings.context || s;
211210

212211
// convert data if not already a string
213212
if ( s.data && s.processData && typeof s.data !== "string" ) {
@@ -244,14 +243,14 @@ jQuery.extend({
244243
// Handle JSONP-style loading
245244
window[ jsonp ] = window[ jsonp ] || function( tmp ) {
246245
data = tmp;
247-
success();
248-
complete();
246+
jQuery.ajax.handleSuccess( s, xhr, status, data );
247+
jQuery.ajax.handleComplete( s, xhr, status, data );
249248
// Garbage collect
250249
window[ jsonp ] = undefined;
251250

252251
try {
253252
delete window[ jsonp ];
254-
} catch(e) {}
253+
} catch( jsonpError ) {}
255254

256255
if ( head ) {
257256
head.removeChild( script );
@@ -279,7 +278,7 @@ jQuery.extend({
279278
}
280279

281280
// Watch for a new set of requests
282-
if ( s.global && jQuery.active++ === 0 ) {
281+
if ( s.global && jQuery.ajax.active++ === 0 ) {
283282
jQuery.event.trigger( "ajaxStart" );
284283
}
285284

@@ -306,8 +305,8 @@ jQuery.extend({
306305
if ( !done && (!this.readyState ||
307306
this.readyState === "loaded" || this.readyState === "complete") ) {
308307
done = true;
309-
success();
310-
complete();
308+
jQuery.ajax.handleSuccess( s, xhr, status, data );
309+
jQuery.ajax.handleComplete( s, xhr, status, data );
311310

312311
// Handle memory leak in IE
313312
script.onload = script.onreadystatechange = null;
@@ -371,12 +370,12 @@ jQuery.extend({
371370
xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ?
372371
s.accepts[ s.dataType ] + ", */*" :
373372
s.accepts._default );
374-
} catch(e) {}
373+
} catch( headerError ) {}
375374

376375
// Allow custom headers/mimetypes and early abort
377-
if ( s.beforeSend && s.beforeSend.call(callbackContext, xhr, s) === false ) {
376+
if ( s.beforeSend && s.beforeSend.call(s.context, xhr, s) === false ) {
378377
// Handle the global AJAX counter
379-
if ( s.global && ! --jQuery.active ) {
378+
if ( s.global && jQuery.ajax.active-- === 1 ) {
380379
jQuery.event.trigger( "ajaxStop" );
381380
}
382381

@@ -386,7 +385,7 @@ jQuery.extend({
386385
}
387386

388387
if ( s.global ) {
389-
contextTrigger("ajaxSend", [xhr, s]);
388+
jQuery.ajax.triggerGlobal( s, "ajaxSend", [xhr, s] );
390389
}
391390

392391
// Wait for a response to come back
@@ -396,7 +395,7 @@ jQuery.extend({
396395
// Opera doesn't call onreadystatechange before this point
397396
// so we simulate the call
398397
if ( !requestDone ) {
399-
complete();
398+
jQuery.ajax.handleComplete( s, xhr, status, data );
400399
}
401400

402401
requestDone = true;
@@ -411,9 +410,9 @@ jQuery.extend({
411410

412411
status = isTimeout === "timeout" ?
413412
"timeout" :
414-
!jQuery.httpSuccess( xhr ) ?
413+
!jQuery.ajax.httpSuccess( xhr ) ?
415414
"error" :
416-
s.ifModified && jQuery.httpNotModified( xhr, s.url ) ?
415+
s.ifModified && jQuery.ajax.httpNotModified( xhr, s.url ) ?
417416
"notmodified" :
418417
"success";
419418

@@ -423,25 +422,25 @@ jQuery.extend({
423422
// Watch for, and catch, XML document parse errors
424423
try {
425424
// process the data (runs the xml through httpData regardless of callback)
426-
data = jQuery.httpData( xhr, s.dataType, s );
427-
} catch(err) {
425+
data = jQuery.ajax.httpData( xhr, s.dataType, s );
426+
} catch( parserError ) {
428427
status = "parsererror";
429-
errMsg = err;
428+
errMsg = parserError;
430429
}
431430
}
432431

433432
// Make sure that the request was successful or notmodified
434433
if ( status === "success" || status === "notmodified" ) {
435434
// JSONP handles its own success callback
436435
if ( !jsonp ) {
437-
success();
436+
jQuery.ajax.handleSuccess( s, xhr, status, data );
438437
}
439438
} else {
440-
jQuery.handleError(s, xhr, status, errMsg);
439+
jQuery.ajax.handleError( s, xhr, status, errMsg );
441440
}
442441

443442
// Fire the complete handlers
444-
complete();
443+
jQuery.ajax.handleComplete( s, xhr, status, data );
445444

446445
if ( isTimeout === "timeout" ) {
447446
xhr.abort();
@@ -465,7 +464,7 @@ jQuery.extend({
465464

466465
onreadystatechange( "abort" );
467466
};
468-
} catch(e) { }
467+
} catch( abortError ) {}
469468

470469
// Timeout checker
471470
if ( s.async && s.timeout > 0 ) {
@@ -480,68 +479,138 @@ jQuery.extend({
480479
// Send the data
481480
try {
482481
xhr.send( type === "POST" || type === "PUT" || type === "DELETE" ? s.data : null );
483-
} catch(e) {
484-
jQuery.handleError(s, xhr, null, e);
482+
483+
} catch( sendError ) {
484+
jQuery.ajax.handleError( s, xhr, null, e );
485+
485486
// Fire the complete handlers
486-
complete();
487+
jQuery.ajax.handleComplete( s, xhr, status, data );
487488
}
488489

489490
// firefox 1.5 doesn't fire statechange for sync requests
490491
if ( !s.async ) {
491492
onreadystatechange();
492493
}
493494

494-
function success() {
495-
// If a local callback was specified, fire it and pass it the data
496-
if ( s.success ) {
497-
s.success.call( callbackContext, data, status, xhr );
498-
}
495+
// return XMLHttpRequest to allow aborting the request etc.
496+
return xhr;
497+
},
499498

500-
// Fire the global callback
501-
if ( s.global ) {
502-
contextTrigger( "ajaxSuccess", [xhr, s] );
499+
// Serialize an array of form elements or a set of
500+
// key/values into a query string
501+
param: function( a, traditional ) {
502+
var s = [], add = function( key, value ) {
503+
// If value is a function, invoke it and return its value
504+
value = jQuery.isFunction(value) ? value() : value;
505+
s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
506+
};
507+
508+
// Set traditional to true for jQuery <= 1.3.2 behavior.
509+
if ( traditional === undefined ) {
510+
traditional = jQuery.ajaxSettings.traditional;
511+
}
512+
513+
// If an array was passed in, assume that it is an array of form elements.
514+
if ( jQuery.isArray(a) || a.jquery ) {
515+
// Serialize the form elements
516+
jQuery.each( a, function() {
517+
add( this.name, this.value );
518+
});
519+
520+
} else {
521+
// If traditional, encode the "old" way (the way 1.3.2 or older
522+
// did it), otherwise encode params recursively.
523+
for ( var prefix in a ) {
524+
buildParams( prefix, a[prefix], traditional, add );
503525
}
504526
}
505527

506-
function complete() {
507-
// Process result
508-
if ( s.complete ) {
509-
s.complete.call( callbackContext, xhr, status);
510-
}
528+
// Return the resulting serialization
529+
return s.join("&").replace(r20, "+");
530+
}
531+
});
511532

512-
// The request was completed
513-
if ( s.global ) {
514-
contextTrigger( "ajaxComplete", [xhr, s] );
515-
}
533+
function buildParams( prefix, obj, traditional, add ) {
534+
if ( jQuery.isArray(obj) ) {
535+
// Serialize array item.
536+
jQuery.each( obj, function( i, v ) {
537+
if ( traditional || /\[\]$/.test( prefix ) ) {
538+
// Treat each array item as a scalar.
539+
add( prefix, v );
516540

517-
// Handle the global AJAX counter
518-
if ( s.global && ! --jQuery.active ) {
519-
jQuery.event.trigger( "ajaxStop" );
541+
} else {
542+
// If array item is non-scalar (array or object), encode its
543+
// numeric index to resolve deserialization ambiguity issues.
544+
// Note that rack (as of 1.0.0) can't currently deserialize
545+
// nested arrays properly, and attempting to do so may cause
546+
// a server error. Possible fixes are to modify rack's
547+
// deserialization algorithm or to provide an option or flag
548+
// to force array serialization to be shallow.
549+
buildParams( prefix + "[" + ( typeof v === "object" || jQuery.isArray(v) ? i : "" ) + "]", v, traditional, add );
520550
}
521-
}
522-
523-
function contextTrigger(type, args) {
524-
(s.context ? jQuery(s.context) : jQuery.event).trigger(type, args);
525-
}
551+
});
552+
553+
} else if ( !traditional && obj != null && typeof obj === "object" ) {
554+
// Serialize object item.
555+
jQuery.each( obj, function( k, v ) {
556+
buildParams( prefix + "[" + k + "]", v, traditional, add );
557+
});
558+
559+
} else {
560+
// Serialize scalar item.
561+
add( prefix, obj );
562+
}
563+
}
526564

527-
// return XMLHttpRequest to allow aborting the request etc.
528-
return xhr;
529-
},
565+
jQuery.extend( jQuery.ajax, {
566+
567+
// Counter for holding the number of active queries
568+
active: 0,
530569

531570
handleError: function( s, xhr, status, e ) {
532571
// If a local callback was specified, fire it
533572
if ( s.error ) {
534-
s.error.call( s.context || s, xhr, status, e );
573+
s.error.call( s.context, xhr, status, e );
535574
}
536575

537576
// Fire the global callback
538577
if ( s.global ) {
539-
(s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] );
578+
jQuery.ajax.triggerGlobal( s, "ajaxError", [xhr, s, e] );
540579
}
541580
},
542581

543-
// Counter for holding the number of active queries
544-
active: 0,
582+
handleSuccess: function( s, xhr, status, data ) {
583+
// If a local callback was specified, fire it and pass it the data
584+
if ( s.success ) {
585+
s.success.call( s.context, data, status, xhr );
586+
}
587+
588+
// Fire the global callback
589+
if ( s.global ) {
590+
jQuery.ajax.triggerGlobal( s, "ajaxSuccess", [xhr, s] );
591+
}
592+
},
593+
594+
handleComplete: function( s, xhr, status ) {
595+
// Process result
596+
if ( s.complete ) {
597+
s.complete.call( s.context, xhr, status );
598+
}
599+
600+
// The request was completed
601+
if ( s.global ) {
602+
jQuery.ajax.triggerGlobal( s, "ajaxComplete", [xhr, s] );
603+
}
604+
605+
// Handle the global AJAX counter
606+
if ( s.global && jQuery.ajax.active-- === 1 ) {
607+
jQuery.event.trigger( "ajaxStop" );
608+
}
609+
},
610+
611+
triggerGlobal: function( s, type, args ) {
612+
(s.context && s.context.url == null ? jQuery(s.context) : jQuery.event).trigger(type, args);
613+
},
545614

546615
// Determines if an XMLHttpRequest was successful or not
547616
httpSuccess: function( xhr ) {
@@ -601,71 +670,6 @@ jQuery.extend({
601670
}
602671

603672
return data;
604-
},
605-
606-
// Serialize an array of form elements or a set of
607-
// key/values into a query string
608-
param: function( a, traditional ) {
609-
var s = [];
610-
611-
// Set traditional to true for jQuery <= 1.3.2 behavior.
612-
if ( traditional === undefined ) {
613-
traditional = jQuery.ajaxSettings.traditional;
614-
}
615-
616-
// If an array was passed in, assume that it is an array of form elements.
617-
if ( jQuery.isArray(a) || a.jquery ) {
618-
// Serialize the form elements
619-
jQuery.each( a, function() {
620-
add( this.name, this.value );
621-
});
622-
623-
} else {
624-
// If traditional, encode the "old" way (the way 1.3.2 or older
625-
// did it), otherwise encode params recursively.
626-
for ( var prefix in a ) {
627-
buildParams( prefix, a[prefix] );
628-
}
629-
}
630-
631-
// Return the resulting serialization
632-
return s.join("&").replace(r20, "+");
633-
634-
function buildParams( prefix, obj ) {
635-
if ( jQuery.isArray(obj) ) {
636-
// Serialize array item.
637-
jQuery.each( obj, function( i, v ) {
638-
if ( traditional || /\[\]$/.test( prefix ) ) {
639-
// Treat each array item as a scalar.
640-
add( prefix, v );
641-
} else {
642-
// If array item is non-scalar (array or object), encode its
643-
// numeric index to resolve deserialization ambiguity issues.
644-
// Note that rack (as of 1.0.0) can't currently deserialize
645-
// nested arrays properly, and attempting to do so may cause
646-
// a server error. Possible fixes are to modify rack's
647-
// deserialization algorithm or to provide an option or flag
648-
// to force array serialization to be shallow.
649-
buildParams( prefix + "[" + ( typeof v === "object" || jQuery.isArray(v) ? i : "" ) + "]", v );
650-
}
651-
});
652-
653-
} else if ( !traditional && obj != null && typeof obj === "object" ) {
654-
// Serialize object item.
655-
jQuery.each( obj, function( k, v ) {
656-
buildParams( prefix + "[" + k + "]", v );
657-
});
658-
659-
} else {
660-
// Serialize scalar item.
661-
add( prefix, obj );
662-
}
663-
}
664-
665-
function add( key, value ) {
666-
// If value is a function, invoke it and return its value
667-
value = jQuery.isFunction(value) ? value() : value;
668-
s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
669-
}
670673
}
674+
671675
});

0 commit comments

Comments
 (0)