-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjkquery.ajax.js
More file actions
140 lines (111 loc) · 3.47 KB
/
jkquery.ajax.js
File metadata and controls
140 lines (111 loc) · 3.47 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
(function (jkQuery, XMLHttpRequest) {
'use strict';
/* jkQuery.ajax()
*
* Expose AJAX constructor as a jkQuery method
*
* @public
* @param {string} file
* @returns {object} new AJAX
*
*/
jkQuery.ajax = function (file) {
return new AJAX(file);
};
/***/
/* jkQuery.ajax()
*
* Construct a new AJAX request
*
* @public
* @param {string} file
* @returns {object} jkQuery.ajax()
*
*/
var AJAX = function (file) {
// Validate file argument
this._file = (typeof file === 'string') ? file : false;
// Empty function to default to if a valid promise is not provided
this._onError = function () { return false; };
return this;
};
/***/
/* jkQuery.ajax().request()
*
* Fetch and process the requested file
*
* @public
* @param {string} file
* @param {function} successCallback
* @param {function} errorCallback
* @returns {object} jkQuery.ajax()
*
*/
AJAX.prototype.request = function (file, successCallback, errorCallback) {
// Create new XMLHttpRequest
var httpRequest = new XMLHttpRequest();
// Throw error and die if XMLHttpRequest or file path is not available
if (!httpRequest || !file || !successCallback) {
errorCallback.call(httpRequest, httpRequest);
return false;
}
// Attach error callback to httpRequest's native error handler
httpRequest.onerror = function () {
return errorCallback.call(httpRequest, httpRequest);
};
// Listen for success state and deliever the success callback
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
var response = httpRequest.responseText;
if (httpRequest.getResponseHeader('content-type').indexOf('json') > -1) {
response = JSON.parse(httpRequest.responseText);
}
return successCallback.call(httpRequest, response);
}
// Throw error if file did not return a 200 status
return errorCallback.call(httpRequest, httpRequest);
}
};
// Create and send the request
httpRequest.open('GET', file);
httpRequest.send();
return this;
};
/***/
/* jkQuery.ajax().success()
*
* Employ the success function as a promise and create a request using the
* file specified in the AJAX constructor.
*
* @public
* @param {function} callback
* @returns {object} jkQuery.ajax().request()
*
*/
AJAX.prototype.success = function (callback) {
if (typeof callback === 'function') {
this._onSuccess = callback;
return this.request(this._file, this._onSuccess, this._onError);
}
return this;
};
/***/
/* jkQuery.ajax().error()
*
* Accept a user error function as a promise.
*
* @public
* @param {string} file
* @param {function} callback
* @returns {object} jkQuery.ajax()
*
*/
AJAX.prototype.error = function (callback) {
if (typeof callback === 'function') {
this._onError = callback;
}
return this;
};
/***/
})(window.jkQuery, window.XMLHttpRequest);