spine-rails 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,808 +0,0 @@
1
- (function() {
2
- var $, Controller, Events, Log, Model, Module, Spine, isArray, isBlank, makeArray, moduleKeywords,
3
- __slice = Array.prototype.slice,
4
- __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
5
- __hasProp = Object.prototype.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
- __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
8
-
9
- Events = {
10
- bind: function(ev, callback) {
11
- var calls, evs, name, _i, _len;
12
- evs = ev.split(' ');
13
- calls = this.hasOwnProperty('_callbacks') && this._callbacks || (this._callbacks = {});
14
- for (_i = 0, _len = evs.length; _i < _len; _i++) {
15
- name = evs[_i];
16
- calls[name] || (calls[name] = []);
17
- calls[name].push(callback);
18
- }
19
- return this;
20
- },
21
- one: function(ev, callback) {
22
- return this.bind(ev, function() {
23
- this.unbind(ev, arguments.callee);
24
- return callback.apply(this, arguments);
25
- });
26
- },
27
- trigger: function() {
28
- var args, callback, ev, list, _i, _len, _ref;
29
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
30
- ev = args.shift();
31
- list = this.hasOwnProperty('_callbacks') && ((_ref = this._callbacks) != null ? _ref[ev] : void 0);
32
- if (!list) return;
33
- for (_i = 0, _len = list.length; _i < _len; _i++) {
34
- callback = list[_i];
35
- if (callback.apply(this, args) === false) break;
36
- }
37
- return true;
38
- },
39
- unbind: function(ev, callback) {
40
- var cb, i, list, _len, _ref;
41
- if (!ev) {
42
- this._callbacks = {};
43
- return this;
44
- }
45
- list = (_ref = this._callbacks) != null ? _ref[ev] : void 0;
46
- if (!list) return this;
47
- if (!callback) {
48
- delete this._callbacks[ev];
49
- return this;
50
- }
51
- for (i = 0, _len = list.length; i < _len; i++) {
52
- cb = list[i];
53
- if (!(cb === callback)) continue;
54
- list = list.slice();
55
- list.splice(i, 1);
56
- this._callbacks[ev] = list;
57
- break;
58
- }
59
- return this;
60
- }
61
- };
62
-
63
- Log = {
64
- trace: true,
65
- logPrefix: '(App)',
66
- log: function() {
67
- var args;
68
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
69
- if (!this.trace) return;
70
- if (this.logPrefix) args.unshift(this.logPrefix);
71
- if (typeof console !== "undefined" && console !== null) {
72
- if (typeof console.log === "function") console.log.apply(console, args);
73
- }
74
- return this;
75
- }
76
- };
77
-
78
- moduleKeywords = ['included', 'extended'];
79
-
80
- Module = (function() {
81
-
82
- Module.include = function(obj) {
83
- var key, value, _ref;
84
- if (!obj) throw 'include(obj) requires obj';
85
- for (key in obj) {
86
- value = obj[key];
87
- if (__indexOf.call(moduleKeywords, key) < 0) this.prototype[key] = value;
88
- }
89
- if ((_ref = obj.included) != null) _ref.apply(this);
90
- return this;
91
- };
92
-
93
- Module.extend = function(obj) {
94
- var key, value, _ref;
95
- if (!obj) throw 'extend(obj) requires obj';
96
- for (key in obj) {
97
- value = obj[key];
98
- if (__indexOf.call(moduleKeywords, key) < 0) this[key] = value;
99
- }
100
- if ((_ref = obj.extended) != null) _ref.apply(this);
101
- return this;
102
- };
103
-
104
- Module.proxy = function(func) {
105
- var _this = this;
106
- return function() {
107
- return func.apply(_this, arguments);
108
- };
109
- };
110
-
111
- Module.prototype.proxy = function(func) {
112
- var _this = this;
113
- return function() {
114
- return func.apply(_this, arguments);
115
- };
116
- };
117
-
118
- function Module() {
119
- if (typeof this.init === "function") this.init.apply(this, arguments);
120
- }
121
-
122
- return Module;
123
-
124
- })();
125
-
126
- Model = (function(_super) {
127
-
128
- __extends(Model, _super);
129
-
130
- Model.extend(Events);
131
-
132
- Model.records = {};
133
-
134
- Model.crecords = {};
135
-
136
- Model.attributes = [];
137
-
138
- Model.configure = function() {
139
- var attributes, name;
140
- name = arguments[0], attributes = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
141
- this.className = name;
142
- this.records = {};
143
- this.crecords = {};
144
- if (attributes.length) this.attributes = attributes;
145
- this.attributes && (this.attributes = makeArray(this.attributes));
146
- this.attributes || (this.attributes = []);
147
- this.unbind();
148
- return this;
149
- };
150
-
151
- Model.toString = function() {
152
- return "" + this.className + "(" + (this.attributes.join(", ")) + ")";
153
- };
154
-
155
- Model.find = function(id) {
156
- var record;
157
- record = this.records[id];
158
- if (!record && ("" + id).match(/c-\d+/)) return this.findCID(id);
159
- if (!record) throw 'Unknown record';
160
- return record.clone();
161
- };
162
-
163
- Model.findCID = function(cid) {
164
- var record;
165
- record = this.crecords[cid];
166
- if (!record) throw 'Unknown record';
167
- return record.clone();
168
- };
169
-
170
- Model.exists = function(id) {
171
- try {
172
- return this.find(id);
173
- } catch (e) {
174
- return false;
175
- }
176
- };
177
-
178
- Model.refresh = function(values, options) {
179
- var record, records, _i, _len;
180
- if (options == null) options = {};
181
- if (options.clear) {
182
- this.records = {};
183
- this.crecords = {};
184
- }
185
- records = this.fromJSON(values);
186
- if (!isArray(records)) records = [records];
187
- for (_i = 0, _len = records.length; _i < _len; _i++) {
188
- record = records[_i];
189
- record.id || (record.id = record.cid);
190
- this.records[record.id] = record;
191
- this.crecords[record.cid] = record;
192
- }
193
- this.trigger('refresh', !options.clear && this.cloneArray(records));
194
- return this;
195
- };
196
-
197
- Model.select = function(callback) {
198
- var id, record, result;
199
- result = (function() {
200
- var _ref, _results;
201
- _ref = this.records;
202
- _results = [];
203
- for (id in _ref) {
204
- record = _ref[id];
205
- if (callback(record)) _results.push(record);
206
- }
207
- return _results;
208
- }).call(this);
209
- return this.cloneArray(result);
210
- };
211
-
212
- Model.findByAttribute = function(name, value) {
213
- var id, record, _ref;
214
- _ref = this.records;
215
- for (id in _ref) {
216
- record = _ref[id];
217
- if (record[name] === value) return record.clone();
218
- }
219
- return null;
220
- };
221
-
222
- Model.findAllByAttribute = function(name, value) {
223
- return this.select(function(item) {
224
- return item[name] === value;
225
- });
226
- };
227
-
228
- Model.each = function(callback) {
229
- var key, value, _ref, _results;
230
- _ref = this.records;
231
- _results = [];
232
- for (key in _ref) {
233
- value = _ref[key];
234
- _results.push(callback(value.clone()));
235
- }
236
- return _results;
237
- };
238
-
239
- Model.all = function() {
240
- return this.cloneArray(this.recordsValues());
241
- };
242
-
243
- Model.first = function() {
244
- var record;
245
- record = this.recordsValues()[0];
246
- return record != null ? record.clone() : void 0;
247
- };
248
-
249
- Model.last = function() {
250
- var record, values;
251
- values = this.recordsValues();
252
- record = values[values.length - 1];
253
- return record != null ? record.clone() : void 0;
254
- };
255
-
256
- Model.count = function() {
257
- return this.recordsValues().length;
258
- };
259
-
260
- Model.deleteAll = function() {
261
- var key, value, _ref, _results;
262
- _ref = this.records;
263
- _results = [];
264
- for (key in _ref) {
265
- value = _ref[key];
266
- _results.push(delete this.records[key]);
267
- }
268
- return _results;
269
- };
270
-
271
- Model.destroyAll = function() {
272
- var key, value, _ref, _results;
273
- _ref = this.records;
274
- _results = [];
275
- for (key in _ref) {
276
- value = _ref[key];
277
- _results.push(this.records[key].destroy());
278
- }
279
- return _results;
280
- };
281
-
282
- Model.update = function(id, atts, options) {
283
- return this.find(id).updateAttributes(atts, options);
284
- };
285
-
286
- Model.create = function(atts, options) {
287
- var record;
288
- record = new this(atts);
289
- return record.save(options);
290
- };
291
-
292
- Model.destroy = function(id, options) {
293
- return this.find(id).destroy(options);
294
- };
295
-
296
- Model.change = function(callbackOrParams) {
297
- if (typeof callbackOrParams === 'function') {
298
- return this.bind('change', callbackOrParams);
299
- } else {
300
- return this.trigger('change', callbackOrParams);
301
- }
302
- };
303
-
304
- Model.fetch = function(callbackOrParams) {
305
- if (typeof callbackOrParams === 'function') {
306
- return this.bind('fetch', callbackOrParams);
307
- } else {
308
- return this.trigger('fetch', callbackOrParams);
309
- }
310
- };
311
-
312
- Model.toJSON = function() {
313
- return this.recordsValues();
314
- };
315
-
316
- Model.fromJSON = function(objects) {
317
- var value, _i, _len, _results;
318
- if (!objects) return;
319
- if (typeof objects === 'string') objects = JSON.parse(objects);
320
- if (isArray(objects)) {
321
- _results = [];
322
- for (_i = 0, _len = objects.length; _i < _len; _i++) {
323
- value = objects[_i];
324
- _results.push(new this(value));
325
- }
326
- return _results;
327
- } else {
328
- return new this(objects);
329
- }
330
- };
331
-
332
- Model.fromForm = function() {
333
- var _ref;
334
- return (_ref = new this).fromForm.apply(_ref, arguments);
335
- };
336
-
337
- Model.recordsValues = function() {
338
- var key, result, value, _ref;
339
- result = [];
340
- _ref = this.records;
341
- for (key in _ref) {
342
- value = _ref[key];
343
- result.push(value);
344
- }
345
- return result;
346
- };
347
-
348
- Model.cloneArray = function(array) {
349
- var value, _i, _len, _results;
350
- _results = [];
351
- for (_i = 0, _len = array.length; _i < _len; _i++) {
352
- value = array[_i];
353
- _results.push(value.clone());
354
- }
355
- return _results;
356
- };
357
-
358
- Model.idCounter = 0;
359
-
360
- Model.uid = function() {
361
- return this.idCounter++;
362
- };
363
-
364
- function Model(atts) {
365
- Model.__super__.constructor.apply(this, arguments);
366
- if (atts) this.load(atts);
367
- this.cid || (this.cid = 'c-' + this.constructor.uid());
368
- }
369
-
370
- Model.prototype.isNew = function() {
371
- return !this.exists();
372
- };
373
-
374
- Model.prototype.isValid = function() {
375
- return !this.validate();
376
- };
377
-
378
- Model.prototype.validate = function() {};
379
-
380
- Model.prototype.load = function(atts) {
381
- var key, value;
382
- for (key in atts) {
383
- value = atts[key];
384
- if (typeof this[key] === 'function') {
385
- this[key](value);
386
- } else {
387
- this[key] = value;
388
- }
389
- }
390
- return this;
391
- };
392
-
393
- Model.prototype.attributes = function() {
394
- var key, result, _i, _len, _ref;
395
- result = {};
396
- _ref = this.constructor.attributes;
397
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
398
- key = _ref[_i];
399
- if (key in this) {
400
- if (typeof this[key] === 'function') {
401
- result[key] = this[key]();
402
- } else {
403
- result[key] = this[key];
404
- }
405
- }
406
- }
407
- if (this.id) result.id = this.id;
408
- return result;
409
- };
410
-
411
- Model.prototype.eql = function(rec) {
412
- return !!(rec && rec.constructor === this.constructor && (rec.id === this.id || rec.cid === this.cid));
413
- };
414
-
415
- Model.prototype.save = function(options) {
416
- var error, record;
417
- if (options == null) options = {};
418
- if (options.validate !== false) {
419
- error = this.validate();
420
- if (error) {
421
- this.trigger('error', error);
422
- return false;
423
- }
424
- }
425
- this.trigger('beforeSave', options);
426
- record = this.isNew() ? this.create(options) : this.update(options);
427
- this.trigger('save', options);
428
- return record;
429
- };
430
-
431
- Model.prototype.updateAttribute = function(name, value) {
432
- this[name] = value;
433
- return this.save();
434
- };
435
-
436
- Model.prototype.updateAttributes = function(atts, options) {
437
- this.load(atts);
438
- return this.save(options);
439
- };
440
-
441
- Model.prototype.changeID = function(id) {
442
- var records;
443
- records = this.constructor.records;
444
- records[id] = records[this.id];
445
- delete records[this.id];
446
- this.id = id;
447
- return this.save();
448
- };
449
-
450
- Model.prototype.destroy = function(options) {
451
- if (options == null) options = {};
452
- this.trigger('beforeDestroy', options);
453
- delete this.constructor.records[this.id];
454
- delete this.constructor.crecords[this.cid];
455
- this.destroyed = true;
456
- this.trigger('destroy', options);
457
- this.trigger('change', 'destroy', options);
458
- this.unbind();
459
- return this;
460
- };
461
-
462
- Model.prototype.dup = function(newRecord) {
463
- var result;
464
- result = new this.constructor(this.attributes());
465
- if (newRecord === false) {
466
- result.cid = this.cid;
467
- } else {
468
- delete result.id;
469
- }
470
- return result;
471
- };
472
-
473
- Model.prototype.clone = function() {
474
- return Object.create(this);
475
- };
476
-
477
- Model.prototype.reload = function() {
478
- var original;
479
- if (this.isNew()) return this;
480
- original = this.constructor.find(this.id);
481
- this.load(original.attributes());
482
- return original;
483
- };
484
-
485
- Model.prototype.toJSON = function() {
486
- return this.attributes();
487
- };
488
-
489
- Model.prototype.toString = function() {
490
- return "<" + this.constructor.className + " (" + (JSON.stringify(this)) + ")>";
491
- };
492
-
493
- Model.prototype.fromForm = function(form) {
494
- var key, result, _i, _len, _ref;
495
- result = {};
496
- _ref = $(form).serializeArray();
497
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
498
- key = _ref[_i];
499
- result[key.name] = key.value;
500
- }
501
- return this.load(result);
502
- };
503
-
504
- Model.prototype.exists = function() {
505
- return this.id && this.id in this.constructor.records;
506
- };
507
-
508
- Model.prototype.update = function(options) {
509
- var clone, records;
510
- this.trigger('beforeUpdate', options);
511
- records = this.constructor.records;
512
- records[this.id].load(this.attributes());
513
- clone = records[this.id].clone();
514
- clone.trigger('update', options);
515
- clone.trigger('change', 'update', options);
516
- return clone;
517
- };
518
-
519
- Model.prototype.create = function(options) {
520
- var clone, record;
521
- this.trigger('beforeCreate', options);
522
- if (!this.id) this.id = this.cid;
523
- record = this.dup(false);
524
- this.constructor.records[this.id] = record;
525
- this.constructor.crecords[this.cid] = record;
526
- clone = record.clone();
527
- clone.trigger('create', options);
528
- clone.trigger('change', 'create', options);
529
- return clone;
530
- };
531
-
532
- Model.prototype.bind = function(events, callback) {
533
- var binder, unbinder,
534
- _this = this;
535
- this.constructor.bind(events, binder = function(record) {
536
- if (record && _this.eql(record)) return callback.apply(_this, arguments);
537
- });
538
- this.constructor.bind('unbind', unbinder = function(record) {
539
- if (record && _this.eql(record)) {
540
- _this.constructor.unbind(events, binder);
541
- return _this.constructor.unbind('unbind', unbinder);
542
- }
543
- });
544
- return binder;
545
- };
546
-
547
- Model.prototype.one = function(events, callback) {
548
- var binder,
549
- _this = this;
550
- return binder = this.bind(events, function() {
551
- _this.constructor.unbind(events, binder);
552
- return callback.apply(_this);
553
- });
554
- };
555
-
556
- Model.prototype.trigger = function() {
557
- var args, _ref;
558
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
559
- args.splice(1, 0, this);
560
- return (_ref = this.constructor).trigger.apply(_ref, args);
561
- };
562
-
563
- Model.prototype.unbind = function() {
564
- return this.trigger('unbind');
565
- };
566
-
567
- return Model;
568
-
569
- })(Module);
570
-
571
- Controller = (function(_super) {
572
-
573
- __extends(Controller, _super);
574
-
575
- Controller.include(Events);
576
-
577
- Controller.include(Log);
578
-
579
- Controller.prototype.eventSplitter = /^(\S+)\s*(.*)$/;
580
-
581
- Controller.prototype.tag = 'div';
582
-
583
- function Controller(options) {
584
- this.release = __bind(this.release, this);
585
- var key, value, _ref;
586
- this.options = options;
587
- _ref = this.options;
588
- for (key in _ref) {
589
- value = _ref[key];
590
- this[key] = value;
591
- }
592
- if (!this.el) this.el = document.createElement(this.tag);
593
- this.el = $(this.el);
594
- if (this.className) this.el.addClass(this.className);
595
- if (this.attributes) this.el.attr(this.attributes);
596
- this.release(function() {
597
- return this.el.remove();
598
- });
599
- if (!this.events) this.events = this.constructor.events;
600
- if (!this.elements) this.elements = this.constructor.elements;
601
- if (this.events) this.delegateEvents();
602
- if (this.elements) this.refreshElements();
603
- Controller.__super__.constructor.apply(this, arguments);
604
- }
605
-
606
- Controller.prototype.release = function(callback) {
607
- if (typeof callback === 'function') {
608
- return this.bind('release', callback);
609
- } else {
610
- return this.trigger('release');
611
- }
612
- };
613
-
614
- Controller.prototype.$ = function(selector) {
615
- return $(selector, this.el);
616
- };
617
-
618
- Controller.prototype.delegateEvents = function() {
619
- var eventName, key, match, method, selector, _ref, _results;
620
- _ref = this.events;
621
- _results = [];
622
- for (key in _ref) {
623
- method = _ref[key];
624
- if (typeof method !== 'function') method = this.proxy(this[method]);
625
- match = key.match(this.eventSplitter);
626
- eventName = match[1];
627
- selector = match[2];
628
- if (selector === '') {
629
- _results.push(this.el.bind(eventName, method));
630
- } else {
631
- _results.push(this.el.delegate(selector, eventName, method));
632
- }
633
- }
634
- return _results;
635
- };
636
-
637
- Controller.prototype.refreshElements = function() {
638
- var key, value, _ref, _results;
639
- _ref = this.elements;
640
- _results = [];
641
- for (key in _ref) {
642
- value = _ref[key];
643
- _results.push(this[value] = this.$(key));
644
- }
645
- return _results;
646
- };
647
-
648
- Controller.prototype.delay = function(func, timeout) {
649
- return setTimeout(this.proxy(func), timeout || 0);
650
- };
651
-
652
- Controller.prototype.html = function(element) {
653
- this.el.html(element.el || element);
654
- this.refreshElements();
655
- return this.el;
656
- };
657
-
658
- Controller.prototype.append = function() {
659
- var e, elements, _ref;
660
- elements = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
661
- elements = (function() {
662
- var _i, _len, _results;
663
- _results = [];
664
- for (_i = 0, _len = elements.length; _i < _len; _i++) {
665
- e = elements[_i];
666
- _results.push(e.el || e);
667
- }
668
- return _results;
669
- })();
670
- (_ref = this.el).append.apply(_ref, elements);
671
- this.refreshElements();
672
- return this.el;
673
- };
674
-
675
- Controller.prototype.appendTo = function(element) {
676
- this.el.appendTo(element.el || element);
677
- this.refreshElements();
678
- return this.el;
679
- };
680
-
681
- Controller.prototype.prepend = function() {
682
- var e, elements, _ref;
683
- elements = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
684
- elements = (function() {
685
- var _i, _len, _results;
686
- _results = [];
687
- for (_i = 0, _len = elements.length; _i < _len; _i++) {
688
- e = elements[_i];
689
- _results.push(e.el || e);
690
- }
691
- return _results;
692
- })();
693
- (_ref = this.el).prepend.apply(_ref, elements);
694
- this.refreshElements();
695
- return this.el;
696
- };
697
-
698
- Controller.prototype.replace = function(element) {
699
- var previous, _ref;
700
- _ref = [this.el, $(element.el || element)], previous = _ref[0], this.el = _ref[1];
701
- previous.replaceWith(this.el);
702
- this.delegateEvents();
703
- this.refreshElements();
704
- return this.el;
705
- };
706
-
707
- return Controller;
708
-
709
- })(Module);
710
-
711
- $ = (typeof window !== "undefined" && window !== null ? window.jQuery : void 0) || (typeof window !== "undefined" && window !== null ? window.Zepto : void 0) || function(element) {
712
- return element;
713
- };
714
-
715
- if (typeof Object.create !== 'function') {
716
- Object.create = function(o) {
717
- var Func;
718
- Func = function() {};
719
- Func.prototype = o;
720
- return new Func();
721
- };
722
- }
723
-
724
- isArray = function(value) {
725
- return Object.prototype.toString.call(value) === '[object Array]';
726
- };
727
-
728
- isBlank = function(value) {
729
- var key;
730
- if (!value) return true;
731
- for (key in value) {
732
- return false;
733
- }
734
- return true;
735
- };
736
-
737
- makeArray = function(args) {
738
- return Array.prototype.slice.call(args, 0);
739
- };
740
-
741
- Spine = this.Spine = {};
742
-
743
- if (typeof module !== "undefined" && module !== null) module.exports = Spine;
744
-
745
- Spine.version = '1.0.5';
746
-
747
- Spine.isArray = isArray;
748
-
749
- Spine.isBlank = isBlank;
750
-
751
- Spine.$ = $;
752
-
753
- Spine.Events = Events;
754
-
755
- Spine.Log = Log;
756
-
757
- Spine.Module = Module;
758
-
759
- Spine.Controller = Controller;
760
-
761
- Spine.Model = Model;
762
-
763
- Module.extend.call(Spine, Events);
764
-
765
- Module.create = Module.sub = Controller.create = Controller.sub = Model.sub = function(instances, statics) {
766
- var result;
767
- result = (function(_super) {
768
-
769
- __extends(result, _super);
770
-
771
- function result() {
772
- result.__super__.constructor.apply(this, arguments);
773
- }
774
-
775
- return result;
776
-
777
- })(this);
778
- if (instances) result.include(instances);
779
- if (statics) result.extend(statics);
780
- if (typeof result.unbind === "function") result.unbind();
781
- return result;
782
- };
783
-
784
- Model.setup = function(name, attributes) {
785
- var Instance;
786
- if (attributes == null) attributes = [];
787
- Instance = (function(_super) {
788
-
789
- __extends(Instance, _super);
790
-
791
- function Instance() {
792
- Instance.__super__.constructor.apply(this, arguments);
793
- }
794
-
795
- return Instance;
796
-
797
- })(this);
798
- Instance.configure.apply(Instance, [name].concat(__slice.call(attributes)));
799
- return Instance;
800
- };
801
-
802
- Module.init = Controller.init = Model.init = function(a1, a2, a3, a4, a5) {
803
- return new this(a1, a2, a3, a4, a5);
804
- };
805
-
806
- Spine.Class = Module;
807
-
808
- }).call(this);