-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi-bem__data.js
More file actions
265 lines (221 loc) · 5.96 KB
/
i-bem__data.js
File metadata and controls
265 lines (221 loc) · 5.96 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
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
/* Initializing BEM Namespace. */
var BEM = BEM || {};
/**
* Extend the class with subclass.
* @method extend
* @access public
* @param object child
* @param object parent
* @return object
*/
var extend = function(child, parent) {
parent = (parent) ? parent : this;
var mutant = function() { parent.apply(this,arguments); },
p = parent.prototype;
for(var prop in parent.prototype) {
mutant.prototype[prop] = parent.prototype[prop];
}
if(child) {
for(var prop in child) {
mutant.prototype[prop] = child[prop];
}
}
return mutant;
};
/**
* Module to represent single data with respected to the user suggested
* schema. Also maintain data operations.
* @namespace BEM
* @module Model
*/
var Model = BEM.Model = function() {
/* Specify the model to act as a collection of data. */
this.collection = false;
/* Schema of the model. */
this.schema = null;
/* Data transformation with respect to Schema. */
this.strict = true;
/* Object to keep current selected data. */
this.datum = {};
/* Array of datum. */
this.data = [];
this.id = 'id';
this.collectionIndex = -1;
this.url = null;
this.initialize.apply(this, arguments);
if(this.schema) { this._schemaVerification(); }
};
/* Extending Model class adding common methods to prototype. */
Model = BEM.Model = extend({
/**
* To verify the dataType specified in the schema is valid or not. if the
* datatype if invalid, it omitts the attribute from the schema.
* @method _schemaVerification
* @access private
*/
_schemaVerification : function() {
for(var attribute in this.schema) {
if(this.schema[attribute] !== 'int' &&
this.schema[attribute] !== 'string' &&
this.schema[attribute] !== 'float') {
delete this.schema[attribute];
}
}
},
/**
* Enforce the datatype of datum to what specified in the model
* schema.
* @method _enforceSchema
* @access private
*/
_enforceSchema : function() {
var schema = this.schema,
dataType = null;
for(var attribute in schema) {
dataType = schema[attribute].toLowerCase();
if(this.datum.hasOwnProperty(attribute)) {
if(dataType === 'int') {
this.datum[attribute] = parseInt(this.datum[attribute]);
}else if(dataType === 'float') {
this.datum[attribute] = parseFloat(this.datum[attribute]);
}else if(dataType === 'string') {
this.datum[attribute] = this.datum[attribute].toString();
}
}
}
},
/**
* Add the datum in the data collection array.
* @method add
* @access private
*/
add : function() {
this.data.push(this.copy());
return this;
},
/**
* To get a copy of current datum.
* @method copy
* @access public
* @return object
*/
copy : function() {
var forge = {};
for(var attribute in this.datum) {
forge[attribute] = this.datum[attribute];
}
return forge;
},
/**
* Set value to the model.
* @method set
* @access public
* @param object|string key
* @param object|string value
*/
set : function(key, value) {
var set = false;
if(typeof key === 'object') {
for(var attribute in this.schema) {
this.datum[attribute] = key[attribute];
}
set = !set;
}else if(typeof key === 'string' && value) {
this.datum[key] = value;
}
/* if strict mode is enabled enforce the datatype of elements in datum
to the what specified in the schema. */
if(this.strict) { this._enforceSchema(); }
if(set) {
/* if the collection transformation flag is enabled add the datum to data
array. */
if(this.collection) { this.add(); }
}
return this;
},
/**
* Get the data. if you pass '*' as key, the method will return full data.
* @method get
* @access public
* @param string key
* @return object
*/
get : function(key) {
if(key === '*') {
return this.datum;
}else {
return this.datum[key];
}
},
/**
* To get the object from the collection with respect to the id give.
* @method find
* @access public
* @param string id
* @return object
*/
find : function(id) {
this.data.forEach(function(datum, index) {
if(datum[this.id] === id) {
this.datum = datum;
this.collectionIndex = index;
}
}, this);
return this.datum;
},
/**
* To get the data from the collection at the index.
* @method at
* @access public
* @param int index
* @return object
*/
at : function(index) {
return this.data[index];
},
/**
* To get an array of objects in the collection matches the supplied attributes.
* @method where
* @access public
* @param object attrs
* @return array
*/
where : function(attrs) {
var filtered = false,
filteredArray = [];
this.data.forEach(function(datum) {
for(var attr in attrs) {
if(datum[attr] === attrs[attr]) { filtered = true; }
else { filtered = false; }
}
if(filtered) { filteredArray.push(datum); }
}, this);
return filteredArray;
},
fetch : function(success, error) {
var successCallback = success,
options = { success : null, error : error };
options.success = function() {
response.forEach(function(datum) {
this.set(datum);
}, this);
if(successCallback) { successCallback.apply(this); }
}
this._comm('GET', null, options, this);
},
_comm : function(method, data, options, context) {
var xhr = new XMLHttpRequest();
xhr.open(method, this.url, true);
xhr.onload = function() {
console.log(this);
response = JSON.parse(this.response);
if(this.status == 200) {
options.success.apply(context, response);
}else if(this.status == 400) {
options.error.apply(context, arguments);
}
}
xhr.send();
}
}, BEM.Model);
Model.extend = BEM.Model.extend = extend;