keypath_observer.coffee
1.11 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
class KeypathObserver
constructor: (@view, @model, @keypath, @callback) ->
@parse()
@objectPath = []
@target = @realize()
parse: =>
interfaces = (k for k, v of @view.adapters)
if @keypath[0] in interfaces
root = @keypath[0]
path = @keypath.substr 1
else
root = @view.config.rootInterface
path = @keypath
@tokens = Rivets.KeypathParser.parse path, interfaces, root
@key = @tokens.pop()
update: =>
unless (next = @realize()) is @target
prev = @target
@target = next
@callback @, prev
realize: =>
current = @model
for token, index in @tokens
if @objectPath[index]?
if current isnt prev = @objectPath[index]
@view.adapters[token.interface].unsubscribe prev, token.path, @update
@view.adapters[token.interface].subscribe current, token.path, @update
@objectPath[index] = current
else
@view.adapters[token.interface].subscribe current, token.path, @update
@objectPath[index] = current
current = @view.adapters[token.interface].read current, token.path
current