Repository navigation
Expand file tree
/
Copy pathspine.coffee
More file actions
535 lines (415 loc) · 11.3 KB
/
Copy pathspine.coffee
File metadata and controls
535 lines (415 loc) · 11.3 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
Events =
bind: (ev, callback) ->
evs = ev.split(' ')
calls = @hasOwnProperty('_callbacks') and @_callbacks or= {}
for name in evs
calls[name] or= []
calls[name].push(callback)
this
one: (ev, callback) ->
@bind ev, ->
@unbind(ev, arguments.callee)
callback.apply(this, arguments)
trigger: (args...) ->
ev = args.shift()
list = @hasOwnProperty('_callbacks') and @_callbacks?[ev]
return unless list
for callback in list
if callback.apply(this, args) is false
break
true
unbind: (ev, callback) ->
unless ev
@_callbacks = {}
return this
list = @_callbacks?[ev]
return this unless list
unless callback
delete @_callbacks[ev]
return this
for cb, i in list when cb is callback
list = list.slice()
list.splice(i, 1)
@_callbacks[ev] = list
break
this
Log =
trace: true
logPrefix: '(App)'
log: (args...) ->
return unless @trace
if @logPrefix then args.unshift(@logPrefix)
console?.log?(args...)
this
moduleKeywords = ['included', 'extended']
class Module
@include: (obj) ->
throw new Error('include(obj) requires obj') unless obj
for key, value of obj when key not in moduleKeywords
@::[key] = value
obj.included?.apply(this)
this
@extend: (obj) ->
throw new Error('extend(obj) requires obj') unless obj
for key, value of obj when key not in moduleKeywords
@[key] = value
obj.extended?.apply(this)
this
@proxy: (func) ->
=> func.apply(this, arguments)
proxy: (func) ->
=> func.apply(this, arguments)
constructor: ->
@init?(arguments...)
class Model extends Module
@extend Events
@records: {}
@crecords: {}
@attributes: []
@configure: (name, attributes...) ->
@className = name
@records = {}
@crecords = {}
@attributes = attributes if attributes.length
@attributes and= makeArray(@attributes)
@attributes or= []
@unbind()
this
@toString: -> "#{@className}(#{@attributes.join(", ")})"
@find: (id) ->
record = @records[id]
if !record and ("#{id}").match(/c-\d+/)
return @findCID(id)
throw new Error('Unknown record') unless record
record.clone()
@findCID: (cid) ->
record = @crecords[cid]
throw new Error('Unknown record') unless record
record.clone()
@exists: (id) ->
try
return @find(id)
catch e
return false
@refresh: (values, options = {}) ->
if options.clear
@records = {}
@crecords = {}
records = @fromJSON(values)
records = [records] unless isArray(records)
for record in records
record.id or= record.cid
@records[record.id] = record
@crecords[record.cid] = record
@trigger('refresh', @cloneArray(records))
this
@select: (callback) ->
result = (record for id, record of @records when callback(record))
@cloneArray(result)
@findByAttribute: (name, value) ->
for id, record of @records
if record[name] is value
return record.clone()
null
@findAllByAttribute: (name, value) ->
@select (item) ->
item[name] is value
@each: (callback) ->
for key, value of @records
callback(value.clone())
@all: ->
@cloneArray(@recordsValues())
@first: ->
record = @recordsValues()[0]
record?.clone()
@last: ->
values = @recordsValues()
record = values[values.length - 1]
record?.clone()
@count: ->
@recordsValues().length
@deleteAll: ->
for key, value of @records
delete @records[key]
@destroyAll: ->
for key, value of @records
@records[key].destroy()
@update: (id, atts, options) ->
@find(id).updateAttributes(atts, options)
@create: (atts, options) ->
record = new @(atts)
record.save(options)
@destroy: (id, options) ->
@find(id).destroy(options)
@change: (callbackOrParams) ->
if typeof callbackOrParams is 'function'
@bind('change', callbackOrParams)
else
@trigger('change', callbackOrParams)
@fetch: (callbackOrParams) ->
if typeof callbackOrParams is 'function'
@bind('fetch', callbackOrParams)
else
@trigger('fetch', callbackOrParams)
@toJSON: ->
@recordsValues()
@fromJSON: (objects) ->
return unless objects
if typeof objects is 'string'
objects = JSON.parse(objects)
if isArray(objects)
(new @(value) for value in objects)
else
new @(objects)
@fromForm: ->
(new this).fromForm(arguments...)
# Private
@recordsValues: ->
result = []
for key, value of @records
result.push(value)
result
@cloneArray: (array) ->
(value.clone() for value in array)
@idCounter: 0
@uid: (prefix = '') ->
uid = prefix + @idCounter++
uid = @uid(prefix) if @exists(uid)
uid
# Instance
constructor: (atts) ->
super
@load atts if atts
@cid = @constructor.uid('c-')
isNew: ->
not @exists()
isValid: ->
not @validate()
validate: ->
load: (atts) ->
for key, value of atts
if typeof @[key] is 'function'
@[key](value)
else
@[key] = value
this
attributes: ->
result = {}
for key in @constructor.attributes when key of this
if typeof @[key] is 'function'
result[key] = @[key]()
else
result[key] = @[key]
result.id = @id if @id
result
eql: (rec) ->
!!(rec and rec.constructor is @constructor and
(rec.cid is @cid) or (rec.id and rec.id is @id))
save: (options = {}) ->
unless options.validate is false
error = @validate()
if error
@trigger('error', error)
return false
@trigger('beforeSave', options)
record = if @isNew() then @create(options) else @update(options)
@trigger('save', options)
record
updateAttribute: (name, value, options) ->
@[name] = value
@save(options)
updateAttributes: (atts, options) ->
@load(atts)
@save(options)
changeID: (id) ->
records = @constructor.records
records[id] = records[@id]
delete records[@id]
@id = id
@save()
destroy: (options = {}) ->
@trigger('beforeDestroy', options)
delete @constructor.records[@id]
delete @constructor.crecords[@cid]
@destroyed = true
@trigger('destroy', options)
@trigger('change', 'destroy', options)
@unbind()
this
dup: (newRecord) ->
result = new @constructor(@attributes())
if newRecord is false
result.cid = @cid
else
delete result.id
result
clone: ->
createObject(this)
reload: ->
return this if @isNew()
original = @constructor.find(@id)
@load(original.attributes())
original
toJSON: ->
@attributes()
toString: ->
"<#{@constructor.className} (#{JSON.stringify(this)})>"
fromForm: (form) ->
result = {}
for key in $(form).serializeArray()
result[key.name] = key.value
@load(result)
exists: ->
@id && @id of @constructor.records
# Private
update: (options) ->
@trigger('beforeUpdate', options)
records = @constructor.records
records[@id].load @attributes()
clone = records[@id].clone()
clone.trigger('update', options)
clone.trigger('change', 'update', options)
clone
create: (options) ->
@trigger('beforeCreate', options)
@id = @cid unless @id
record = @dup(false)
@constructor.records[@id] = record
@constructor.crecords[@cid] = record
clone = record.clone()
clone.trigger('create', options)
clone.trigger('change', 'create', options)
clone
bind: (events, callback) ->
@constructor.bind events, binder = (record) =>
if record && @eql(record)
callback.apply(this, arguments)
@constructor.bind 'unbind', unbinder = (record) =>
if record && @eql(record)
@constructor.unbind(events, binder)
@constructor.unbind('unbind', unbinder)
binder
one: (events, callback) ->
binder = @bind events, =>
@constructor.unbind(events, binder)
callback.apply(this, arguments)
trigger: (args...) ->
args.splice(1, 0, this)
@constructor.trigger(args...)
unbind: ->
@trigger('unbind')
class Controller extends Module
@include Events
@include Log
eventSplitter: /^(\S+)\s*(.*)$/
tag: 'div'
constructor: (options) ->
@options = options
for key, value of @options
@[key] = value
@el = document.createElement(@tag) unless @el
@el = $(@el)
@$el = @el
@el.addClass(@className) if @className
@el.attr(@attributes) if @attributes
@events = @constructor.events unless @events
@elements = @constructor.elements unless @elements
@delegateEvents(@events) if @events
@refreshElements() if @elements
super
release: =>
@trigger 'release'
@el.remove()
@unbind()
$: (selector) -> $(selector, @el)
delegateEvents: (events) ->
for key, method of events
if typeof(method) is 'function'
# Always return true from event handlers
method = do (method) => =>
method.apply(this, arguments)
true
else
unless @[method]
throw new Error("#{method} doesn't exist")
method = do (method) => =>
@[method].apply(this, arguments)
true
match = key.match(@eventSplitter)
eventName = match[1]
selector = match[2]
if selector is ''
@el.bind(eventName, method)
else
@el.delegate(selector, eventName, method)
refreshElements: ->
for key, value of @elements
@[value] = @$(key)
delay: (func, timeout) ->
setTimeout(@proxy(func), timeout || 0)
html: (element) ->
@el.html(element.el or element)
@refreshElements()
@el
append: (elements...) ->
elements = (e.el or e for e in elements)
@el.append(elements...)
@refreshElements()
@el
appendTo: (element) ->
@el.appendTo(element.el or element)
@refreshElements()
@el
prepend: (elements...) ->
elements = (e.el or e for e in elements)
@el.prepend(elements...)
@refreshElements()
@el
replace: (element) ->
[previous, @el] = [@el, $(element.el or element)]
previous.replaceWith(@el)
@delegateEvents(@events)
@refreshElements()
@el
# Utilities & Shims
$ = window?.jQuery or window?.Zepto or (element) -> element
createObject = Object.create or (o) ->
Func = ->
Func.prototype = o
new Func()
isArray = (value) ->
Object::toString.call(value) is '[object Array]'
isBlank = (value) ->
return true unless value
return false for key of value
true
makeArray = (args) ->
Array::slice.call(args, 0)
# Globals
Spine = @Spine = {}
module?.exports = Spine
Spine.version = '1.0.8'
Spine.isArray = isArray
Spine.isBlank = isBlank
Spine.$ = $
Spine.Events = Events
Spine.Log = Log
Spine.Module = Module
Spine.Controller = Controller
Spine.Model = Model
# Global events
Module.extend.call(Spine, Events)
# JavaScript compatability
Module.create = Module.sub =
Controller.create = Controller.sub =
Model.sub = (instances, statics) ->
class Result extends this
Result.include(instances) if instances
Result.extend(statics) if statics
Result.unbind?()
Result
Model.setup = (name, attributes = []) ->
class Instance extends this
Instance.configure(name, attributes...)
Instance
Spine.Class = Module