-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiobject.js
More file actions
167 lines (150 loc) · 6.03 KB
/
apiobject.js
File metadata and controls
167 lines (150 loc) · 6.03 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
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
/**
*
* API OBJECT
* @author Victor Aurélio
*/
ApiObject = function(data){
for(property in data){
this[property] = data[property];
}
this._init();
};
ApiObject.prototype = {
seq: [], // Composite keys are acceptable. Default: sequence primary keys
name: "Name of object", // To show erros and default to url api
attr: [], // Atributtes of object
u_insert: null,
u_update: null,
u_get: null,
u_get_table: null,
u_delete: null,
tableName: null,
tableObject: null,
selectToTable: null,
_defaultInsertSuccess: "Inserido com sucesso!",
_defaultInsertError: "Error ao inserir",
_init: function(){
this.selectToTable = 'select.toTable' + this.name.capitalizeFirstLetter();
this.tableName = "table#" + this.name;//default: table#nameOfObjectInTable
this.u_insert = (isNotNull(this.u_insert) ? this.u_insert : this.name);
this.u_update = (isNotNull(this.u_update) ? this.u_update : this.name);
this.u_get = (isNotNull(this.u_get) ? this.u_get : this.name);
this.u_get_table = (isNotNull(this.u_get_table) ? this.u_get_table : this.name)+ '/table';// If exists a select type
this.u_delete = (isNotNull(this.u_delete) ? this.u_delete : this.name);
},
get: function(){
return $.ajax({
context: this,
url: getUrlRequest(this.u_get + ""),
type: "GET",
error: function (xhr) {
console.log("Error on get for " + this.u_get);
},
success: function(result){
this.attr = result['data'];
}
});
},
getFull: function(){
return $.ajax({
context: this,
url: getUrlRequest(this.u_get + '/full'),
type: "GET",
data: {seq: this.seq},
error: function (xhr) {
console.log("Error on getFull for " + this.u_get+'/full');
},
success: function(result){
this.attr = result['data'];
if(isNotNull(this.attr[0])){//A vector ?
for(property in this.attr[0]){//Adding property inside vetor to attr
this.attr[property] = this.attr[0][property];
}
}
delete this.attr[0];//Delete vetor
}
});
},
getForTable: function(){
if(typeof(this.tableObject) === "function"){
this.tableObject();
}else{
this.tableObject.clear().draw();
this.tableObject.ajax.url(getUrlRequest(this.u_get_table + (isNotNull($(this.params())) ? "/" + JSON.stringify( this.params() ): "")));
console.log(this.tableObject.ajax.url());
this.tableObject.ajax.reload();
this.tableObject.draw(true);
}
},
insert: function(name, success, error, type, excludes){
//everything on attr will be passed in ajax's data option, but we can excludes some of them or everybody
name = typeof name !== 'undefined' ? name : this.name;
success = typeof success !== 'undefined' ? success : this._defaultInsertSuccess;
error = typeof error !== 'undefined' ? error : this._defaultInsertError;
type = typeof type !== 'undefined' ? type : "POST";
if(type != "POST" && !type)
throw new Error("Only POST request are acceptable");
var att = this.attr;
if(excludes){
for(var i = 0; i < excludes.length; i++){
if((index = att.indexOf(excludes[i])) != -1)
att.splice(index, 1);
}
}
return $.ajax({url: getUrlRequest(name), type: type, data: att, success: success, error: error});
},
update: function(name, success, error, type, excludes){
//everything on attr will be passed in ajax's data option, but we can excludes some of them or everybody
name = typeof name !== 'undefined' ? name : this.name;
success = typeof success !== 'undefined' ? success : this._defaultInsertSuccess;
error = typeof error !== 'undefined' ? error : this._defaultInsertError;
type = typeof type !== 'undefined' ? type : "PUT";
if(type != "PUT" && !type)
throw new Error("Only PUT request are acceptable");
var att = this.attr;
if(excludes){
for(var i = 0; i < excludes.length; i++){
if((index = att.indexOf(excludes[i])) != -1)
att.splice(index, 1);
}
}
return $.ajax({url: getUrlRequest(name), type: type, data: att, success: success, error: error});
},
delete: function(success, error, type, data){
if(type != "POST" || type != "DELETE")
throw new Error("Only DELETE or POST requests are acceptable");
if(seq.length > 1){
var seq = JSON.stringify(this.seq);
}else if(seq.length === 0){
var seq = this.seq[0];
}else{
throw new Error("I need a Senzu Bean, Vegeta :(");
}
var _success = success || this._defaultInsertSuccess;
var _error = error || this._defaultInsertError;
return $.ajax({url: "", data: seq, type: type, success: _success, error: _error});
},
getAndAfterGoToHtml: function(){
$.when(this.get()).done(this.toHtml);
}
};
function isNotNull(obj){
if(arguments.length > 1){
for(var i=0; i<arguments.length; i++) {
if(! isNotNull(arguments[i])) return false;
}
return true;
}else{
return obj != null && obj != undefined && obj != "";
}
}
function getUrlRequest(requirement){
var protocolo = window.location.protocol;
var url = window.location.hostname;
//var url_origem = protocolo + "//" + url + "/www/_admin/_server/admin/" + requirement;
var url_origem = "myurl/url/url" + requirement;
return url_origem;
}
String.prototype.capitalizeFirstLetter = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}