-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRESTCallbacks.js
65 lines (63 loc) · 2 KB
/
RESTCallbacks.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
// Generic ajax GETs with callback parameter
function CallMethod(url, successCallback, data) {
var q = new Queue(true).add(function () {
data = data?"/"+data:"";
successCallback = successCallback || "";
$.ajax({
beforeSend: function(request) {
// Where we set the custom header. The value doesn't matter. In the future we want to set this and verify the values matches the servers'
request.setRequestHeader("Grandmas-Cookies", Math.random());
},
url:serverURL+url+data,
type: "GET",
async: true,
contentType: "application/json",
dataType: "text",
error: function(response) {
console.log(response);
},
success: successCallback
});
});
}
// API queue management for all rest calls
var Queue = (function () {
Queue.prototype.autorun = true;
Queue.prototype.running = false;
Queue.prototype.queue = [];
function Queue(autorun) {
if (typeof autorun !== "undefined") {
this.autorun = autorun;
}
this.queue = []; //initialize the queue
};
Queue.prototype.add = function (callback) {
var _this = this;
//add callback to the queue
this.queue.push(function () {
var finished = callback();
if (typeof finished === "undefined" || finished) {
// if callback returns `false`, then you have to
// call `next` somewhere in the callback
_this.dequeue();
}
});
if (this.autorun && !this.running) {
// if nothing is running, then start the engines!
this.dequeue();
}
return this; // for chaining fun!
};
Queue.prototype.dequeue = function () {
this.running = false;
//get the first element off the queue
var shift = this.queue.shift();
if (shift) {
this.running = true;
shift();
}
return shift;
};
Queue.prototype.next = Queue.prototype.dequeue;
return Queue;
})();