1 |
2687a6fa
|
Scott Ullrich
|
var AjaxQueue = {
|
2 |
|
|
batchSize: 1, //No.of simultaneous AJAX requests allowed, Default : 1
|
3 |
|
|
urlQueue: [], //Request URLs will be pushed into this array
|
4 |
1c3351bd
|
Vinicius Coque
|
elementsQueue: [], //Element IDs of elements to be updated on completion of a request
|
5 |
2687a6fa
|
Scott Ullrich
|
optionsQueue: [], //Request options will be pushed into this array
|
6 |
1c3351bd
|
Vinicius Coque
|
currentRequest: null,
|
7 |
2687a6fa
|
Scott Ullrich
|
setBatchSize: function(bSize){ //Method to set a different batch size. Recommended: Set batchSize before making requests
|
8 |
|
|
this.batchSize = bSize;
|
9 |
|
|
},
|
10 |
1c3351bd
|
Vinicius Coque
|
push: function(url, options, elementID){ //Push the request in the queue. elementID is optional and required only for Ajax requests that updates the element
|
11 |
2687a6fa
|
Scott Ullrich
|
this.urlQueue.push(url);
|
12 |
|
|
this.optionsQueue.push(options);
|
13 |
|
|
if(elementID!=null){
|
14 |
|
|
this.elementsQueue.push(elementID);
|
15 |
|
|
} else {
|
16 |
|
|
this.elementsQueue.push("NOTSPECIFIED");
|
17 |
|
|
}
|
18 |
|
|
|
19 |
|
|
this._processNext();
|
20 |
|
|
},
|
21 |
|
|
_processNext: function() { // Method for processing the requests in the queue. Private method. Don't call it explicitly
|
22 |
1c3351bd
|
Vinicius Coque
|
if(this.currentRequest == null && this.urlQueue.length > 0) // Check if the currently processing request count is less than batch size
|
23 |
|
|
{
|
24 |
|
|
// Call jQuery.ajax on the first item in the queue and remove it from the queue
|
25 |
|
|
AjaxQueue.currentRequest = jQuery.ajax(AjaxQueue.urlQueue.shift(), AjaxQueue.optionsQueue.shift());
|
26 |
|
|
AjaxQueue.currentRequest.complete( function() {
|
27 |
|
|
//Call AjaxQueue._processNext on completion ( success / failure) of this AJAX request.
|
28 |
|
|
AjaxQueue.currentRequest = null;
|
29 |
|
|
AjaxQueue._processNext();
|
30 |
|
|
});
|
31 |
|
|
if(this.elementsQueue[0]=="NOTSPECIFIED") { //Check if an elementID was specified
|
32 |
|
|
// If no ElementID was specified remove the first item from the queue
|
33 |
2687a6fa
|
Scott Ullrich
|
var junk = AjaxQueue.elementsQueue.shift();
|
34 |
|
|
} else {
|
35 |
1c3351bd
|
Vinicius Coque
|
// If ElementID was specified update the first item in the queue and remove it from the queue
|
36 |
|
|
AjaxQueue.currentRequest.success( function(data) {
|
37 |
|
|
jQuery(AjaxQueue.elementsQueue.shift()).html(data);
|
38 |
|
|
});
|
39 |
|
|
}
|
40 |
2687a6fa
|
Scott Ullrich
|
}
|
41 |
|
|
}
|
42 |
|
|
};
|