bgneal@45: /**
bgneal@45: * Ajax Queue Plugin
bgneal@45: *
bgneal@45: * Homepage: http://jquery.com/plugins/project/ajaxqueue
bgneal@45: * Documentation: http://docs.jquery.com/AjaxQueue
bgneal@45: */
bgneal@45:
bgneal@45: /**
bgneal@45:
bgneal@45:
bgneal@45:
bgneal@45:
bgneal@45: */
bgneal@45: /*
bgneal@45: * Queued Ajax requests.
bgneal@45: * A new Ajax request won't be started until the previous queued
bgneal@45: * request has finished.
bgneal@45: */
bgneal@45:
bgneal@45: /*
bgneal@45: * Synced Ajax requests.
bgneal@45: * The Ajax request will happen as soon as you call this method, but
bgneal@45: * the callbacks (success/error/complete) won't fire until all previous
bgneal@45: * synced requests have been completed.
bgneal@45: */
bgneal@45:
bgneal@45:
bgneal@45: (function($) {
bgneal@45:
bgneal@45: var ajax = $.ajax;
bgneal@45:
bgneal@45: var pendingRequests = {};
bgneal@45:
bgneal@45: var synced = [];
bgneal@45: var syncedData = [];
bgneal@45:
bgneal@45: $.ajax = function(settings) {
bgneal@45: // create settings for compatibility with ajaxSetup
bgneal@45: settings = jQuery.extend(settings, jQuery.extend({}, jQuery.ajaxSettings, settings));
bgneal@45:
bgneal@45: var port = settings.port;
bgneal@45:
bgneal@45: switch(settings.mode) {
bgneal@45: case "abort":
bgneal@45: if ( pendingRequests[port] ) {
bgneal@45: pendingRequests[port].abort();
bgneal@45: }
bgneal@45: return pendingRequests[port] = ajax.apply(this, arguments);
bgneal@45: case "queue":
bgneal@45: var _old = settings.complete;
bgneal@45: settings.complete = function(){
bgneal@45: if ( _old )
bgneal@45: _old.apply( this, arguments );
bgneal@45: jQuery([ajax]).dequeue("ajax" + port );;
bgneal@45: };
bgneal@45:
bgneal@45: jQuery([ ajax ]).queue("ajax" + port, function(){
bgneal@45: ajax( settings );
bgneal@45: });
bgneal@45: return;
bgneal@45: case "sync":
bgneal@45: var pos = synced.length;
bgneal@45:
bgneal@45: synced[ pos ] = {
bgneal@45: error: settings.error,
bgneal@45: success: settings.success,
bgneal@45: complete: settings.complete,
bgneal@45: done: false
bgneal@45: };
bgneal@45:
bgneal@45: syncedData[ pos ] = {
bgneal@45: error: [],
bgneal@45: success: [],
bgneal@45: complete: []
bgneal@45: };
bgneal@45:
bgneal@45: settings.error = function(){ syncedData[ pos ].error = arguments; };
bgneal@45: settings.success = function(){ syncedData[ pos ].success = arguments; };
bgneal@45: settings.complete = function(){
bgneal@45: syncedData[ pos ].complete = arguments;
bgneal@45: synced[ pos ].done = true;
bgneal@45:
bgneal@45: if ( pos == 0 || !synced[ pos-1 ] )
bgneal@45: for ( var i = pos; i < synced.length && synced[i].done; i++ ) {
bgneal@45: if ( synced[i].error ) synced[i].error.apply( jQuery, syncedData[i].error );
bgneal@45: if ( synced[i].success ) synced[i].success.apply( jQuery, syncedData[i].success );
bgneal@45: if ( synced[i].complete ) synced[i].complete.apply( jQuery, syncedData[i].complete );
bgneal@45:
bgneal@45: synced[i] = null;
bgneal@45: syncedData[i] = null;
bgneal@45: }
bgneal@45: };
bgneal@45: }
bgneal@45: return ajax.apply(this, arguments);
bgneal@45: };
bgneal@45:
bgneal@45: })(jQuery);