-
Notifications
You must be signed in to change notification settings - Fork 0
/
jQuery.CRUD.js
165 lines (136 loc) · 5.28 KB
/
jQuery.CRUD.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
(function ($, undefined) {
if (!$) {
throw "Dependency: jQuery is not defined. Check javascript file imports.";
}
var defaults = {
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
data: null
},
// CRUD
operations = {
$create: {
type: "POST",
processData: true
},
$read: {
type: "GET",
processData: false
},
$update: {
type: "PUT",
processData: true
},
$delete: {
type: "DELETE",
processData: false
}
};
function prepareUri(url, data) {
// replaces tokens with their actual values and removes the replaced values from the data array
// e.g. url: "service/{route}?tag={tag}", data: {route:"bookmarks", tag:"public"} => service/bookmarks?tag=public
var k,
v,
u;
for (k in data) {
v = data[k];
u = url.replace('{' + k + '}', v);
if (u !== url) {
url = u;
delete data[k];
}
}
return url;
}
function prepareData() {
// handle JSON serialization from Object
if (this.data && typeof this.data !== "string") {
if ($.isEmptyObject(this.data)) {
this.data = null;
} else {
this.data = JSON.stringify(this.data);
}
}
}
function prepare(uri, data, callback, options) {
if (typeof uri !== "string" || !uri) {
throw "Parameter: 'uri' must be of type string";
}
if (options && $.isPlainObject(options) === false) {
throw "Parameter: 'options' must be of type object";
}
options = options || {};
// merge default settings and the current operation (crud operation) settings
var o = $.extend({}, defaults, operations[this]);
// determine if data is a function or an object
// 1. function - $.Read("uri", OnSuccess)
// 2. object - $.Read("uri", {} )
if ($.isFunction(data)) {
// determine if the callback is an object
// 1. object - $.Read("uri", OnSuccess, {} )
if ($.isPlainObject(callback)) {
options = callback;
}
callback = data;
data = null;
} else {
uri = prepareUri(uri, data);
options.data = data.data || data;
// determine if callback is a function or an object
// 1. object - $.Read("uri", {}, {} )
// 2. function - $.Read("uri", {}, OnSuccess )
if ($.isPlainObject(callback)) {
options = callback;
callback = null;
}
}
o.url = uri;
if (callback) {
o.success = callback;
}
if (options) {
prepareData.apply(options);
$.extend(o, options);
}
return o;
}
function execute(options) {
return $.ajax(options);
}
// extend jQuery
$.extend({
Create: function (uri, data, callback, options) {
///<summary>Create operation (uses POST by default)</summary>
///[String] the uri template (required)
///[Object|Function] token replacements and/or JSON object OR the $.ajax success callback (optional)
///[Object|Function] the $.ajax success callback OR the $.ajax settings (optional)
///[Object] the $.ajax settings (optional)
return execute(prepare.apply("$create", arguments));
},
Read: function (uri, data, callback, options) {
///<summary>Read operation (uses GET by default)</summary>
///[String] the uri template (required)
///[Object|Function] token replacements and/or JSON object OR the $.ajax success callback (optional)
///[Object|Function] the $.ajax success callback OR the $.ajax settings (optional)
///[Object] the $.ajax settings (optional)
return execute(prepare.apply("$read", arguments));
},
Update: function (uri, data, callback, options) {
///<summary>Update operation (uses PUT by default)</summary>
///[String] the uri template (required)
///[Object|Function] token replacements and/or JSON object OR the $.ajax success callback (optional)
///[Object|Function] the $.ajax success callback OR the $.ajax settings (optional)
///[Object] the $.ajax settings (optional)
return execute(prepare.apply("$update", arguments));
},
Delete: function (uri, data, callback, options) {
///<summary>Delete operation (uses DELETE by default)</summary>
///[String] the uri template (required)
///[Object|Function] token replacements and/or JSON object OR the $.ajax success callback (optional)
///[Object|Function] the $.ajax success callback OR the $.ajax settings (optional)
///[Object] the $.ajax settings (optional)
return execute(prepare.apply("$delete", arguments));
}
});
} (jQuery));