@base-framework/base 2.6.0 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/base.min.js +1 -0
  2. package/es5/base.js +2968 -0
  3. package/es5/modules/ajax.js +663 -0
  4. package/es5/modules/animation.js +188 -0
  5. package/es5/modules/animations.js +1080 -0
  6. package/es5/modules/atom.js +65 -0
  7. package/es5/modules/component.js +1310 -0
  8. package/es5/modules/data-binder.js +1131 -0
  9. package/es5/modules/data.js +1808 -0
  10. package/es5/modules/date.js +525 -0
  11. package/es5/modules/form-validator.js +324 -0
  12. package/es5/modules/history.js +126 -0
  13. package/es5/modules/html-builder.js +461 -0
  14. package/es5/modules/layout.js +1679 -0
  15. package/es5/modules/mouse.js +124 -0
  16. package/es5/modules/nav-link.js +123 -0
  17. package/es5/modules/olderversions/animations-ease.js +1095 -0
  18. package/es5/modules/olderversions/animations-update.js +1048 -0
  19. package/es5/modules/olderversions/base-animations.js +636 -0
  20. package/es5/modules/olderversions/base-component-class.js +100 -0
  21. package/es5/modules/olderversions/base-data-binder-1.js +407 -0
  22. package/es5/modules/olderversions/base-data-binder-class.js +358 -0
  23. package/es5/modules/olderversions/base-layout-parser-class.js +172 -0
  24. package/es5/modules/olderversions/base-mode-1.js +777 -0
  25. package/es5/modules/olderversions/base-model-class.js +585 -0
  26. package/es5/modules/olderversions/data-binder/element-binding/base-data-binder-class.js +358 -0
  27. package/es5/modules/olderversions/data-binder/element-binding/base-model-class.js +585 -0
  28. package/es5/modules/olderversions/data-binder/model-binding/base-data-binder-class.js +353 -0
  29. package/es5/modules/olderversions/data-binder/model-binding/base-model-class.js +604 -0
  30. package/es5/modules/olderversions/data-binder-update-watcher.js +640 -0
  31. package/es5/modules/olderversions/data-tracker.js +187 -0
  32. package/es5/modules/olderversions/event-update.js +666 -0
  33. package/es5/modules/olderversions/nav-link.js +119 -0
  34. package/es5/modules/olderversions/router-with-templates-1.js +785 -0
  35. package/es5/modules/olderversions/router-with-templates.js +701 -0
  36. package/es5/modules/prototypes/ajax.js +657 -0
  37. package/es5/modules/prototypes/atom.js +65 -0
  38. package/es5/modules/prototypes/component.js +972 -0
  39. package/es5/modules/prototypes/data-binder.js +1089 -0
  40. package/es5/modules/prototypes/data.js +1290 -0
  41. package/es5/modules/prototypes/html-builder.js +414 -0
  42. package/es5/modules/prototypes/layout.js +879 -0
  43. package/es5/modules/router.js +1680 -0
  44. package/es5/modules/state.js +274 -0
  45. package/es6/.jshintrc +3 -0
  46. package/es6/base.js +41 -0
  47. package/es6/core.js +1 -0
  48. package/es6/data-tracker.js +351 -0
  49. package/es6/events.js +602 -0
  50. package/es6/legacy/es5/base.js +2968 -0
  51. package/es6/legacy/es5/modules/ajax.js +663 -0
  52. package/es6/legacy/es5/modules/animation.js +188 -0
  53. package/es6/legacy/es5/modules/animations.js +1080 -0
  54. package/es6/legacy/es5/modules/atom.js +65 -0
  55. package/es6/legacy/es5/modules/component.js +1310 -0
  56. package/es6/legacy/es5/modules/data-binder.js +1131 -0
  57. package/es6/legacy/es5/modules/data.js +1808 -0
  58. package/es6/legacy/es5/modules/date.js +525 -0
  59. package/es6/legacy/es5/modules/form-validator.js +324 -0
  60. package/es6/legacy/es5/modules/history.js +126 -0
  61. package/es6/legacy/es5/modules/html-builder.js +461 -0
  62. package/es6/legacy/es5/modules/layout.js +1679 -0
  63. package/es6/legacy/es5/modules/mouse.js +124 -0
  64. package/es6/legacy/es5/modules/nav-link.js +123 -0
  65. package/es6/legacy/es5/modules/olderversions/animations-ease.js +1095 -0
  66. package/es6/legacy/es5/modules/olderversions/animations-update.js +1048 -0
  67. package/es6/legacy/es5/modules/olderversions/base-animations.js +636 -0
  68. package/es6/legacy/es5/modules/olderversions/base-component-class.js +100 -0
  69. package/es6/legacy/es5/modules/olderversions/base-data-binder-1.js +407 -0
  70. package/es6/legacy/es5/modules/olderversions/base-data-binder-class.js +358 -0
  71. package/es6/legacy/es5/modules/olderversions/base-layout-parser-class.js +172 -0
  72. package/es6/legacy/es5/modules/olderversions/base-mode-1.js +777 -0
  73. package/es6/legacy/es5/modules/olderversions/base-model-class.js +585 -0
  74. package/es6/legacy/es5/modules/olderversions/data-binder/element-binding/base-data-binder-class.js +358 -0
  75. package/es6/legacy/es5/modules/olderversions/data-binder/element-binding/base-model-class.js +585 -0
  76. package/es6/legacy/es5/modules/olderversions/data-binder/model-binding/base-data-binder-class.js +353 -0
  77. package/es6/legacy/es5/modules/olderversions/data-binder/model-binding/base-model-class.js +604 -0
  78. package/es6/legacy/es5/modules/olderversions/data-binder-update-watcher.js +640 -0
  79. package/es6/legacy/es5/modules/olderversions/data-tracker.js +187 -0
  80. package/es6/legacy/es5/modules/olderversions/event-update.js +666 -0
  81. package/es6/legacy/es5/modules/olderversions/nav-link.js +119 -0
  82. package/es6/legacy/es5/modules/olderversions/router-with-templates-1.js +785 -0
  83. package/es6/legacy/es5/modules/olderversions/router-with-templates.js +701 -0
  84. package/es6/legacy/es5/modules/prototypes/ajax.js +657 -0
  85. package/es6/legacy/es5/modules/prototypes/atom.js +65 -0
  86. package/es6/legacy/es5/modules/prototypes/component.js +972 -0
  87. package/es6/legacy/es5/modules/prototypes/data-binder.js +1089 -0
  88. package/es6/legacy/es5/modules/prototypes/data.js +1290 -0
  89. package/es6/legacy/es5/modules/prototypes/html-builder.js +414 -0
  90. package/es6/legacy/es5/modules/prototypes/layout.js +879 -0
  91. package/es6/legacy/es5/modules/router.js +1680 -0
  92. package/es6/legacy/es5/modules/state.js +274 -0
  93. package/es6/main.js +1331 -0
  94. package/es6/modules/ajax/ajax.js +514 -0
  95. package/es6/modules/animation/animation.js +236 -0
  96. package/es6/modules/animations/animation-controller.js +231 -0
  97. package/es6/modules/animations/animation.js +64 -0
  98. package/es6/modules/animations/attr-movement.js +66 -0
  99. package/es6/modules/animations/css-movement.js +170 -0
  100. package/es6/modules/animations/movement.js +131 -0
  101. package/es6/modules/animations/value.js +187 -0
  102. package/es6/modules/atom/atom.js +54 -0
  103. package/es6/modules/component/component.js +230 -0
  104. package/es6/modules/component/event-helper.js +119 -0
  105. package/es6/modules/component/jot.js +144 -0
  106. package/es6/modules/component/state-helper.js +262 -0
  107. package/es6/modules/component/unit.js +551 -0
  108. package/es6/modules/data/attrs.js +40 -0
  109. package/es6/modules/data/basic-data.js +500 -0
  110. package/es6/modules/data/data-utils.js +29 -0
  111. package/es6/modules/data/data.js +3 -0
  112. package/es6/modules/data/deep-data.js +541 -0
  113. package/es6/modules/data/model-service.js +528 -0
  114. package/es6/modules/data/model.js +133 -0
  115. package/es6/modules/data/simple-data.js +33 -0
  116. package/es6/modules/data-binder/connection-tracker.js +113 -0
  117. package/es6/modules/data-binder/connection.js +16 -0
  118. package/es6/modules/data-binder/data-binder.js +352 -0
  119. package/es6/modules/data-binder/data-pub-sub.js +141 -0
  120. package/es6/modules/data-binder/data-source.js +56 -0
  121. package/es6/modules/data-binder/element-source.js +219 -0
  122. package/es6/modules/data-binder/one-way-connection.js +46 -0
  123. package/es6/modules/data-binder/one-way-source.js +43 -0
  124. package/es6/modules/data-binder/source.js +36 -0
  125. package/es6/modules/data-binder/two-way-connection.js +75 -0
  126. package/es6/modules/data-binder/two-way-source.js +41 -0
  127. package/es6/modules/date/date.js +544 -0
  128. package/es6/modules/history/history.js +89 -0
  129. package/es6/modules/html-builder/html-builder.js +434 -0
  130. package/es6/modules/import/import.js +390 -0
  131. package/es6/modules/layout/layout-builder.js +1269 -0
  132. package/es6/modules/layout/layout-parser.js +134 -0
  133. package/es6/modules/layout/watcher-helper.js +282 -0
  134. package/es6/modules/mouse/mouse.js +114 -0
  135. package/es6/modules/router/component-helper.js +163 -0
  136. package/es6/modules/router/history-controller.js +216 -0
  137. package/es6/modules/router/nav-link.js +124 -0
  138. package/es6/modules/router/route.js +401 -0
  139. package/es6/modules/router/router.js +789 -0
  140. package/es6/modules/router/utils.js +31 -0
  141. package/es6/modules/state/state-target.js +91 -0
  142. package/es6/modules/state/state.js +171 -0
  143. package/es6/package-lock.json +13 -0
  144. package/es6/package.json +28 -0
  145. package/es6/shared/objects.js +99 -0
  146. package/legacy/es5/base.js +2968 -0
  147. package/legacy/es5/modules/ajax.js +663 -0
  148. package/legacy/es5/modules/animation.js +188 -0
  149. package/legacy/es5/modules/animations.js +1080 -0
  150. package/legacy/es5/modules/atom.js +65 -0
  151. package/legacy/es5/modules/component.js +1310 -0
  152. package/legacy/es5/modules/data-binder.js +1131 -0
  153. package/legacy/es5/modules/data.js +1808 -0
  154. package/legacy/es5/modules/date.js +525 -0
  155. package/legacy/es5/modules/form-validator.js +324 -0
  156. package/legacy/es5/modules/history.js +126 -0
  157. package/legacy/es5/modules/html-builder.js +461 -0
  158. package/legacy/es5/modules/layout.js +1679 -0
  159. package/legacy/es5/modules/mouse.js +124 -0
  160. package/legacy/es5/modules/nav-link.js +123 -0
  161. package/legacy/es5/modules/olderversions/animations-ease.js +1095 -0
  162. package/legacy/es5/modules/olderversions/animations-update.js +1048 -0
  163. package/legacy/es5/modules/olderversions/base-animations.js +636 -0
  164. package/legacy/es5/modules/olderversions/base-component-class.js +100 -0
  165. package/legacy/es5/modules/olderversions/base-data-binder-1.js +407 -0
  166. package/legacy/es5/modules/olderversions/base-data-binder-class.js +358 -0
  167. package/legacy/es5/modules/olderversions/base-layout-parser-class.js +172 -0
  168. package/legacy/es5/modules/olderversions/base-mode-1.js +777 -0
  169. package/legacy/es5/modules/olderversions/base-model-class.js +585 -0
  170. package/legacy/es5/modules/olderversions/data-binder/element-binding/base-data-binder-class.js +358 -0
  171. package/legacy/es5/modules/olderversions/data-binder/element-binding/base-model-class.js +585 -0
  172. package/legacy/es5/modules/olderversions/data-binder/model-binding/base-data-binder-class.js +353 -0
  173. package/legacy/es5/modules/olderversions/data-binder/model-binding/base-model-class.js +604 -0
  174. package/legacy/es5/modules/olderversions/data-binder-update-watcher.js +640 -0
  175. package/legacy/es5/modules/olderversions/data-tracker.js +187 -0
  176. package/legacy/es5/modules/olderversions/event-update.js +666 -0
  177. package/legacy/es5/modules/olderversions/nav-link.js +119 -0
  178. package/legacy/es5/modules/olderversions/router-with-templates-1.js +785 -0
  179. package/legacy/es5/modules/olderversions/router-with-templates.js +701 -0
  180. package/legacy/es5/modules/prototypes/ajax.js +657 -0
  181. package/legacy/es5/modules/prototypes/atom.js +65 -0
  182. package/legacy/es5/modules/prototypes/component.js +972 -0
  183. package/legacy/es5/modules/prototypes/data-binder.js +1089 -0
  184. package/legacy/es5/modules/prototypes/data.js +1290 -0
  185. package/legacy/es5/modules/prototypes/html-builder.js +414 -0
  186. package/legacy/es5/modules/prototypes/layout.js +879 -0
  187. package/legacy/es5/modules/router.js +1680 -0
  188. package/legacy/es5/modules/state.js +274 -0
  189. package/package.json +8 -3
  190. package/update +16 -0
@@ -0,0 +1,777 @@
1
+ /* base framework module */
2
+ (function()
3
+ {
4
+ "use strict";
5
+
6
+ /*
7
+ Model
8
+
9
+ this create a model module that can bind
10
+ the data using the dase data binder.
11
+
12
+ @param (object) settings = the new model properties
13
+ and methods
14
+ */
15
+ var Model = function(settings)
16
+ {
17
+ this._init();
18
+
19
+ this.dirty = false;
20
+
21
+ /* this will setup the attributes object and the
22
+ attribute stage object */
23
+ this.attributes = {};
24
+ this.stage = {};
25
+
26
+ /* this will setup the event sub for
27
+ one way binding */
28
+ this.eventSub = new base.DataPubSub();
29
+
30
+ /* this will set the construct attributes */
31
+ var attributes = setupAttrSettings(settings);
32
+ this.set(attributes);
33
+
34
+ /* this will setup the data binding for two
35
+ way binding */
36
+ this.bind = true;
37
+
38
+ this.initialize();
39
+
40
+ this.xhr = null;
41
+ };
42
+
43
+ Model.prototype =
44
+ {
45
+ constructor: Model,
46
+
47
+ /* this will setup the model number and unique
48
+ instance id */
49
+ _init: function()
50
+ {
51
+ var constructor = this.constructor;
52
+ this._modelNumber = (typeof constructor._modelNumber === 'undefined')? constructor._modelNumber = 0 : (++constructor._modelNumber);
53
+
54
+ var modelId = this.modelTypeId + '-';
55
+ this._id = modelId + this._modelNumber;
56
+ },
57
+
58
+ /* this will get the unique model id
59
+ @return (string) the id */
60
+ getModelId: function()
61
+ {
62
+ return this._id;
63
+ },
64
+
65
+ remove: function()
66
+ {
67
+
68
+ },
69
+
70
+ initialize: function()
71
+ {
72
+
73
+ },
74
+
75
+ on: function(attrName, callBack)
76
+ {
77
+ var message = attrName + ':change';
78
+ this.eventSub.on(message, callBack);
79
+ },
80
+
81
+ off: function(attrName, callBack)
82
+ {
83
+ var message = attrName + ':change';
84
+ this.eventSub.off(message, callBack);
85
+ },
86
+
87
+ set: function()
88
+ {
89
+ var self = this;
90
+ var set = function(attr, val, committer, stopMerge)
91
+ {
92
+ var setValue = function(obj, attr, val)
93
+ {
94
+ var test = /(\.|\[)/;
95
+ if(test.test(attr))
96
+ {
97
+ var pattern = /(\w+)|(?:\[(\d)\))/g;
98
+ var props = attr.match(pattern);
99
+ for (var i = 0, length = props.length - 1; i < length; i++)
100
+ {
101
+ var prop = props[i];
102
+ var propValue = obj[prop];
103
+ if (propValue === undefined)
104
+ {
105
+ /* this will check to setup a new object
106
+ or an array if the prop is a number */
107
+ obj[prop] = !isNaN(prop)? {} : [];
108
+ }
109
+
110
+ obj = propValue;
111
+ }
112
+
113
+ obj[props[i]] = val;
114
+ }
115
+ else
116
+ {
117
+ obj[attr] = val;
118
+ }
119
+ };
120
+
121
+ if(self.bind === true)
122
+ {
123
+ var binder = base.DataBinder,
124
+ msg = self._id + ':change';
125
+
126
+ /* this will recursivley update the data binder to
127
+ publish updates on values. this will convert the value
128
+ to a string path to where the object is stored to allow
129
+ deep nested data to update on the bound elements.
130
+ @param (mixed) obj
131
+ @param (string) pathString */
132
+ var publish = function(obj, pathString)
133
+ {
134
+ pathString = pathString || "";
135
+ if(obj && typeof obj === 'object')
136
+ {
137
+ var subPath, value;
138
+ if(Array.isArray(obj))
139
+ {
140
+ var length = obj.length;
141
+ for(var i = 0; i < length; i++)
142
+ {
143
+ value = obj[i];
144
+ subPath = pathString + '[' + i + ']';
145
+ if(value && typeof value === 'object')
146
+ {
147
+ publish(value, subPath);
148
+ }
149
+ else
150
+ {
151
+ /* save path and value */
152
+ binder.publish(msg, subPath, value, self);
153
+ }
154
+ }
155
+ }
156
+ else
157
+ {
158
+ for(var prop in obj)
159
+ {
160
+ if(obj.hasOwnProperty(prop))
161
+ {
162
+ value = obj[prop];
163
+ subPath = pathString + '.' + prop;
164
+ if(value && typeof value === 'object')
165
+ {
166
+ publish(value, subPath);
167
+ }
168
+ else
169
+ {
170
+ /* save path and value */
171
+ binder.publish(msg, subPath, value, self);
172
+ }
173
+ }
174
+ }
175
+ }
176
+ }
177
+ else
178
+ {
179
+ /* save path and value */
180
+ binder.publish(msg, pathString, obj, self);
181
+ }
182
+ return pathString;
183
+ };
184
+
185
+ /* this will check to update the model based on who
186
+ updated it. if the data binder updated the data only
187
+ the stage data is updated */
188
+ if(!committer)
189
+ {
190
+ /* this will publish the data to the data binder
191
+ to update any ui elements that are subscribed */
192
+ publish(val, attr);
193
+
194
+ /* this will update both stage and attribute
195
+ data because it wasupdated outside the data
196
+ binder */
197
+ if(stopMerge !== true)
198
+ {
199
+ setValue(self.attributes, attr, val);
200
+ }
201
+ setValue(self.stage, attr, val);
202
+ }
203
+ else
204
+ {
205
+ setValue(self.stage, attr, val);
206
+
207
+ if(self.dirty === false)
208
+ {
209
+ self.dirty = true;
210
+ }
211
+ }
212
+ }
213
+ else
214
+ {
215
+ if(stopMerge !== true)
216
+ {
217
+ setValue(self.attributes, attr, val);
218
+ }
219
+ setValue(self.stage, attr, val);
220
+ }
221
+
222
+ /* this will check to update any local events
223
+ on the model */
224
+ var message = attr + ':change';
225
+ self.eventSub.publish(message, attr, val);
226
+ };
227
+
228
+ var setupObject = function(object, committer, stopMerge)
229
+ {
230
+ var items = object;
231
+ for(var attr in items)
232
+ {
233
+ if(items.hasOwnProperty(attr))
234
+ {
235
+ var item = items[attr];
236
+ if(typeof item === 'function')
237
+ {
238
+ continue;
239
+ }
240
+ set(attr, item, committer, stopMerge);
241
+ }
242
+ }
243
+ };
244
+
245
+ var args = arguments;
246
+ if(typeof args[0] === 'object')
247
+ {
248
+ setupObject(args[0], args[1], args[2]);
249
+ }
250
+ else
251
+ {
252
+ set(args[0], args[1], args[2], args[3]);
253
+ }
254
+ },
255
+
256
+ mergeStage: function()
257
+ {
258
+ /* this will clone the stage object to the
259
+ attribute object */
260
+ this.attributes = JSON.parse(JSON.stringify(this.stage));
261
+ this.dirty = false;
262
+ },
263
+
264
+ getModelData: function()
265
+ {
266
+ this.mergeStage();
267
+ return this.attributes;
268
+ },
269
+
270
+ revert: function()
271
+ {
272
+ /* this will reset the stage to the previous
273
+ attributes */
274
+ this.set(this.attributes);
275
+ this.dirty = false;
276
+ },
277
+
278
+ get: function( attrName )
279
+ {
280
+ if(typeof attrName !== 'undefined')
281
+ {
282
+ var get = function(obj, attr)
283
+ {
284
+ var test = /(\.|\[)/;
285
+ if(test.test(attr))
286
+ {
287
+ var pattern = /(\w+)|(?:\[(\d)\))/g;
288
+ var props = attr.match(pattern);
289
+ for (var i = 0, length = props.length - 1; i < length; i++)
290
+ {
291
+ var prop = props[i];
292
+ var propValue = obj[prop];
293
+ if (propValue !== undefined)
294
+ {
295
+ obj = propValue;
296
+ }
297
+ else
298
+ {
299
+ break;
300
+ }
301
+ }
302
+
303
+ return obj[props[i]];
304
+ }
305
+ else
306
+ {
307
+ return obj[attr];
308
+ }
309
+ };
310
+ return get(this.stage, attrName);
311
+ }
312
+ else
313
+ {
314
+ return this.getModelData();
315
+ }
316
+ }
317
+ };
318
+
319
+ /* this will get the model attributes.
320
+ @param (object) settings = the model settings
321
+ @return (object) the attributes */
322
+ var setupAttrSettings = function(settings)
323
+ {
324
+ var attributes = {};
325
+ if(settings && typeof settings === 'object')
326
+ {
327
+ for(var prop in settings)
328
+ {
329
+ if(settings.hasOwnProperty(prop))
330
+ {
331
+ var setting = settings[prop];
332
+ if(typeof setting !== 'function')
333
+ {
334
+ attributes[prop] = setting;
335
+ delete settings[prop];
336
+ }
337
+ }
338
+ }
339
+ }
340
+ return attributes;
341
+ };
342
+
343
+ /* this will get the model attributes.
344
+ @param (object) settings = the model settings
345
+ @return (object) the attributes */
346
+ var setupDefaultAttr = function(settings)
347
+ {
348
+ var attributes = {};
349
+ if(settings && typeof settings === 'object')
350
+ {
351
+ var defaults = settings.defaults;
352
+ if(defaults)
353
+ {
354
+ for(var prop in defaults)
355
+ {
356
+ if(defaults.hasOwnProperty(prop))
357
+ {
358
+ var attr = defaults[prop];
359
+ if(typeof attr !== 'function')
360
+ {
361
+ attributes[prop] = attr;
362
+ }
363
+ }
364
+ }
365
+ delete settings.defaults;
366
+ }
367
+ }
368
+ return attributes;
369
+ };
370
+
371
+ /* this will get the model attributes.
372
+ @param (object) settings = the model settings
373
+ @return (object) the attributes */
374
+ var getXhr = function(settings)
375
+ {
376
+ if(settings && typeof settings === 'object')
377
+ {
378
+ var settingsXhr = settings.xhr;
379
+ if(settingsXhr && typeof settingsXhr === 'object')
380
+ {
381
+ var xhr = base.createObject(settingsXhr);
382
+ delete settings.xhr;
383
+ return xhr;
384
+ }
385
+ }
386
+ return {};
387
+ };
388
+
389
+ /* this will track the number of model types */
390
+ var modelTypeNumber = 0;
391
+
392
+ /* this will extend the model.
393
+ @param (object) settings = the new model properties
394
+ and methods
395
+ @param (object) the new model */
396
+ Model.extend = function(settings)
397
+ {
398
+ var parent = this;
399
+
400
+ var xhr = getXhr(settings);
401
+ var modelService = this.prototype.xhr.extend(xhr);
402
+
403
+ settings = settings || {};
404
+
405
+ /* this will setup the default attribute settings for
406
+ the model */
407
+ var defaultAttributes = setupDefaultAttr(settings);
408
+ var model = function(instanceSettings)
409
+ {
410
+ /* this will clone the original
411
+ instance settings object */
412
+ if(instanceSettings && typeof instanceSettings === 'object')
413
+ {
414
+ instanceSettings = JSON.parse(JSON.stringify(instanceSettings));
415
+ }
416
+
417
+ /* this will get the instance attributes that
418
+ the model will set as attribute data */
419
+ var instanceAttr = setupAttrSettings(instanceSettings);
420
+
421
+ /* we want to extend the default attr with the
422
+ instance attr before we set the data and call
423
+ the parent constructor */
424
+ instanceAttr = base.extendObject(defaultAttributes, instanceAttr);
425
+ parent.call(this, instanceAttr);
426
+
427
+ /* this will setup the model service and
428
+ pass the new model instance to the service */
429
+ this.xhr = new modelService(this);
430
+ };
431
+
432
+ /* this will extend the model and add the static
433
+ methods to the new object */
434
+ model.prototype = base.extendClass(this.prototype, settings);
435
+ model.prototype.constructor = model;
436
+ model.prototype.xhr = modelService;
437
+
438
+ /* this will assign a unique id to the model type */
439
+ model.prototype.modelTypeId = 'bm' + (modelTypeNumber++);
440
+
441
+ /* this will extend the static methods */
442
+ base.extendObject(parent, model);
443
+ return model;
444
+ };
445
+
446
+ base.extend.Model = Model;
447
+
448
+ /*
449
+ ModelService
450
+
451
+ this is a service that can connect a model to
452
+ a web service api.
453
+
454
+ @param (object) model
455
+ */
456
+ var ModelService = function(model)
457
+ {
458
+ this.model = model;
459
+ this.url = '';
460
+
461
+ this.init();
462
+ };
463
+
464
+ ModelService.prototype =
465
+ {
466
+ constrcutor: ModelService,
467
+
468
+ init: function()
469
+ {
470
+ var model = this.model;
471
+ if(model && model.url)
472
+ {
473
+ this.url = model.url;
474
+ }
475
+ },
476
+
477
+ validateCallBack: null,
478
+
479
+ isValid: function()
480
+ {
481
+ var result = this.validate();
482
+ if(result === false)
483
+ {
484
+ var callBack = this.validateCallBack;
485
+ if(typeof callBack === 'function')
486
+ {
487
+ callBack(result);
488
+ }
489
+ }
490
+ return result;
491
+ },
492
+
493
+ /* this is a method to use to override to set
494
+ a default validate methode to check the model
495
+ before sending data.
496
+ @return (bool) true or false to submit */
497
+ validate: function()
498
+ {
499
+ return true;
500
+ },
501
+
502
+ /* this is a method to use to override to set
503
+ a default param string to be appended to each
504
+ xhr request
505
+ @return (string) params */
506
+ getDefaultParams: function()
507
+ {
508
+ return '';
509
+ },
510
+
511
+ /* this will add the individual params with the default
512
+ params.
513
+ @param (string) params
514
+ @return (string) the appended params */
515
+ setupParams: function(params)
516
+ {
517
+ var defaults = this.getDefaultParams();
518
+ params = this.addParams(params, defaults);
519
+ return params;
520
+ },
521
+
522
+ parseQueryString: function(str)
523
+ {
524
+ str = str || '';
525
+ var objURL = {},
526
+ regExp = /([^?=&]+)(=([^&]*))?/g;
527
+
528
+ str.replace(regExp, function(a, b, c, d)
529
+ {
530
+ /* we want to save the key and the
531
+ value to the objURL */
532
+ objURL[b] = d;
533
+ });
534
+
535
+ /* we want to check if the url has any params */
536
+ return objURL;
537
+ },
538
+
539
+ /* this will add params strings.
540
+ @param (string) params
541
+ @param (string) addingParams
542
+ @return (string) the params combined */
543
+ addParams: function(params, addingParams)
544
+ {
545
+ params = params || {};
546
+ if(typeof params !== 'object')
547
+ {
548
+ params = this.parseQueryString(params);
549
+ }
550
+
551
+ addingParams = addingParams || {};
552
+ if(typeof addingParams === 'string')
553
+ {
554
+ addingParams = this.parseQueryString(addingParams);
555
+ }
556
+
557
+ return base.extendObject(params, addingParams);
558
+ },
559
+
560
+ /* this is a property to override the return type
561
+ used in the the request and response xhr calls. this
562
+ will define what the web service defines as the object type. */
563
+ objectType: 'item',
564
+
565
+ /* this will get a model object from the service.
566
+ @param [(string)] instanceParams = the params to add
567
+ to the service instance
568
+ @param [(function)] callBack = the callBack to handle
569
+ the service xhr response
570
+ @return (object) xhr object */
571
+ get: function(instanceParams, callBack)
572
+ {
573
+ var id = this.model.get('id');
574
+ var params = 'op=get' +
575
+ '&id=' + id;
576
+
577
+ var model = this.model,
578
+ self = this;
579
+ return this.request(params, instanceParams, callBack, function(response)
580
+ {
581
+ if(response)
582
+ {
583
+ /* this will update the model with the get request
584
+ response */
585
+ var object = self.getObject(response);
586
+ if(object)
587
+ {
588
+ model.set(object);
589
+ }
590
+ }
591
+ });
592
+ },
593
+
594
+ getObject: function(response)
595
+ {
596
+ /* this will update the model with the get request
597
+ response */
598
+ var object = response[this.objectType] || response;
599
+ return (object)? object : false;
600
+ },
601
+
602
+ /* this will add or update a model object to the service.
603
+ @param [(string)] instanceParams = the params to add
604
+ to the service instance
605
+ @param [(function)] callBack = the callBack to handle
606
+ the service xhr response
607
+ @return (object) xhr object or false on error */
608
+ setup: function(instanceParams, callBack)
609
+ {
610
+ if(this.isValid())
611
+ {
612
+ var item = this.model.get();
613
+ var params = 'op=setup' +
614
+ '&' + this.objectType + '=' + base.prepareJsonUrl(item);
615
+
616
+ /* this will add the instance params with the
617
+ method params */
618
+ params = this.addParams(params, instanceParams, instanceParams);
619
+
620
+ return this.request(params, callBack);
621
+ }
622
+ return false;
623
+ },
624
+
625
+ /* this will add a model object from the service.
626
+ @param [(string)] instanceParams = the params to add
627
+ to the service instance
628
+ @param [(function)] callBack = the callBack to handle
629
+ the service xhr response
630
+ @return (object) xhr object or false on error*/
631
+ add: function(instanceParams, callBack)
632
+ {
633
+ if(this.isValid())
634
+ {
635
+ var item = this.model.get();
636
+ var params = 'op=add' +
637
+ '&' + this.objectType + '=' + base.prepareJsonUrl(item);
638
+
639
+ return this.request(params, instanceParams, callBack);
640
+ }
641
+ return false;
642
+ },
643
+
644
+ /* this will update a model object from the service.
645
+ @param [(string)] instanceParams = the params to add
646
+ to the service instance
647
+ @param [(function)] callBack = the callBack to handle
648
+ the service xhr response
649
+ @return (object) xhr object or false on error*/
650
+ update: function(instanceParams, callBack)
651
+ {
652
+ if(this.isValid())
653
+ {
654
+ var item = this.model.get();
655
+ var params = 'op=update' +
656
+ '&' + this.objectType + '=' + base.prepareJsonUrl(item);
657
+
658
+ return this.request(params, instanceParams, callBack);
659
+ }
660
+ return false;
661
+ },
662
+
663
+ /* this will delete a model object from the service.
664
+ @param [(string)] instanceParams = the params to add
665
+ to the service instance
666
+ @param [(function)] callBack = the callBack to handle
667
+ the service xhr response
668
+ @return (object) xhr object */
669
+ delete: function(instanceParams, callBack)
670
+ {
671
+ var id = this.model.get('id');
672
+ var params = 'op=delete' +
673
+ '&id=' + id;
674
+
675
+ return this.request(params, instanceParams, callBack);
676
+ },
677
+
678
+ /* this will get a list of model object from the service.
679
+ @param [(string)] instanceParams = the params to add
680
+ to the service instance
681
+ @param [(function)] callBack = the callBack to handle
682
+ the service xhr response
683
+ @param [(int)] start = the row start count
684
+ @param [(int)] count = the row numbers to get
685
+ @param [(string)] filter = the filter
686
+ @return (object) xhr object */
687
+ all: function(instanceParams, callBack, start, count, filter)
688
+ {
689
+ filter = filter || '';
690
+ start = !isNaN(start)? start : 0;
691
+ count = !isNaN(count)? count : 50;
692
+
693
+ var params = 'op=all' +
694
+ '&option=' + filter +
695
+ '&start=' + start +
696
+ '&stop=' + count;
697
+
698
+ return this.request(params, instanceParams, callBack);
699
+ },
700
+
701
+ /* this will make the xhr request to the
702
+ service.
703
+ @param (string) params
704
+ @param (string) instanceParams = the params to add
705
+ to the service instance
706
+ @param (function) callBack
707
+ @param [(function)] requestCallBack = the call back for the
708
+ service method
709
+ @return (object) xhr object */
710
+ request: function(params, instanceParams, callBack, requestCallBack)
711
+ {
712
+ params = this.setupParams(params);
713
+
714
+ /* this will add the instance params with the
715
+ method params */
716
+ params = this.addParams(params, instanceParams);
717
+
718
+ var self = this;
719
+ return base.ajax({
720
+ url: this.url,
721
+ params: params,
722
+ completed: function(response, xhr)
723
+ {
724
+ if(typeof requestCallBack === 'function')
725
+ {
726
+ requestCallBack(response);
727
+ }
728
+
729
+ self.getResponse(response, callBack, xhr);
730
+ }
731
+ });
732
+ },
733
+
734
+ /* this will handle the xhr response.
735
+ @param (mixed) response = the xhr response
736
+ @param (function) callBack
737
+ @param (object) the xhr object */
738
+ getResponse: function(response, callBack, xhr)
739
+ {
740
+ /* this will check to return the response
741
+ to the callBack function */
742
+ if(typeof callBack === 'function')
743
+ {
744
+ callBack(response, xhr);
745
+ }
746
+ }
747
+ };
748
+
749
+ /* this will setup the service object to extend itself.
750
+ @param (object) settings = the new service settings to
751
+ extend
752
+ @return (object) the new model service */
753
+ ModelService.extend = function(settings)
754
+ {
755
+ var parent = this;
756
+
757
+ /* this will setup a new model service constructor */
758
+ var modelService = function(model)
759
+ {
760
+ /* this will call the parent contructor */
761
+ parent.call(this, model);
762
+ };
763
+
764
+ /* this will extend the model and add the static
765
+ methods to the new object */
766
+ modelService.prototype = base.extendClass(this.prototype, settings);
767
+ modelService.prototype.constructor = modelService;
768
+
769
+ base.extendObject(parent, modelService);
770
+
771
+ return modelService;
772
+ };
773
+
774
+ /* we need to add the service object to the
775
+ model prototype as the xhr service */
776
+ Model.prototype.xhr = ModelService;
777
+ })();