-
Notifications
You must be signed in to change notification settings - Fork 7
/
ajax-action-wrapper.js
139 lines (123 loc) · 3.55 KB
/
ajax-action-wrapper.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var AjawV1 = window.AjawV1 || {};
AjawV1.AjaxAction = (function () {
"use strict";
function AjawAjaxAction(action, properties) {
this.action = action;
this.ajaxUrl = properties['ajaxUrl'];
this.nonce = properties['nonce'];
this.requiredMethod = (typeof properties['method'] !== 'undefined') ? properties['method'] : null;
}
/**
* Send a POST request.
*
* @param {Object} params
* @param {Function} success
* @param {Function} [error]
*/
AjawAjaxAction.prototype.post = function (params, success, error) {
return this.request(params, success, error, 'POST');
};
/**
* Send a GET request.
*
* @param {Object} params
* @param {Function} success
* @param {Function} [error]
*/
AjawAjaxAction.prototype.get = function(params, success, error) {
return this.request(params, success, error, 'GET');
};
/**
* Send an AJAX request using the specified HTTP method.
*
* @param {Object} params
* @param {Function} success
* @param {Function} [error]
* @param {String} [method]
*/
AjawAjaxAction.prototype.request = function(params, success, error, method) {
if (typeof params === 'function') {
//It looks like "params" was omitted and the first argument is actually the success callback.
//Shift all arguments left one step. The reverse order is due to argument binding shenanigans.
method = arguments[2];
error = arguments[1];
success = arguments[0];
params = {};
}
if (typeof params === 'undefined') {
params = {};
} else if (typeof params !== 'object') {
//While jQuery accepts request data in object and string form, this library only supports objects.
throw 'Data that\'s to be sent to the server must be an object, not ' + (typeof params);
}
if (typeof method === 'undefined') {
method = this.requiredMethod || 'POST';
}
if (this.requiredMethod && (method !== this.requiredMethod)) {
throw 'Wrong HTTP method. This action requires ' + this.requiredMethod;
}
//noinspection JSUnusedGlobalSymbols
return jQuery.ajax(
this.ajaxUrl,
{
method: method,
data: this.prepareRequestParams(params),
success: function(data, textStatus, jqXHR) {
if (success) {
success(data, textStatus, jqXHR);
}
},
error: function(jqXHR, textStatus, errorThrown) {
var data = jqXHR.responseText;
if (typeof jqXHR['responseJSON'] !== 'undefined') {
data = jqXHR['responseJSON'];
} else if (typeof jqXHR['responseXML'] !== 'undefined') {
data = jqXHR['responseXML'];
}
if (error) {
error(data, textStatus, jqXHR, errorThrown);
}
}
}
);
};
AjawAjaxAction.prototype.prepareRequestParams = function(params) {
if (params === null) {
params = {};
}
params['action'] = this.action;
if (this.nonce !== null) {
params['_ajax_nonce'] = this.nonce;
}
return params;
};
return AjawAjaxAction;
}());
AjawV1.actionRegistry = (function() {
var actions = {};
return {
/**
*
* @param {String} actionName
* @return {AjawAjaxAction}
*/
get: function(actionName) {
if (actions.hasOwnProperty(actionName)) {
return actions[actionName];
}
return null;
},
add: function(actionName, properties) {
actions[actionName] = new AjawV1.AjaxAction(actionName, properties);
}
}
})();
/**
* Get a registered action wrapper.
*
* @param {string} action
* @return {AjawAjaxAction|null}
*/
AjawV1.getAction = function(action) {
return this.actionRegistry.get(action);
};