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