Made some tweaks to the different AJAX callbacks.

This commit is contained in:
John Resig 2006-06-22 22:25:03 +00:00
parent 0fd3648d45
commit 2d9979c73f

View file

@ -83,7 +83,7 @@ if ( jQuery.browser == "msie" )
// Attach a bunch of functions for handling common AJAX events // Attach a bunch of functions for handling common AJAX events
(function(){ (function(){
var e = "ajaxStart.ajaxComplete.ajaxError.ajaxSuccess".split(','); var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess".split(',');
for ( var i = 0; i < e.length; i++ ){ (function(){ for ( var i = 0; i < e.length; i++ ){ (function(){
var o = e[i]; var o = e[i];
@ -106,6 +106,10 @@ jQuery.ajax = function( type, url, data, ret ) {
type = type.type; type = type.type;
} }
// Watch for a new set of requests
if ( ! jQuery.ajax.active++ )
jQuery.event.trigger( "ajaxStart" );
// Create the request object // Create the request object
var xml = new XMLHttpRequest(); var xml = new XMLHttpRequest();
@ -125,23 +129,8 @@ jQuery.ajax = function( type, url, data, ret ) {
// Wait for a response to come back // Wait for a response to come back
xml.onreadystatechange = function(){ xml.onreadystatechange = function(){
// Socket is openend // The transfer is complete and the data is available
if ( xml.readyState == 1 ) {
// Increase counter
jQuery.ajax.active++;
// Show 'loader'
jQuery.event.trigger( "ajaxStart" );
}
// Socket is closed and data is available
if ( xml.readyState == 4 ) { if ( xml.readyState == 4 ) {
// Hide loader if needed
if ( ! --jQuery.ajax.active ) {
jQuery.event.trigger( "ajaxComplete" );
jQuery.ajax.active = 0
}
// Make sure that the request was successful // Make sure that the request was successful
if ( jQuery.httpSuccess( xml ) ) { if ( jQuery.httpSuccess( xml ) ) {
@ -160,6 +149,13 @@ jQuery.ajax = function( type, url, data, ret ) {
jQuery.event.trigger( "ajaxError" ); jQuery.event.trigger( "ajaxError" );
} }
// The request was completed
jQuery.event.trigger( "ajaxComplete" );
// Handle the global AJAX counter
if ( ! --jQuery.ajax.active )
jQuery.event.trigger( "ajaxStop" );
// Process result // Process result
if ( ret ) ret(xml); if ( ret ) ret(xml);
} }