joosy 1.2.0.alpha.14 → 1.2.0.alpha.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/Gruntfile.coffee +42 -18
  4. data/README.md +16 -12
  5. data/bower.json +1 -1
  6. data/lib/extensions/form.js +592 -0
  7. data/lib/extensions/preloaders.js +193 -0
  8. data/lib/extensions/resources.js +667 -0
  9. data/lib/joosy.js +1141 -2680
  10. data/lib/joosy.rb +0 -5
  11. data/package.json +1 -1
  12. data/spec/helpers/helper.coffee +1 -1
  13. data/spec/joosy/core/joosy_spec.coffee +1 -28
  14. data/spec/joosy/core/modules/renderer_spec.coffee +0 -95
  15. data/spec/joosy/{core → extensions/form}/form_spec.coffee +2 -2
  16. data/spec/joosy/{core → extensions/form}/helpers/forms_spec.coffee +1 -1
  17. data/spec/joosy/{preloaders → extensions/preloaders}/caching_spec.coffee +0 -0
  18. data/spec/joosy/{preloaders → extensions/preloaders}/inline_spec.coffee +0 -0
  19. data/spec/joosy/{core/resource/generic_spec.coffee → extensions/resources/base_spec.coffee} +19 -20
  20. data/spec/joosy/{core/resource → extensions/resources}/collection_spec.coffee +4 -4
  21. data/spec/joosy/{core/resource → extensions/resources}/rest_collection_spec.coffee +4 -4
  22. data/spec/joosy/{core/resource → extensions/resources}/rest_spec.coffee +6 -7
  23. data/src/joosy.coffee +1 -2
  24. data/src/joosy/core/helpers/view.coffee +0 -8
  25. data/src/joosy/core/joosy.coffee +5 -44
  26. data/src/joosy/core/modules/renderer.coffee +10 -29
  27. data/src/joosy/core/{resource → resources}/watcher.coffee +2 -1
  28. data/src/joosy/{core → extensions/form}/form.coffee +3 -9
  29. data/src/joosy/{core → extensions/form}/helpers/form.coffee +2 -2
  30. data/src/joosy/extensions/form/index.coffee +1 -0
  31. data/src/joosy/{preloaders → extensions/preloaders}/caching.coffee +0 -0
  32. data/src/joosy/extensions/preloaders/index.coffee +1 -0
  33. data/src/joosy/{preloaders → extensions/preloaders}/inline.coffee +0 -0
  34. data/src/joosy/{core/resource/generic.coffee → extensions/resources/base.coffee} +21 -21
  35. data/src/joosy/{core/resource → extensions/resources}/collection.coffee +11 -11
  36. data/src/joosy/extensions/resources/index.coffee +1 -0
  37. data/src/joosy/{core/resource → extensions/resources}/rest.coffee +15 -12
  38. data/src/joosy/{core/resource → extensions/resources}/rest_collection.coffee +4 -2
  39. data/src/joosy/generators/base.coffee +1 -1
  40. metadata +25 -20
  41. data/src/joosy/core/preloader.coffee +0 -13
@@ -0,0 +1,193 @@
1
+
2
+
3
+ /*** src/joosy/extensions/preloaders/caching ***/
4
+
5
+ this.CachingPreloader = {
6
+ force: false,
7
+ prefix: "cache:",
8
+ counter: 0,
9
+ load: function(libraries, options) {
10
+ var i, key, lib, val, _i, _len, _ref, _ref1;
11
+ if (options == null) {
12
+ options = {};
13
+ }
14
+ for (key in options) {
15
+ val = options[key];
16
+ this[key] = val;
17
+ }
18
+ this.libraries = libraries.slice();
19
+ _ref = this.libraries;
20
+ for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
21
+ lib = _ref[i];
22
+ this.libraries[i] = this.prefix + lib[0];
23
+ }
24
+ if (!this.force && this.check()) {
25
+ return this.restore();
26
+ } else {
27
+ if ((_ref1 = this.start) != null) {
28
+ _ref1.call(window);
29
+ }
30
+ this.clean();
31
+ return this.download(libraries);
32
+ }
33
+ },
34
+ check: function() {
35
+ var flag, i, name, _i, _len, _ref;
36
+ flag = true;
37
+ _ref = this.libraries;
38
+ for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
39
+ name = _ref[i];
40
+ flag && (flag = window.localStorage.getItem(name) != null);
41
+ }
42
+ return flag;
43
+ },
44
+ escapeStr: function(str) {
45
+ return str.replace(new RegExp("\u0001", 'g'), "\\u0001").replace(new RegExp("\u000B", 'g'), "\\u000B");
46
+ },
47
+ restore: function() {
48
+ var i, name, _i, _len, _ref, _ref1;
49
+ _ref = this.libraries;
50
+ for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
51
+ name = _ref[i];
52
+ window.evalGlobaly(window.localStorage.getItem(name));
53
+ }
54
+ return (_ref1 = this.complete) != null ? _ref1.call(window, true) : void 0;
55
+ },
56
+ download: function(libraries) {
57
+ var lib, size, url, _ref,
58
+ _this = this;
59
+ if (libraries.length > 0) {
60
+ this.counter += 1;
61
+ lib = libraries.shift();
62
+ url = lib[0];
63
+ size = lib[1];
64
+ return this.ajax(url, size, function(xhr) {
65
+ var code;
66
+ code = xhr.responseText;
67
+ if (window.navigator.appName === "Microsoft Internet Explorer") {
68
+ code = _this.escapeStr(code);
69
+ }
70
+ window.localStorage.setItem(_this.prefix + url, code);
71
+ window.evalGlobaly(xhr.responseText);
72
+ return _this.download(libraries);
73
+ });
74
+ } else {
75
+ return (_ref = this.complete) != null ? _ref.call(window) : void 0;
76
+ }
77
+ },
78
+ ajax: function(url, size, callback) {
79
+ var poller, x,
80
+ _this = this;
81
+ if (window.XMLHttpRequest) {
82
+ x = new XMLHttpRequest;
83
+ } else {
84
+ x = new ActiveXObject('Microsoft.XMLHTTP');
85
+ }
86
+ x.open('GET', url, 1);
87
+ x.onreadystatechange = function() {
88
+ if (x.readyState > 3) {
89
+ clearInterval(_this.interval);
90
+ return typeof callback === "function" ? callback(x) : void 0;
91
+ }
92
+ };
93
+ if (this.progress) {
94
+ poller = function() {
95
+ var e;
96
+ try {
97
+ return _this.progress.call(window, Math.round((x.responseText.length / size) * (_this.counter / _this.libraries.length) * 100));
98
+ } catch (_error) {
99
+ e = _error;
100
+ }
101
+ };
102
+ this.interval = setInterval(poller, 100);
103
+ }
104
+ return x.send();
105
+ },
106
+ clean: function() {
107
+ var find, i, key, _results;
108
+ i = 0;
109
+ find = function(arr, obj) {
110
+ var x, _i, _len;
111
+ for (_i = 0, _len = arr.length; _i < _len; _i++) {
112
+ x = arr[_i];
113
+ if (obj === x) {
114
+ return i;
115
+ }
116
+ }
117
+ return -1;
118
+ };
119
+ _results = [];
120
+ while (i < window.localStorage.length && (key = window.localStorage.key(i))) {
121
+ if (key.indexOf(this.prefix) === 0 && find(this.libraries, key) < 0) {
122
+ _results.push(window.localStorage.removeItem(key));
123
+ } else {
124
+ _results.push(i += 1);
125
+ }
126
+ }
127
+ return _results;
128
+ }
129
+ };
130
+
131
+ window.evalGlobaly = function(src) {
132
+ if (src.length === 0) {
133
+ return;
134
+ }
135
+ if (window.execScript) {
136
+ return window.execScript(src);
137
+ } else {
138
+ return window["eval"](src);
139
+ }
140
+ };
141
+
142
+ this.Preloader = this.CachingPreloader;
143
+
144
+
145
+ /*** src/joosy/extensions/preloaders/inline ***/
146
+
147
+ this.InlinePreloader = {
148
+ load: function(libraries, options) {
149
+ var key, val,
150
+ _this = this;
151
+ for (key in options) {
152
+ val = options[key];
153
+ this[key] = val;
154
+ }
155
+ if (typeof this.start === "function") {
156
+ this.start();
157
+ }
158
+ if (libraries.length > 0) {
159
+ return this.receive(libraries.shift()[0], function() {
160
+ return _this.load(libraries);
161
+ });
162
+ } else {
163
+ return typeof this.complete === "function" ? this.complete() : void 0;
164
+ }
165
+ },
166
+ receive: function(url, callback) {
167
+ var done, head, proceed, script;
168
+ head = document.getElementsByTagName("head")[0];
169
+ script = document.createElement("script");
170
+ script.src = url;
171
+ done = false;
172
+ proceed = function() {
173
+ if (!done && ((this.readyState == null) || this.readyState === "loaded" || this.readyState === "complete")) {
174
+ done = true;
175
+ if (typeof callback === "function") {
176
+ callback();
177
+ }
178
+ return script.onload = script.onreadystatechange = null;
179
+ }
180
+ };
181
+ script.onload = script.onreadystatechange = proceed;
182
+ head.appendChild(script);
183
+ return void 0;
184
+ }
185
+ };
186
+
187
+ this.Preloader = this.InlinePreloader;
188
+
189
+
190
+ /*** src/joosy/extensions/preloaders/index ***/
191
+
192
+
193
+ ;
@@ -0,0 +1,667 @@
1
+
2
+
3
+ /*** src/joosy/extensions/resources/base ***/
4
+
5
+ var __hasProp = {}.hasOwnProperty,
6
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
7
+
8
+ Joosy.Resources.Base = (function(_super) {
9
+ __extends(Base, _super);
10
+
11
+ Base.include(Joosy.Modules.Log);
12
+
13
+ Base.include(Joosy.Modules.Events);
14
+
15
+ Base.prototype.__primaryKey = 'id';
16
+
17
+ Base.prototype.__source = false;
18
+
19
+ Base.resetIdentity = function() {
20
+ return Joosy.Resources.Base.identity = {};
21
+ };
22
+
23
+ Base.beforeLoad = function(action) {
24
+ if (!this.prototype.hasOwnProperty('__beforeLoads')) {
25
+ this.prototype.__beforeLoads = [].concat(this.__super__.__beforeLoads || []);
26
+ }
27
+ return this.prototype.__beforeLoads.push(action);
28
+ };
29
+
30
+ Base.primaryKey = function(primaryKey) {
31
+ return this.prototype.__primaryKey = primaryKey;
32
+ };
33
+
34
+ Base.source = function(location) {
35
+ return this.__source = location;
36
+ };
37
+
38
+ Base.at = function(entity) {
39
+ var Clone, _ref;
40
+ Clone = (function(_super1) {
41
+ __extends(Clone, _super1);
42
+
43
+ function Clone() {
44
+ _ref = Clone.__super__.constructor.apply(this, arguments);
45
+ return _ref;
46
+ }
47
+
48
+ return Clone;
49
+
50
+ })(this);
51
+ if (entity instanceof Joosy.Resources.Base) {
52
+ Clone.__source = entity.memberPath();
53
+ if (this.prototype.__entityName) {
54
+ Clone.__source += '/' + this.prototype.__entityName.pluralize();
55
+ }
56
+ } else {
57
+ Clone.__source = entity;
58
+ }
59
+ return Clone;
60
+ };
61
+
62
+ Base.entity = function(name) {
63
+ return this.prototype.__entityName = name;
64
+ };
65
+
66
+ Base.collection = function(klass) {
67
+ return this.prototype.__collection = function() {
68
+ return klass;
69
+ };
70
+ };
71
+
72
+ Base.prototype.__collection = function() {
73
+ var named;
74
+ named = this.__entityName.camelize().pluralize() + 'Collection';
75
+ if (window[named]) {
76
+ return window[named];
77
+ } else {
78
+ return Joosy.Resources.Collection;
79
+ }
80
+ };
81
+
82
+ Base.map = function(name, klass) {
83
+ if (klass == null) {
84
+ klass = false;
85
+ }
86
+ if (!klass) {
87
+ klass = window[name.singularize().camelize()];
88
+ }
89
+ if (!klass) {
90
+ throw new Error("" + (Joosy.Module.__className(this)) + "> class can not be detected for '" + name + "' mapping");
91
+ }
92
+ return this.beforeLoad(function(data) {
93
+ if (!Joosy.Module.hasAncestor(klass, Joosy.Resources.Base)) {
94
+ klass = klass();
95
+ }
96
+ return this.__map(data, name, klass);
97
+ });
98
+ };
99
+
100
+ Base.build = function(data) {
101
+ var id, key, klass, shim, value, _base, _base1, _ref;
102
+ if (data == null) {
103
+ data = {};
104
+ }
105
+ klass = this.prototype.__entityName;
106
+ (_base = Joosy.Resources.Base).identity || (_base.identity = {});
107
+ (_base1 = Joosy.Resources.Base.identity)[klass] || (_base1[klass] = {});
108
+ shim = function() {
109
+ return shim.__call.apply(shim, arguments);
110
+ };
111
+ if (shim.__proto__) {
112
+ shim.__proto__ = this.prototype;
113
+ } else {
114
+ _ref = this.prototype;
115
+ for (key in _ref) {
116
+ value = _ref[key];
117
+ shim[key] = value;
118
+ }
119
+ }
120
+ shim.constructor = this;
121
+ if (Object.isNumber(data) || Object.isString(data)) {
122
+ id = data;
123
+ data = {};
124
+ data[shim.__primaryKey] = id;
125
+ }
126
+ if (Joosy.Application.identity) {
127
+ id = data[shim.__primaryKey];
128
+ if ((id != null) && Joosy.Resources.Base.identity[klass][id]) {
129
+ shim = Joosy.Resources.Base.identity[klass][id];
130
+ shim.load(data);
131
+ } else {
132
+ Joosy.Resources.Base.identity[klass][id] = shim;
133
+ this.apply(shim, [data]);
134
+ }
135
+ } else {
136
+ this.apply(shim, [data]);
137
+ }
138
+ return shim;
139
+ };
140
+
141
+ function Base(data) {
142
+ if (data == null) {
143
+ data = {};
144
+ }
145
+ this.__fillData(data, false);
146
+ }
147
+
148
+ Base.prototype.id = function() {
149
+ return this.data[this.__primaryKey];
150
+ };
151
+
152
+ Base.prototype.knownAttributes = function() {
153
+ return this.data.keys();
154
+ };
155
+
156
+ Base.prototype.load = function(data) {
157
+ this.__fillData(data);
158
+ return this;
159
+ };
160
+
161
+ Base.prototype.__get = function(path) {
162
+ var target;
163
+ target = this.__callTarget(path);
164
+ if (target[0] instanceof Joosy.Resources.Base) {
165
+ return target[0](target[1]);
166
+ } else {
167
+ return target[0][target[1]];
168
+ }
169
+ };
170
+
171
+ Base.prototype.__set = function(path, value) {
172
+ var target;
173
+ target = this.__callTarget(path);
174
+ if (target[0] instanceof Joosy.Resources.Base) {
175
+ target[0](target[1], value);
176
+ } else {
177
+ target[0][target[1]] = value;
178
+ }
179
+ this.trigger('changed');
180
+ return null;
181
+ };
182
+
183
+ Base.prototype.__callTarget = function(path) {
184
+ var keyword, part, target, _i, _len;
185
+ if (path.has(/\./) && (this.data[path] == null)) {
186
+ path = path.split('.');
187
+ keyword = path.pop();
188
+ target = this.data;
189
+ for (_i = 0, _len = path.length; _i < _len; _i++) {
190
+ part = path[_i];
191
+ target[part] || (target[part] = {});
192
+ if (target instanceof Joosy.Resources.Base) {
193
+ target = target(part);
194
+ } else {
195
+ target = target[part];
196
+ }
197
+ }
198
+ return [target, keyword];
199
+ } else {
200
+ return [this.data, path];
201
+ }
202
+ };
203
+
204
+ Base.prototype.__call = function(path, value) {
205
+ if (arguments.length > 1) {
206
+ return this.__set(path, value);
207
+ } else {
208
+ return this.__get(path);
209
+ }
210
+ };
211
+
212
+ Base.prototype.__fillData = function(data, notify) {
213
+ if (notify == null) {
214
+ notify = true;
215
+ }
216
+ this.raw = data;
217
+ if (!this.hasOwnProperty('data')) {
218
+ this.data = {};
219
+ }
220
+ Joosy.Module.merge(this.data, this.__prepareData(data));
221
+ if (notify) {
222
+ this.trigger('changed');
223
+ }
224
+ return null;
225
+ };
226
+
227
+ Base.prototype.__prepareData = function(data) {
228
+ var bl, name, _i, _len, _ref;
229
+ if (Object.isObject(data) && Object.keys(data).length === 1 && this.__entityName) {
230
+ name = this.__entityName.camelize(false);
231
+ if (data[name]) {
232
+ data = data[name];
233
+ }
234
+ }
235
+ if (this.__beforeLoads != null) {
236
+ _ref = this.__beforeLoads;
237
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
238
+ bl = _ref[_i];
239
+ data = bl.call(this, data);
240
+ }
241
+ }
242
+ return data;
243
+ };
244
+
245
+ Base.prototype.__map = function(data, name, klass) {
246
+ var entry;
247
+ if (Object.isArray(data[name])) {
248
+ entry = new (klass.prototype.__collection())(klass);
249
+ entry.load(data[name]);
250
+ data[name] = entry;
251
+ } else if (Object.isObject(data[name])) {
252
+ data[name] = klass.build(data[name]);
253
+ }
254
+ return data;
255
+ };
256
+
257
+ return Base;
258
+
259
+ })(Joosy.Module);
260
+
261
+
262
+ /*** src/joosy/extensions/resources/collection ***/
263
+
264
+ var __hasProp = {}.hasOwnProperty,
265
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
266
+ __slice = [].slice;
267
+
268
+ Joosy.Resources.Collection = (function(_super) {
269
+ __extends(Collection, _super);
270
+
271
+ Collection.include(Joosy.Modules.Events);
272
+
273
+ Collection.beforeLoad = function(action) {
274
+ return this.prototype.__beforeLoad = action;
275
+ };
276
+
277
+ Collection.model = function(model) {
278
+ return this.prototype.model = model;
279
+ };
280
+
281
+ function Collection(model, findOptions) {
282
+ if (model == null) {
283
+ model = false;
284
+ }
285
+ this.findOptions = findOptions;
286
+ if (model) {
287
+ this.model = model;
288
+ }
289
+ this.data = [];
290
+ if (!this.model) {
291
+ throw new Error("" + (Joosy.Module.__className(this)) + "> model can't be empty");
292
+ }
293
+ }
294
+
295
+ Collection.prototype.load = function(entities, notify) {
296
+ if (notify == null) {
297
+ notify = true;
298
+ }
299
+ if (this.__beforeLoad != null) {
300
+ entities = this.__beforeLoad(entities);
301
+ }
302
+ this.data = this.modelize(entities);
303
+ if (notify) {
304
+ this.trigger('changed');
305
+ }
306
+ return this;
307
+ };
308
+
309
+ Collection.prototype.modelize = function(collection) {
310
+ var root,
311
+ _this = this;
312
+ root = this.model.prototype.__entityName.pluralize();
313
+ if (!(collection instanceof Array)) {
314
+ collection = collection != null ? collection[root.camelize(false)] : void 0;
315
+ if (!(collection instanceof Array)) {
316
+ throw new Error("Can not read incoming JSON");
317
+ }
318
+ }
319
+ return collection.map(function(x) {
320
+ return _this.model.build(x);
321
+ });
322
+ };
323
+
324
+ Collection.prototype.each = function(callback) {
325
+ return this.data.each(callback);
326
+ };
327
+
328
+ Collection.prototype.size = function() {
329
+ return this.data.length;
330
+ };
331
+
332
+ Collection.prototype.find = function(description) {
333
+ return this.data.find(description);
334
+ };
335
+
336
+ Collection.prototype.sortBy = function() {
337
+ var params, _ref;
338
+ params = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
339
+ return (_ref = this.data).sortBy.apply(_ref, params);
340
+ };
341
+
342
+ Collection.prototype.findById = function(id) {
343
+ return this.data.find(function(x) {
344
+ return x.id().toString() === id.toString();
345
+ });
346
+ };
347
+
348
+ Collection.prototype.at = function(i) {
349
+ return this.data[i];
350
+ };
351
+
352
+ Collection.prototype.remove = function(target, notify) {
353
+ var index, result;
354
+ if (notify == null) {
355
+ notify = true;
356
+ }
357
+ if (Object.isNumber(target)) {
358
+ index = target;
359
+ } else {
360
+ index = this.data.indexOf(target);
361
+ }
362
+ if (index >= 0) {
363
+ result = this.data.splice(index, 1)[0];
364
+ if (notify) {
365
+ this.trigger('changed');
366
+ }
367
+ }
368
+ return result;
369
+ };
370
+
371
+ Collection.prototype.add = function(element, index, notify) {
372
+ if (index == null) {
373
+ index = false;
374
+ }
375
+ if (notify == null) {
376
+ notify = true;
377
+ }
378
+ if (typeof index === 'number') {
379
+ this.data.splice(index, 0, element);
380
+ } else {
381
+ this.data.push(element);
382
+ }
383
+ if (notify) {
384
+ this.trigger('changed');
385
+ }
386
+ return element;
387
+ };
388
+
389
+ return Collection;
390
+
391
+ })(Joosy.Module);
392
+
393
+
394
+ /*** src/joosy/extensions/resources/rest ***/
395
+
396
+ var _ref,
397
+ __hasProp = {}.hasOwnProperty,
398
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
399
+
400
+ Joosy.Resources.REST = (function(_super) {
401
+ __extends(REST, _super);
402
+
403
+ function REST() {
404
+ _ref = REST.__super__.constructor.apply(this, arguments);
405
+ return _ref;
406
+ }
407
+
408
+ REST.prototype.__collection = function() {
409
+ var named;
410
+ named = this.__entityName.camelize().pluralize() + 'Collection';
411
+ if (window[named]) {
412
+ return window[named];
413
+ } else {
414
+ return Joosy.Resources.RESTCollection;
415
+ }
416
+ };
417
+
418
+ REST.__parentsPath = function(parents) {
419
+ return parents.reduce(function(path, parent) {
420
+ return path += Joosy.Module.hasAncestor(parent.constructor, Joosy.Resources.REST) ? parent.memberPath() : parent;
421
+ }, '');
422
+ };
423
+
424
+ REST.basePath = function(options) {
425
+ var path;
426
+ if (options == null) {
427
+ options = {};
428
+ }
429
+ if ((this.__source != null) && (options.parent == null)) {
430
+ path = this.__source;
431
+ } else {
432
+ path = '/';
433
+ if (this.__namespace__.length > 0) {
434
+ path += this.__namespace__.map(function(s) {
435
+ return s.toLowerCase();
436
+ }).join('/') + '/';
437
+ }
438
+ path += this.prototype.__entityName.pluralize();
439
+ }
440
+ if (options.parent != null) {
441
+ path = this.__parentsPath(Object.isArray(options.parent) ? options.parent : [options.parent]) + path;
442
+ }
443
+ return path;
444
+ };
445
+
446
+ REST.prototype.basePath = function(options) {
447
+ if (options == null) {
448
+ options = {};
449
+ }
450
+ return this.constructor.basePath(options);
451
+ };
452
+
453
+ REST.memberPath = function(id, options) {
454
+ var path;
455
+ if (options == null) {
456
+ options = {};
457
+ }
458
+ path = this.basePath(options) + ("/" + id);
459
+ if (options.from != null) {
460
+ path += "/" + options.from;
461
+ }
462
+ return path;
463
+ };
464
+
465
+ REST.prototype.memberPath = function(options) {
466
+ if (options == null) {
467
+ options = {};
468
+ }
469
+ return this.constructor.memberPath(this.id(), options);
470
+ };
471
+
472
+ REST.collectionPath = function(options) {
473
+ var path;
474
+ if (options == null) {
475
+ options = {};
476
+ }
477
+ path = this.basePath(options);
478
+ if (options.from != null) {
479
+ path += "/" + options.from;
480
+ }
481
+ return path;
482
+ };
483
+
484
+ REST.prototype.collectionPath = function(options) {
485
+ if (options == null) {
486
+ options = {};
487
+ }
488
+ return this.constructor.collectionPath(options);
489
+ };
490
+
491
+ REST.get = function(options, callback) {
492
+ if (Object.isFunction(options)) {
493
+ callback = options;
494
+ options = {};
495
+ }
496
+ return this.__query(this.collectionPath(options), 'GET', options.params, callback);
497
+ };
498
+
499
+ REST.post = function(options, callback) {
500
+ if (Object.isFunction(options)) {
501
+ callback = options;
502
+ options = {};
503
+ }
504
+ return this.__query(this.collectionPath(options), 'POST', options.params, callback);
505
+ };
506
+
507
+ REST.put = function(options, callback) {
508
+ if (Object.isFunction(options)) {
509
+ callback = options;
510
+ options = {};
511
+ }
512
+ return this.__query(this.collectionPath(options), 'PUT', options.params, callback);
513
+ };
514
+
515
+ REST["delete"] = function(options, callback) {
516
+ if (Object.isFunction(options)) {
517
+ callback = options;
518
+ options = {};
519
+ }
520
+ return this.__query(this.collectionPath(options), 'DELETE', options.params, callback);
521
+ };
522
+
523
+ REST.prototype.get = function(options, callback) {
524
+ if (Object.isFunction(options)) {
525
+ callback = options;
526
+ options = {};
527
+ }
528
+ return this.constructor.__query(this.memberPath(options), 'GET', options.params, callback);
529
+ };
530
+
531
+ REST.prototype.post = function(options, callback) {
532
+ if (Object.isFunction(options)) {
533
+ callback = options;
534
+ options = {};
535
+ }
536
+ return this.constructor.__query(this.memberPath(options), 'POST', options.params, callback);
537
+ };
538
+
539
+ REST.prototype.put = function(options, callback) {
540
+ if (Object.isFunction(options)) {
541
+ callback = options;
542
+ options = {};
543
+ }
544
+ return this.constructor.__query(this.memberPath(options), 'PUT', options.params, callback);
545
+ };
546
+
547
+ REST.prototype["delete"] = function(options, callback) {
548
+ if (Object.isFunction(options)) {
549
+ callback = options;
550
+ options = {};
551
+ }
552
+ return this.constructor.__query(this.memberPath(options), 'DELETE', options.params, callback);
553
+ };
554
+
555
+ REST.find = function(where, options, callback) {
556
+ var result,
557
+ _this = this;
558
+ if (options == null) {
559
+ options = {};
560
+ }
561
+ if (callback == null) {
562
+ callback = false;
563
+ }
564
+ if (Object.isFunction(options)) {
565
+ callback = options;
566
+ options = {};
567
+ }
568
+ if (where === 'all') {
569
+ result = new (this.prototype.__collection())(this, options);
570
+ this.__query(this.collectionPath(options), 'GET', options.params, function(data) {
571
+ result.load(data);
572
+ return typeof callback === "function" ? callback(result, data) : void 0;
573
+ });
574
+ } else {
575
+ result = this.build(where);
576
+ this.__query(this.memberPath(where, options), 'GET', options.params, function(data) {
577
+ result.load(data);
578
+ return typeof callback === "function" ? callback(result, data) : void 0;
579
+ });
580
+ }
581
+ return result;
582
+ };
583
+
584
+ REST.__query = function(path, method, params, callback) {
585
+ var options;
586
+ options = {
587
+ data: params,
588
+ type: method,
589
+ cache: false,
590
+ dataType: 'json'
591
+ };
592
+ if (Object.isFunction(callback)) {
593
+ options.success = callback;
594
+ } else {
595
+ Joosy.Module.merge(options, callback);
596
+ }
597
+ return $.ajax(path, options);
598
+ };
599
+
600
+ REST.prototype.reload = function(options, callback) {
601
+ var _this = this;
602
+ if (options == null) {
603
+ options = {};
604
+ }
605
+ if (callback == null) {
606
+ callback = false;
607
+ }
608
+ if (Object.isFunction(options)) {
609
+ callback = options;
610
+ options = {};
611
+ }
612
+ return this.constructor.__query(this.memberPath(options), 'GET', options.params, function(data) {
613
+ _this.load(data);
614
+ return typeof callback === "function" ? callback(_this) : void 0;
615
+ });
616
+ };
617
+
618
+ return REST;
619
+
620
+ })(Joosy.Resources.Base);
621
+
622
+
623
+ /*** src/joosy/extensions/resources/rest_collection ***/
624
+
625
+ var _ref,
626
+ __hasProp = {}.hasOwnProperty,
627
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
628
+
629
+ Joosy.Resources.RESTCollection = (function(_super) {
630
+ __extends(RESTCollection, _super);
631
+
632
+ function RESTCollection() {
633
+ _ref = RESTCollection.__super__.constructor.apply(this, arguments);
634
+ return _ref;
635
+ }
636
+
637
+ RESTCollection.include(Joosy.Modules.Log);
638
+
639
+ RESTCollection.include(Joosy.Modules.Events);
640
+
641
+ RESTCollection.prototype.reload = function(options, callback) {
642
+ var _this = this;
643
+ if (options == null) {
644
+ options = {};
645
+ }
646
+ if (callback == null) {
647
+ callback = false;
648
+ }
649
+ if (Object.isFunction(options)) {
650
+ callback = options;
651
+ options = {};
652
+ }
653
+ return this.model.__query(this.model.collectionPath(options), 'GET', options.params, function(data) {
654
+ _this.load(data);
655
+ return typeof callback === "function" ? callback(data) : void 0;
656
+ });
657
+ };
658
+
659
+ return RESTCollection;
660
+
661
+ })(Joosy.Resources.Collection);
662
+
663
+
664
+ /*** src/joosy/extensions/resources/index ***/
665
+
666
+
667
+ ;