-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.coffee
200 lines (142 loc) · 5.19 KB
/
model.coffee
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
## Data model for client-side JS
# (c) 2012 Socialabs
Events = Backbone.Events
class EventedBase
_.extend @::, Events
## Properties
class Field
constructor: (@name, @options) ->
{@type, @validate} = @options
getProperty: ->
self = this
(value, options={}) ->
if arguments.length == 0
if self.name of @changes
return @changes[self.name]
else
return @data[self.name]
if self.validate and not self.validate(value)
@trigger 'error', "Invalid value for '#{self.name}': '#{value}'"
return false
@changes[self.name] = value
@trigger "change:#{self.name}", this, value
if not options.batch
data = {}
data[self.name] = value
@trigger 'change', this, data
this
class Property
constructor: (@name, {@get, @set}) ->
getProperty: ->
self = this
(value, options={}) ->
if arguments.length == 0
if not self.get
throw ("Error: attempt to read " +
"non-readable property: '#{self.name}'")
return self.get.call(this)
if not self.set
throw ("Error: attempt to change " +
"non-writable property: '#{self.name}'")
self.set.call(this, value)
@trigger "change:#{self.name}", this, value
if not options.batch
data = {}
data[self.name] = value
@trigger 'change', this, data
this
class Relation
constructor: (@type, @name, @model, @options) ->
getProperty: ->
self = this
(value, options={}) ->
if arguments.length == 0
true
## Model
class Model extends EventedBase
constructor: (data, options={}) ->
@data = _.extend({}, data)
@changes = {}
if options.backend
@backend = options.backend
if @initialize
@initialize data, options
# Model definition layer
@field: (name, options) ->
if not @fields
@fields = {}
@fields[name] = new Field(name, options)
@::[name] = @fields[name].getProperty()
@property: (name, options) ->
if not @fields
@fields = {}
@fields[name] = new Property(name, options)
@::[name] = @fields[name].getProperty()
@_relation: (type, name, model, options) ->
if typeof model == 'string'
model = eval(model)
@fields[name] = new Relation(type, name, model, options)
@::[name] = @fields[name].getProperty()
@oneToMany: (name, model, options) ->
@_relation('oneToMany', name, model, options)
@oneToOne: (name, model, options) ->
@_relation('oneToOne', name, model, options)
@manyToOne: (name, model, options) ->
@_relation('manyToOne', name, model, options)
# Instance methods
set: (data, options={}) ->
this unless data
for key, value of data
if key not of @constructor.fields
throw "Unknown field name: '#{key}'"
else if (@constructor.fields[key].constructor == Field and
_.isEqual(@data[key], value))
# remove changes if we have any
delete @changes[key]
else
@[key](value, batch: true)
@trigger 'change', this, data
this
# Pending changes take over original data
get: (name) ->
@[name]()
# Data which we had before changes
previous: (name) ->
field = @fields[name]
@data[field.fieldName or name]
save: (data, options={}) ->
@set(data) if data
# Utility
toObject: ->
_.extend({}, @data, @changes)
## Model set
class Set extends EventedBase
constructor: (@model, @models=[]) ->
# Underscore methods to implement on the Set
methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find',
'detect', 'filter', 'select', 'reject', 'every', 'all', 'some', 'any',
'include', 'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex',
'toArray', 'size', 'first', 'rest', 'last', 'without', 'indexOf',
'lastIndexOf', 'isEmpty', 'groupBy'];
for method in methods
@::[method] = ->
_[method].apply(_, [@models].concat(_.toArray(arguments)))
## Backends
class EventBackend extends EventedBase
create: (model) ->
@trigger 'create', model.constructor, model.toObject()
update: (model) ->
@trigger 'update', model.constructor, _.extend({}, model.changes)
delete: (model) ->
@trigger 'delete', model.constructor, model.id()
read: (model) ->
throw 'Read not implemented'
class LocalStorageBackend extends EventedBase
create: (model) ->
@trigger 'create', model.constructor, model.toObject()
update: (model) ->
@trigger 'update', model.constructor, _.extend({}, model.changes)
delete: (model) ->
@trigger 'delete', model.constructor, model.id()
read: (model) ->
throw 'Read not implemented'