-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModelProxy.coffee
More file actions
55 lines (40 loc) · 1.35 KB
/
ModelProxy.coffee
File metadata and controls
55 lines (40 loc) · 1.35 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
define [
'cord!Model'
'cord!Collection'
'cord!utils/Future'
], (Model, Collection, Future) ->
class ModelProxy
_modelLinks: []
_collectionLinks: []
_arrayLinks: []
@inject: ['serviceContainer']
addModelLink: (object, field) ->
@_modelLinks.push object: object, field: field
addCollectionLink: (object, field) ->
@_collectionLinks.push object: object, field: field
addArrayLink: (object, field) ->
@_arrayLinks.push object: object, field: field
restoreLinks: ->
promises = []
for link in @_modelLinks
promises.push(
Model.unserializeLink(link.object[link.field], @serviceContainer).then (model) ->
link.object[link.field] = model
return
)
for link in @_collectionLinks
promises.push(
Collection.unserializeLink(link.object[link.field], @serviceContainer).then (collection) ->
link.object[link.field] = collection
return
)
for link in @_arrayLinks
storedLinks = link.object[link.field]
link.object[link.field] = []
for item in storedLinks
promises.push(
Model.unserializeLink(item, @serviceContainer).then (model) ->
link.object[link.field].push(model)
return
)
Future.all(promises).then -> undefined