-
Notifications
You must be signed in to change notification settings - Fork 0
/
jModel.js
77 lines (72 loc) · 2.48 KB
/
jModel.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
(function (window) {
var Model = (function () {
function Model(opts) {
this.api = window.location.origin + '/';
this.ext = '.json';
}
Model.prototype = {
create: function (opts) {
var self = this,
link = this._clean(this.api) + this._clean(opts.action) + this._clean(this.ext);
$.ajax({
url: link,
type: 'POST',
data: opts.data,
}).done(function (data) {
if (opts.callback) {
opts.callback.call(self, data);
}
}).fail(function () {
console.log("error");
}).always(function () {
//console.log("complete");
});
},
read: function (opts) {
var self = this,
link = this._clean(this.api) + this._clean(opts.action) + this._clean(this.ext);
$.ajax({
url: link,
type: 'GET',
data: opts.data,
}).done(function (data) {
if (opts.callback) {
opts.callback.call(self, data);
}
}).fail(function () {
console.log("error");
}).always(function () {
//console.log("complete");
});
},
update: function (opts) {
return this.url;
},
delete: function (opts) {
var self = this,
link = this._clean(opts.api) + this._clean(opts.action) + this._clean(opts.ext);
$.ajax({
url: link,
type: 'POST',
data: opts.data,
}).done(function (data) {
if (opts.callback) {
opts.callback.call(self, data);
}
}).fail(function () {
console.log("error");
}).always(function () {
console.log("complete");
});
},
_clean: function (entity) {
return entity || "";
}
};
return Model;
}());
Model.initialize = function (opts) {
return new Model(opts);
};
window.Model = Model;
}(window));