-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax-utilities.js
116 lines (98 loc) · 3.27 KB
/
ajax-utilities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Utility AJAX functions
* @author V.Radev <mail@radev.info>
*/
var Ajax = function (){
this.requests = [];
this.queue = true;
this.queueRunning = false;
return this;
};
Ajax.prototype.send = function(params){
var self = this,
defaults = {
method: 'POST',
url: '',
data: {},
beforeSend: function(){}
};
params = jQuery.extend(defaults, params);
//Overwrite these callback hooks
params.error = function(){};
params.success = function(){};
//If queue is disabled overwrite complete()
if ( !self.queue ){
params.complete = function(){};
}
//If queue is enabled
if ( self.queue ){
self.queueRequest(params);
return self.runQueue();
} else {
return jQuery.ajax(params);
}
};
Ajax.prototype.post = function(ajaxUrl, ajaxData) {
ajaxUrl = typeof ajaxUrl !== 'undefined' ? ajaxUrl : '';
ajaxData = typeof ajaxData !== 'undefined' ? ajaxData : {};
return this.send({ method: 'POST', url: ajaxUrl, data: ajaxData });
};
Ajax.prototype.queueRequest = function(params) {
var obj = {};
obj.params = params;
obj.active = false;
this.requests.push(obj);
};
Ajax.prototype.runQueue = function() {
var self = this,
i,
deferred;
//If there is request running currently
if ( self.queueRunning ){
deferred = $.Deferred();
//Loop all requests
for (i = 0; i < self.requests.length; i++) {
//If the request is not active
if ( self.requests[i].hasOwnProperty('active') && !self.requests[i].active ){
//If the request has not been handled by this loop
if ( !self.requests[i].hasOwnProperty('handled') || !self.requests[i].handled ){
//Add the deferred to the request
self.requests[i].deferred = deferred;
//Mark request as handled
self.requests[i].handled = true;
}
}
}
//Return a promise so you can be able to hook done(), fail() and always()
return deferred.promise();
}//END if queue is running
//If you have requests
if( self.requests.length ) {
//Queue is running
self.queueRunning = true;
//Set this request to active
self.requests[0].active = true;
//Modify the complete()
self.requests[0].params.complete = function() {
//Remove this request
self.requests.shift();
//Make queue available
self.queueRunning = false;
//Continue queue
self.runQueue.apply(self, []);//This will check the queue again when the request is done
};
//If request has a deferred
if ( self.requests[0].hasOwnProperty('deferred') ){
//Run request and notify the deferred
jQuery.ajax(self.requests[0].params)
.done(function(data) {
self.requests[0].deferred.resolve(data);
})
.fail(function(data){
self.requests[0].deferred.reject(data);
});
return;
}
return jQuery.ajax(self.requests[0].params);
}//END if requests
};//END runQueue()