Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes abort in prefilter. No global event will be fired in that case …

…even if the global option is set to true. Unit test added.
  • Loading branch information...
commit 03bad0a9600f05e61a1464c55354d26fcd8f97a4 1 parent d66cc55
Julian Aubourg authored February 04, 2011
18  src/ajax.js
@@ -388,6 +388,8 @@ jQuery.extend({
388 388
 			parts,
389 389
 			// The jqXHR state
390 390
 			state = 0,
  391
+			// To know if global events are to be dispatched
  392
+			fireGlobals,
391 393
 			// Loop variable
392 394
 			i,
393 395
 			// Fake xhr
@@ -529,7 +531,7 @@ jQuery.extend({
529 531
 			jqXHR.statusCode( statusCode );
530 532
 			statusCode = undefined;
531 533
 
532  
-			if ( s.global ) {
  534
+			if ( fireGlobals ) {
533 535
 				globalEventContext.trigger( "ajax" + ( isSuccess ? "Success" : "Error" ),
534 536
 						[ jqXHR, s, isSuccess ? success : error ] );
535 537
 			}
@@ -537,7 +539,7 @@ jQuery.extend({
537 539
 			// Complete
538 540
 			completeDeferred.resolveWith( callbackContext, [ jqXHR, statusText ] );
539 541
 
540  
-			if ( s.global ) {
  542
+			if ( fireGlobals ) {
541 543
 				globalEventContext.trigger( "ajaxComplete", [ jqXHR, s] );
542 544
 				// Handle the global AJAX counter
543 545
 				if ( !( --jQuery.active ) ) {
@@ -594,6 +596,14 @@ jQuery.extend({
594 596
 		// Apply prefilters
595 597
 		inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
596 598
 
  599
+		// If request was aborted inside a prefiler, stop there
  600
+		if ( state === 2 ) {
  601
+			return false;
  602
+		}
  603
+
  604
+		// We can fire global events as of now if asked to
  605
+		fireGlobals = s.global;
  606
+
597 607
 		// Uppercase the type
598 608
 		s.type = s.type.toUpperCase();
599 609
 
@@ -601,7 +611,7 @@ jQuery.extend({
601 611
 		s.hasContent = !rnoContent.test( s.type );
602 612
 
603 613
 		// Watch for a new set of requests
604  
-		if ( s.global && jQuery.active++ === 0 ) {
  614
+		if ( fireGlobals && jQuery.active++ === 0 ) {
605 615
 			jQuery.event.trigger( "ajaxStart" );
606 616
 		}
607 617
 
@@ -678,7 +688,7 @@ jQuery.extend({
678 688
 				// Set state as sending
679 689
 				state = jqXHR.readyState = 1;
680 690
 				// Send global event
681  
-				if ( s.global ) {
  691
+				if ( fireGlobals ) {
682 692
 					globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
683 693
 				}
684 694
 				// Timeout
19  test/unit/ajax.js
@@ -2177,6 +2177,25 @@ test("jQuery.ajax - transitive conversions", function() {
2177 2177
 
2178 2178
 });
2179 2179
 
  2180
+test("jQuery.ajax - abort in prefilter", function() {
  2181
+
  2182
+	expect( 1 );
  2183
+
  2184
+	jQuery.ajaxPrefilter(function( options, _, jqXHR ) {
  2185
+		if ( options.abortInPrefilter ) {
  2186
+			jqXHR.abort();
  2187
+		}
  2188
+	});
  2189
+
  2190
+	strictEqual( jQuery.ajax({
  2191
+		abortInPrefilter: true,
  2192
+		error: function() {
  2193
+			ok( false, "error callback called" );
  2194
+		}
  2195
+	}), false, "Request was properly aborted early by the prefilter" );
  2196
+
  2197
+});
  2198
+
2180 2199
 test("jQuery.ajax - active counter", function() {
2181 2200
     ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
2182 2201
 });

0 notes on commit 03bad0a

Please sign in to comment.
Something went wrong with that request. Please try again.