-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.restly.js
258 lines (221 loc) · 6.31 KB
/
jquery.restly.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// ==ClosureCompiler==
// @compilation_level WHITESPACE_ONLY
// @output_file_name jquery.restly.min.js
// ==/ClosureCompiler==
/* =========================================================
* jquery.restly.js
* ========================================================= */
/**
* GET /api/vX/resource index resource.index
* POST /api/vX/resource store resource.store
* GET /api/vX/resource/{id} show resource.show
* PUT/PATCH /api/vX/resource/{id} update resource.update
* DELETE /api/vX/resource/{id} destroy resource.destroy
*/
(function ( $ ) {
$.fn.restly = function(options)
{
var opts = $.extend( {}, $.fn.restly.defaults, options );
return this.each(function()
{
var element = $(this);
element.click(function(event)
{
event.preventDefault();
var confirmMessage = element.data('confirm');
if(bootbox && confirmMessage)
{
bootbox.confirm(confirmMessage, function(confirmed)
{
if(confirmed) $.fn.restly.processClick(element, opts);
});
}
else
{
$.fn.restly.processClick(element, opts);
}
});
return this;
});
};
/**
* Process the clicked element.
*
* @param element
* @param options
*/
$.fn.restly.processClick = function(element, options)
{
var opts = $.extend( {}, options, element.data() );
$.fn.restly.send(opts);
}
/**
* Sends the request using the given options.
*
* @param opts
*/
$.fn.restly.send = function(options)
{
var opts = $.extend( {}, $.fn.restly.defaults, options );
if( ! opts['method'] ) throw new Error('No method given');
if( ! opts['endpoint'] ) throw new Error('No endpoint given');
if( ! opts['resource'] ) throw new Error('No resource given');
opts['method'] = opts['method'].toUpperCase();
if( (opts['method'] == 'DELETE' || opts['method'] == 'PUT' || opts['method'] == 'PATCH') && ! opts['id'])
throw new Error('No resource id given');
if(typeof opts['redirect'] == 'string')
{
opts['success'] = function(data, textStatus, jqXHR) {
window.location.href = opts['redirect'];
}
}
else if (typeof opts['success'] == 'string')
{
var successCallback = window[opts['success']];
opts['success'] = function(data, textStatus, jqXHR) {
successCallback(data, textStatus, jqXHR);
}
}
if(typeof opts['error'] == 'string')
{
var errorCallback = window[opts['error']];
opts['error'] = function(jqXHR, textStatus, errorThrown)
{
errorCallback(jqXHR, textStatus, errorThrown);
}
}
if (opts['method'] == 'POST' || opts['method'] == 'PUT' || opts['method'] == 'PATCH')
{
opts['data'] = JSON.stringify($.fn.restly.prepareAdditionalFieldData(opts['fields']));
}
opts['url'] = $.fn.restly.buildApiUrl(opts);
$.ajax(opts);
}
/**
* Shorthand method for a get request
*
* @param options
*/
$.fn.restly.get = function(options)
{
options['method'] = 'GET';
$.fn.restly.send(options);
}
/**
* Shorthand method for a delete request
*
* @param options
*/
$.fn.restly.delete = function(options)
{
options['method'] = 'DELETE';
$.fn.restly.send(options);
}
/**
* Shorthand method for a post request
*
* @param options
*/
$.fn.restly.post = function(options)
{
options['method'] = 'POST';
$.fn.restly.send(options);
}
/**
* Shorthand method for a put request
*
* @param options
*/
$.fn.restly.put = function(options)
{
options['method'] = 'PUT';
$.fn.restly.send(options);
}
/**
* Shorthand method for a patch request
*
* @param options
*/
$.fn.restly.patch = function(options)
{
options['method'] = 'PATCH';
$.fn.restly.send(options);
}
/**
* Fetches data from additional fields.
*
* @param fields
* @returns {{}}
*/
$.fn.restly.prepareAdditionalFieldData = function(fields)
{
var data = {};
if(fields)
{
$.each(fields.split(','), function(index, fieldName)
{
data[fieldName] = $('[name=' + fieldName + ']').val();
});
}
return data;
}
/**
* Builds the request url.
*
* @param options
* @returns string
*/
$.fn.restly.buildApiUrl = function(options)
{
var requestUrl = options['endpoint'].concat('/').concat(options['resource']);
if ($.isNumeric(options['id']))
{
requestUrl = requestUrl.concat('/').concat(options['id']);
}
return requestUrl;
}
/**
* Processes a rest error response from an ajax request.
*
* @param jqXHR
* @param textStatus
* @param errorThrown
*/
$.fn.restly.processErrorResponse = function(jqXHR, textStatus, errorThrown)
{
if(toastr)
{
if(jqXHR.status == '500')
{
toastr.error(jqXHR.statusText);
}
else if(jqXHR.responseText)
{
var response = $.parseJSON(jqXHR.responseText);
toastr.error(response.userMessage);
}
else
{
toastr.error('Invalid response.');
}
}
}
/**
* Declaration of default options (ajax request options).
*/
$.fn.restly.defaults = {
endpoint: null,
resource: null,
id: null,
contentType: 'application/json; charset=utf-8',
method: 'GET',
dataType: 'json',
fields: null,
redirect: null,
success: jQuery.noop,
error: $.fn.restly.processErrorResponse
};
if(typeof window['restly'] != 'function') {
window['restly'] = $.fn.restly;
}
}( jQuery ));