forked from cordjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelProxy.coffee
52 lines (37 loc) · 1.27 KB
/
ModelProxy.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
define [
'cord!Model'
'cord!Collection'
'cord!utils/Future'
], (Model, Collection, Future) ->
class ModelProxy
_modelLinks: []
_collectionLinks: []
_arrayLinks: []
@inject: ['container']
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: ->
promise = new Future
for link in @_modelLinks
promise.fork()
Model.unserializeLink link.object[link.field], @container, (model) ->
link.object[link.field] = model
promise.resolve()
for link in @_collectionLinks
promise.fork()
Collection.unserializeLink link.object[link.field], @container, (collection) ->
link.object[link.field] = collection
promise.resolve()
for link in @_arrayLinks
storedLinks = link.object[link.field]
link.object[link.field] = []
for item in storedLinks
promise.fork()
Model.unserializeLink item, @container, (model) ->
link.object[link.field].push(model)
promise.resolve()
promise