-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertebrate.js
305 lines (295 loc) · 11.1 KB
/
vertebrate.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* VertebrateJS v 0.3.3 by Michael Smith @psalmody
* https://github.com/psalmody/vertebratejs
*/
var Vertebrate = (function($) {
if (typeof($) == 'undefined') {
console.log('%cError:','color:red;font-weight:bold','Vertebrate.js requires jQuery 1.11+');
return false;
}
var Vertebrate = {
settings: {},
get: function(setting) {
return (typeof(setting)) == 'undefined' ?
this.settings :
this.settings[setting];
},
set: function(setting, value) {
return this.settings[setting] = value;
},
shared: {
fn: function() {
this.attributes = {};
this.changedattrs = [];
this.set = function(attr, value) {
this.attributes[attr] = value;
if (this.changedattrs.indexOf(attr) == -1) this.changedattrs.push(attr);
$([this]).trigger('vertebrate:changeattr', [this, this.attributes, this.changedattrs]);
$(document).trigger('vertebrate:changeattr', [this, this.attributes, this.changedattrs]);
return true;
};
this.get = function(attr) {
if (typeof(attr) == 'undefined') return this.attributes;
return this.attributes[attr];
};
this.has = function(attr) {
return (typeof(this.attributes[attr]) == 'undefined') ? false : true;
};
this.hasChanged = function(attr) {
var attr = typeof(attr) == 'undefined' ? false : attr;
if (!attr) {
return this.changedattrs.length > 0 ? true : false;
}
return this.changedattrs.indexOf(attr) >= 0 ? true : false;
};
}
}
};
Vertebrate.Model = function(options) {
var self = this;
Vertebrate.shared.fn.call(this);
var options = typeof(options) == 'undefined' ? false : options;
if (options) {
$.each(options,function(k,v){
if (typeof(v) == 'object') {
self[k] = $.extend(self[k],v);
} else {
self[k] = v;
}
})
}
this.save = function(callback) {
var promise = $.ajax({
url: typeof(this.url) == 'undefined' ? Vertebrate.get('url') : this.url,
method: 'POST',
data: {"model":this.get()},
dataType: 'JSON',
success: function(data, status, xhr) {
self.changedattrs = [];
if (typeof(callback) == 'function') callback.call(self,data, status, xhr);
}
});
return promise;
};
this.fetch = function(callback) {
var promise = $.ajax({
url: typeof(this.url) == 'undefined' ? Vertebrate.get('url') : this.url,
method: 'GET',
dataType: 'JSON',
data: {"model":this.get()},
success: function(data, status, xhr) {
self.changedattrs = [];
self.attributes = $.extend(self.attributes, data[0]);
if (typeof(callback) == 'function') callback.call(self, data, status, xhr);
}
});
return promise;
};
this.delete = function(callback) {
var promise = $.ajax({
url: typeof(this.url) == 'undefined' ? Vertebrate.get('url') : this.url,
method: 'DELETE',
data: {"model":this.get()},
dataType: 'JSON',
success: function(data, status, xhr) {
$([self]).trigger('vertebrate:deleted', [self, self.attributes, data, status, xhr]);
$(document).trigger('vertebrate:deleted', [self, self.attributes, data, status, xhr]);
if (typeof(callback) == 'function') callback.call(self,data, status, xhr);
}
});
return promise;
};
};
Vertebrate.Model.prototype = Object.create(Vertebrate.shared.fn);
Vertebrate.Model.prototype.constructor = Vertebrate.Model;
Vertebrate.Model.Extend = function(options) {
var model = function(opts) {
Vertebrate.Model.call(this, options);
var self = this;
var opts = typeof(opts) == 'undefined' ? false : opts;
if (opts) {
self.attributes = $.extend(self.attributes,opts);
}
return this;
}
model.prototype = Object.create(Vertebrate.Model.prototype);
model.prototype.constructor = model;
return model;
};
Vertebrate.Collection = function(options) {
Vertebrate.shared.fn.call(this, options);
this.model = false;
this.models = [];
this.added = [];
this.removed = [];
var self = this;
$.each(options,function(k,v){
if (typeof(v) == 'object') {
$.extend(self[k],v);
} else {
self[k] = v;
}
})
/**
* this.save() - post all models to url (this.url or Vertebrate.get('url')
* if not defined)
*
* clears this.removed[]
*/
this.save = function(callback) {
var postdata = {
"collection": {
"attributes": this.get(),
"models": JSON.stringify(this.models)
}
};
var promise = $.ajax({
url: typeof(this.url) == 'undefined' ? Vertebrate.get('url') : this.url,
method: 'POST',
data: postdata,
dataType: 'JSON',
success: function(data, status, xhr) {
self.removed = [];
if (typeof(callback) == 'function') callback.call(self, data, status, xhr);
}
});
return promise;
};
/**
* this.fetch() - get models from url (this.url or Vertebrate.get('url')
* if not defined)
*
* wipes existing models from collection
*/
this.fetch = function(callback) {
if (!self.model) {
console.log('No model defined. Models must be defined before fetch call.');
return false;
}
var promise = $.ajax({
url: typeof(this.url) == 'undefined' ? Vertebrate.get('url') : this.url,
method: 'GET',
dataType: 'JSON',
data: {"collection":this.get()},
success: function(data, status, xhr) {
self.models = [];
$.each(data, function(i) {
self.models.push(new self.model(data[i]));
});
self.removed = [];
$([self]).trigger('vertebrate:fetched',[self.models]);
$(document).trigger('vertebrate:fetched',[self,self.models]);
if (typeof(callback) == 'function') callback.call(self,data, status, xhr);
}
});
return promise;
};
/**
* this.find() returns the first model which matches
*
* this.find( term ) returns collection.models[term]
*
* this.find( term, attr ) searches through the collection
* and returns the first model where model[attr] = term
*/
this.find = function(term, attr) {
var attr = (typeof(attr) == 'undefined') ? false : attr;
var found = false;
if (attr) {
$.each(self.models, function(k, v) {
if (v.attributes[attr] == term) {
found = this;
return false;
}
});
if (!found) return false;
return found;
} else {
return self.models[term];
}
};
/**
* this.findAll() returns an array of models from the collection
*
* this.findAll() can be a bit tricky depending on your JSON attributes
* from this.fetch()
*
* MySQL -> PHP returns integers as strings, so PHP with json_encode()
* means we get strings in this.attributes which can cause it to be
* tricky with .findAll()
*/
this.findAll = function(term, attr) {
var found = false;
var founds = [];
$.each(self.models, function(k, v) {
if (v.attributes[attr] == term) {
found = true;
founds.push(this);
}
});
if (!found) return false;
return founds;
};
this.count = function() {
return self.models.length;
}
this.add = function(model) {
self.added.push(model);
self.models.push(model);
$([self]).trigger('vertebrate:added', [model, self.added]);
$(document).trigger('vertebrate:added', [self, model, self.added]);
return self.models.length;
};
this.remove = function(model) {
var newmodels = self.models.filter(function(m) {
return m != model;
});
self.models = newmodels;
self.removed.push(model);
$([self]).trigger('vertebrate:removed', [model, self.removed]);
$(document).trigger('vertebrate:removed', [self, model, self.removed]);
return self.models.length;
};
/**
* returns max value of numerical
*/
this.max = function(attr) {
if (typeof(attr) == 'undefined') return false;
var greatest = 0;
$.each(self.models,function(k,v) {
if (Number(v.attributes[attr]) > greatest) {
greatest = v.attributes[attr];
}
});
return greatest;
};
/**
* Returns the next value of an attribute .max() + 1
*/
this.next = function( attr ) {
var m = self.max( attr );
return Number(m)+1;
}
};
Vertebrate.Collection.prototype = Object.create(Vertebrate.shared.fn);
Vertebrate.Collection.prototype.constructor = Vertebrate.Collection;
Vertebrate.Collection.Extend = function(options) {
function collection(opts) {
Vertebrate.Collection.call(this, options);
var self = this;
if (typeof(opts) != 'undefined') {
$.each(opts,function(k,v){
if (typeof(v) == 'object') {
$.extend(self[k],v);
} else {
self[k] = v;
}
})
}
}
collection.prototype = Object.create(Vertebrate.Collection.prototype);
collection.prototype.constructor = collection;
return collection;
};
return Vertebrate;
}($));