-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackbone-orm.coffee
224 lines (184 loc) · 6.14 KB
/
backbone-orm.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#root of Porkepic
Porkepic = {}
#Export it outside this scope
this.Porkepic = Porkepic
# ModelStore
# Trivial identity map for related models.
Porkepic.ModelStore = (model)->
this.store = {}
this.model = model
this
_.extend Porkepic.ModelStore.prototype, Backbone.Events,
create : (attributes, options) ->
id = attributes["id"]
if id?
object = this.store[id]
if not object?
this.store[id] = new this.model(attributes, options)
object = this.store[id]
this.trigger("add", object)
else
object = new this.model(attributes, options)
return object
records: ->
this.store
lookupOne: (id)->
this.store[id]
#the different model stores
Porkepic.ModelStores = ->
this
_.extend Porkepic.ModelStores.prototype, Backbone.Events,
storeForModel : (model) ->
store = this[model.type]
return store if store?
this[model.type] = new Porkepic.ModelStore(model)
Porkepic.Stores = new Porkepic.ModelStores()
#Types of associations
# EmbedsOne
# EmbedsMany
# HasOne
# HasMany
# EmbeddedInMany
#
# There are no BelongsTo because the last argument of EmbedsOne and HasOne (foreign_key)
# allows you to reciprocate the relation to the other model.
# EmbedsOne
# There is already a complete model inside the model
Porkepic.EmbedsOne = (model_object, foreign_model, key, foreign_key) ->
this.model_object = model_object
relation =
foreign_model : foreign_model
key : key
foreign_key : foreign_key
store : Porkepic.Stores.storeForModel(foreign_model)
this[key + "Relation"] = relation
this.createEmbedsOneRelation(relation)
this
# _.extend(this, new Porkepic.EmbedsOne(this, Project, "project"))
_.extend Porkepic.EmbedsOne.prototype, Backbone.Events,
createEmbedsOneRelation : (relation)->
object = this.model_object.get(relation.key)
if object?
foreign_object = relation.store.create(object)
# set it on the model
value = {}
value[relation.key] = foreign_object
this.model_object.set value
#check for embdeddedinmany reciprocity
fRel = foreign_object[relation.key + "RelationEIM"]
foreign_object.embedInMany(this.model_object, fRel) if fRel? and foreign_object.embedInMany?
#do the corresponding for one
if relation.foreign_key
value = {}
value[relation.foreign_key] = this.model_object
foreign_object.set value
Porkepic.EmbedsMany = (model_object, foreign_model, key, foreign_key) ->
this.model_object = model_object
relation =
foreign_model : foreign_model
key : key
foreign_key : foreign_key
store : Porkepic.Stores.storeForModel(foreign_model)
this[key + "Relation"] = relation
this.createEmbedsManyRelation(relation)
this
_.extend Porkepic.EmbedsMany.prototype, Backbone.Events,
createEmbedsManyRelation : (relation)->
objects = this.model_object.get(relation.key)
if objects?
foreign_objects = new Backbone.Collection
#create all objects
model = this.model_object
_.each objects, (object)->
foreign_object = relation.store.create(object)
foreign_objects.add foreign_object
#check for embdeddedinMany reciprocity
fRel = foreign_object[relation.key + "RelationEIM"]
foreign_object.embedInMany(model, fRel) if fRel? and fRel.key == relation.foreign_key and foreign_object.embedInMany?
#do the corresponding for one
if relation.foreign_key
value = {}
value[relation.foreign_key] = model
foreign_object.set value
# set it on the model
value = {}
value[relation.key] = foreign_objects
this.model_object.set value
# EmbeddedInMany
# The model is embedded more than once in different objects
# _.extend(this, new Porkepic.EmbeddedInMany(this, WorkSheet, "worksheets"))
Porkepic.EmbeddedInMany = (model_object, foreign_model, key, foreign_key) ->
relation =
key : key
foreign_model : foreign_model
foreign_key: foreign_key
this[foreign_key + "RelationEIM"] = relation
this.model_object = model_object
this
_.extend Porkepic.EmbeddedInMany.prototype, Backbone.Events,
embedInMany : (object, params)->
foreign_model = params.foreign_model
objects = this.model_object.get params.key
if not objects?
objects = new Backbone.Collection
objects.add object
value = {}
value[params.key] = objects
this.model_object.set value
# HasOne
# The model is related to one other object
# _.extend(this, new Porkepic.HasOne(this, Foreman, "foreman"))
Porkepic.HasOne = (model_object, foreign_model, key, foreign_key) ->
this.model_object = model_object
relation =
foreign_model : foreign_model
key : key
foreign_key : foreign_key
store : Porkepic.Stores.storeForModel(foreign_model)
object_id = model_object.get(key + "_id")
return this if not object_id?
object = relation.store.lookupOne( object_id )
if not object?
#bind to the add event
hasOne = this
relation.store.bind "add", (object)->
if model_object.get(relation.key + "_id") is object.id
hasOne.createHasOneRelation(relation, object)
else
#create the relation now
this.createHasOneRelation(relation, object)
this[key + "Relation"] = relation
this
_.extend Porkepic.HasOne.prototype, Backbone.Events,
createHasOneRelation: (relation, object) ->
value = {}
value[relation.key] = object
this.model_object.set value
#check for HasMany reciprocity
fRel = object[relation.key + "RelationHM"]
object.addInMany(this.model_object, fRel) if fRel? and fRel.key == relation.foreign_key and object.addInMany?
if relation.foreign_key?
value = {}
value[relation.foreign_key] = this.model_object
object.set value
# HasMany
# The model is related to many other objects
# _.extend(this, new Porkepic.HasMany(this, Project, "projects", "foreman"))
Porkepic.HasMany = (model_object, foreign_model, key, foreign_key) ->
relation =
key : key
foreign_model : foreign_model
foreign_key: foreign_key
this[foreign_key + "RelationHM"] = relation
this.model_object = model_object
this
_.extend Porkepic.HasMany.prototype, Backbone.Events,
addInMany: (object, params)->
foreign_model = params.foreign_model
objects = this.model_object.get params.key
if not objects?
objects = new Backbone.Collection
objects.add object
value = {}
value[params.key] = objects
this.model_object.set value