jquery.ajaxQueue.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*! jQuery Ajax Queue - v0.1.1 - 2013-01-16
  2. * https://github.com/gnarf37/jquery-ajaxQueue
  3. * Copyright (c) 2013 Corey Frang; Licensed MIT */
  4. (function($) {
  5. // jQuery on an empty object, we are going to use this as our Queue
  6. var ajaxQueue = $({});
  7. $.ajaxQueue = function( ajaxOpts ) {
  8. var jqXHR,
  9. dfd = $.Deferred(),
  10. promise = dfd.promise();
  11. // run the actual query
  12. function doRequest( next ) {
  13. jqXHR = $.ajax( ajaxOpts )
  14. .done( dfd.resolve )
  15. .fail( dfd.reject )
  16. .then( next, next );
  17. }
  18. // queue our ajax request
  19. ajaxQueue.queue( doRequest );
  20. // add the abort method
  21. promise.abort = function( statusText ) {
  22. // proxy abort to the jqXHR if it is active
  23. if ( jqXHR ) {
  24. return jqXHR.abort( statusText );
  25. }
  26. // if there wasn't already a jqXHR we need to remove from queue
  27. var queue = ajaxQueue.queue(),
  28. index = $.inArray( doRequest, queue );
  29. if ( index > -1 ) {
  30. queue.splice( index, 1 );
  31. }
  32. // and then reject the deferred
  33. dfd.rejectWith( ajaxOpts.context || ajaxOpts, [ promise, statusText, "" ] );
  34. return promise;
  35. };
  36. return promise;
  37. };
  38. })(jQuery);