jquery.ajaxQueue.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Ajax Queue Plugin
  3. *
  4. * Homepage: http://jquery.com/plugins/project/ajaxqueue
  5. * Documentation: http://docs.jquery.com/AjaxQueue
  6. */
  7. /**
  8. <script>
  9. $(function(){
  10. jQuery.ajaxQueue({
  11. url: "test.php",
  12. success: function(html){ jQuery("ul").append(html); }
  13. });
  14. jQuery.ajaxQueue({
  15. url: "test.php",
  16. success: function(html){ jQuery("ul").append(html); }
  17. });
  18. jQuery.ajaxSync({
  19. url: "test.php",
  20. success: function(html){ jQuery("ul").append("<b>"+html+"</b>"); }
  21. });
  22. jQuery.ajaxSync({
  23. url: "test.php",
  24. success: function(html){ jQuery("ul").append("<b>"+html+"</b>"); }
  25. });
  26. });
  27. </script>
  28. <ul style="position: absolute; top: 5px; right: 5px;"></ul>
  29. */
  30. /*
  31. * Queued Ajax requests.
  32. * A new Ajax request won't be started until the previous queued
  33. * request has finished.
  34. */
  35. /*
  36. * Synced Ajax requests.
  37. * The Ajax request will happen as soon as you call this method, but
  38. * the callbacks (success/error/complete) won't fire until all previous
  39. * synced requests have been completed.
  40. */
  41. (function(jQuery) {
  42. var ajax = jQuery.ajax;
  43. var pendingRequests = {};
  44. var synced = [];
  45. var syncedData = [];
  46. jQuery.ajax = function(settings) {
  47. // create settings for compatibility with ajaxSetup
  48. settings = jQuery.extend(settings, jQuery.extend({}, jQuery.ajaxSettings, settings));
  49. var port = settings.port;
  50. switch(settings.mode) {
  51. case "abort":
  52. if ( pendingRequests[port] ) {
  53. pendingRequests[port].abort();
  54. }
  55. return pendingRequests[port] = ajax.apply(this, arguments);
  56. case "queue":
  57. var _old = settings.complete;
  58. settings.complete = function(){
  59. if ( _old )
  60. _old.apply( this, arguments );
  61. jQuery([ajax]).dequeue("ajax" + port );;
  62. };
  63. jQuery([ ajax ]).queue("ajax" + port, function(){
  64. ajax( settings );
  65. });
  66. return;
  67. case "sync":
  68. var pos = synced.length;
  69. synced[ pos ] = {
  70. error: settings.error,
  71. success: settings.success,
  72. complete: settings.complete,
  73. done: false
  74. };
  75. syncedData[ pos ] = {
  76. error: [],
  77. success: [],
  78. complete: []
  79. };
  80. settings.error = function(){ syncedData[ pos ].error = arguments; };
  81. settings.success = function(){ syncedData[ pos ].success = arguments; };
  82. settings.complete = function(){
  83. syncedData[ pos ].complete = arguments;
  84. synced[ pos ].done = true;
  85. if ( pos == 0 || !synced[ pos-1 ] )
  86. for ( var i = pos; i < synced.length && synced[i].done; i++ ) {
  87. if ( synced[i].error ) synced[i].error.apply( jQuery, syncedData[i].error );
  88. if ( synced[i].success ) synced[i].success.apply( jQuery, syncedData[i].success );
  89. if ( synced[i].complete ) synced[i].complete.apply( jQuery, syncedData[i].complete );
  90. synced[i] = null;
  91. syncedData[i] = null;
  92. }
  93. };
  94. }
  95. return ajax.apply(this, arguments);
  96. };
  97. })((typeof window.jQuery == 'undefined' && typeof window.django != 'undefined')
  98. ? django.jQuery
  99. : jQuery
  100. );