Skip to content

Commit

Permalink
Fixes #4964. Adds a statusCode object together with a new statusCode …
Browse files Browse the repository at this point in the history
…method on the jXHR object (deferred behaviour). They accept a map of statusCode/callback(s). Callbacks are fired when the status code of the response correponds to the key (as a success or an error callback depending on how the request completed). Unit tests added.
  • Loading branch information
jaubourg committed Jan 13, 2011
1 parent 5795615 commit 44fc87f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/ajax.js
Expand Up @@ -265,6 +265,8 @@ jQuery.extend({
// Deferreds // Deferreds
deferred = jQuery.Deferred(), deferred = jQuery.Deferred(),
completeDeferred = jQuery._Deferred(), completeDeferred = jQuery._Deferred(),
// Status-dependent callbacks
statusCode = s.statusCode || {},
// Headers (they are sent all at once) // Headers (they are sent all at once)
requestHeaders = {}, requestHeaders = {},
// Response headers // Response headers
Expand Down Expand Up @@ -520,6 +522,9 @@ jQuery.extend({
deferred.fireReject( callbackContext , [ jXHR , statusText , error ] ); deferred.fireReject( callbackContext , [ jXHR , statusText , error ] );
} }


// Status-dependent callbacks
jXHR.statusCode( statusCode );

if ( s.global ) { if ( s.global ) {
globalEventContext.trigger( "ajax" + ( isSuccess ? "Success" : "Error" ) , globalEventContext.trigger( "ajax" + ( isSuccess ? "Success" : "Error" ) ,
[ jXHR , s , isSuccess ? success : error ] ); [ jXHR , s , isSuccess ? success : error ] );
Expand All @@ -543,6 +548,28 @@ jQuery.extend({
jXHR.error = jXHR.fail; jXHR.error = jXHR.fail;
jXHR.complete = completeDeferred.done; jXHR.complete = completeDeferred.done;


// Status-dependent callbacks
jXHR.statusCode = function( map ) {
if ( map ) {
var resolved = jXHR.isResolved(),
tmp;
if ( resolved || jXHR.isRejected() ) {
tmp = map[ jXHR.status ];
if ( tmp ) {
if ( map === statusCode ) {
delete statusCode[ jXHR.status ];
}
jXHR[ resolved ? "done" : "fail" ]( tmp );
}
} else {
for( tmp in map ) {
statusCode[ tmp ] = [ statusCode[ tmp ] , map[ tmp ] ];
}
}
}
return this;
};

// Remove hash character (#7531: and string promotion) // Remove hash character (#7531: and string promotion)
s.url = ( "" + s.url ).replace( rhash , "" ); s.url = ( "" + s.url ).replace( rhash , "" );


Expand Down
70 changes: 70 additions & 0 deletions test/unit/ajax.js
Expand Up @@ -1915,6 +1915,76 @@ test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
ok( success, "document.location did not generate exception" ); ok( success, "document.location did not generate exception" );
}); });


test( "jQuery.ajax - statusCode" , function() {

var count = 10;

expect( 16 );
stop();

function countComplete() {
if ( ! --count ) {
start();
}
}

function createStatusCodes( name , isSuccess ) {
name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
return {
200: function() {
ok( isSuccess , name );
},
404: function() {
ok( ! isSuccess , name );
}
}
}

jQuery.each( {
"data/name.html": true,
"data/someFileThatDoesNotExist.html": false
} , function( uri , isSuccess ) {

jQuery.ajax( url( uri ) , {
statusCode: createStatusCodes( "in options" , isSuccess ),
complete: countComplete
});

jQuery.ajax( url( uri ) , {
complete: countComplete
}).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );

jQuery.ajax( url( uri ) , {
complete: function(jXHR) {
jXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
countComplete();
}
});

jQuery.ajax( url( uri ) , {
complete: function(jXHR) {
setTimeout( function() {
jXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
countComplete();
} , 100 );
}
});

jQuery.ajax( url( uri ) , {
statusCode: createStatusCodes( "all (options)" , isSuccess ),
complete: function(jXHR) {
jXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
setTimeout( function() {
jXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
countComplete();
} , 100 );
}
}).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );

});

});

} }


//} //}

0 comments on commit 44fc87f

Please sign in to comment.