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.
This commit is contained in:
parent
d66cc55316
commit
03bad0a960
18
src/ajax.js
18
src/ajax.js
|
@ -388,6 +388,8 @@ jQuery.extend({
|
||||||
parts,
|
parts,
|
||||||
// The jqXHR state
|
// The jqXHR state
|
||||||
state = 0,
|
state = 0,
|
||||||
|
// To know if global events are to be dispatched
|
||||||
|
fireGlobals,
|
||||||
// Loop variable
|
// Loop variable
|
||||||
i,
|
i,
|
||||||
// Fake xhr
|
// Fake xhr
|
||||||
|
@ -529,7 +531,7 @@ jQuery.extend({
|
||||||
jqXHR.statusCode( statusCode );
|
jqXHR.statusCode( statusCode );
|
||||||
statusCode = undefined;
|
statusCode = undefined;
|
||||||
|
|
||||||
if ( s.global ) {
|
if ( fireGlobals ) {
|
||||||
globalEventContext.trigger( "ajax" + ( isSuccess ? "Success" : "Error" ),
|
globalEventContext.trigger( "ajax" + ( isSuccess ? "Success" : "Error" ),
|
||||||
[ jqXHR, s, isSuccess ? success : error ] );
|
[ jqXHR, s, isSuccess ? success : error ] );
|
||||||
}
|
}
|
||||||
|
@ -537,7 +539,7 @@ jQuery.extend({
|
||||||
// Complete
|
// Complete
|
||||||
completeDeferred.resolveWith( callbackContext, [ jqXHR, statusText ] );
|
completeDeferred.resolveWith( callbackContext, [ jqXHR, statusText ] );
|
||||||
|
|
||||||
if ( s.global ) {
|
if ( fireGlobals ) {
|
||||||
globalEventContext.trigger( "ajaxComplete", [ jqXHR, s] );
|
globalEventContext.trigger( "ajaxComplete", [ jqXHR, s] );
|
||||||
// Handle the global AJAX counter
|
// Handle the global AJAX counter
|
||||||
if ( !( --jQuery.active ) ) {
|
if ( !( --jQuery.active ) ) {
|
||||||
|
@ -594,6 +596,14 @@ jQuery.extend({
|
||||||
// Apply prefilters
|
// Apply prefilters
|
||||||
inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
|
inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
|
||||||
|
|
||||||
|
// If request was aborted inside a prefiler, stop there
|
||||||
|
if ( state === 2 ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We can fire global events as of now if asked to
|
||||||
|
fireGlobals = s.global;
|
||||||
|
|
||||||
// Uppercase the type
|
// Uppercase the type
|
||||||
s.type = s.type.toUpperCase();
|
s.type = s.type.toUpperCase();
|
||||||
|
|
||||||
|
@ -601,7 +611,7 @@ jQuery.extend({
|
||||||
s.hasContent = !rnoContent.test( s.type );
|
s.hasContent = !rnoContent.test( s.type );
|
||||||
|
|
||||||
// Watch for a new set of requests
|
// Watch for a new set of requests
|
||||||
if ( s.global && jQuery.active++ === 0 ) {
|
if ( fireGlobals && jQuery.active++ === 0 ) {
|
||||||
jQuery.event.trigger( "ajaxStart" );
|
jQuery.event.trigger( "ajaxStart" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -678,7 +688,7 @@ jQuery.extend({
|
||||||
// Set state as sending
|
// Set state as sending
|
||||||
state = jqXHR.readyState = 1;
|
state = jqXHR.readyState = 1;
|
||||||
// Send global event
|
// Send global event
|
||||||
if ( s.global ) {
|
if ( fireGlobals ) {
|
||||||
globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
|
globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
|
||||||
}
|
}
|
||||||
// Timeout
|
// Timeout
|
||||||
|
|
|
@ -2177,6 +2177,25 @@ test("jQuery.ajax - transitive conversions", function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("jQuery.ajax - abort in prefilter", function() {
|
||||||
|
|
||||||
|
expect( 1 );
|
||||||
|
|
||||||
|
jQuery.ajaxPrefilter(function( options, _, jqXHR ) {
|
||||||
|
if ( options.abortInPrefilter ) {
|
||||||
|
jqXHR.abort();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
strictEqual( jQuery.ajax({
|
||||||
|
abortInPrefilter: true,
|
||||||
|
error: function() {
|
||||||
|
ok( false, "error callback called" );
|
||||||
|
}
|
||||||
|
}), false, "Request was properly aborted early by the prefilter" );
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
test("jQuery.ajax - active counter", function() {
|
test("jQuery.ajax - active counter", function() {
|
||||||
ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
|
ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue