-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafity.ajax.js
executable file
·63 lines (56 loc) · 1.14 KB
/
crafity.ajax.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
(function (crafity, $) {
"use strict";
var ajaxCallCount = 0;
/**
*
* @param url
* @param [success] - optional
* @param [error] - optional
*/
crafity.ajax = function (url, success, error) {
// Normalize parameters
var type = "GET", data;
if (typeof url === 'object' && !success && !error) {
success = url.success;
error = url.error;
type = url.type || type;
data = url.data || data;
url = url.url;
}
if (!success) {
success = function () {
return false;
};
}
if (!error) {
error = function () {
return false;
};
}
function startLoading() {
ajaxCallCount += 1;
$("html").addClass("loading");
}
function stopLoading() {
ajaxCallCount -= 1;
if (ajaxCallCount === 0) {
$("html").removeClass("loading");
}
}
startLoading();
$.ajax({
url: url,
type: type,
data: data,
contentType : !data ? "text/html" : undefined,
success: function (data) {
stopLoading();
success.apply(this, arguments);
},
error: function (err) {
stopLoading();
error.apply(this, arguments);
}
});
};
}(window.crafity = window.crafity || {}, jQuery));