Update KeypathObserver to have better state management, passing in the previous …
…target to the callback for unbinding.
Showing
1 changed file
with
17 additions
and
5 deletions
1 | class KeypathObserver | 1 | class KeypathObserver |
2 | constructor: (@view, @model, @keypath, @callback) -> | 2 | constructor: (@view, @model, @keypath, @callback) -> |
3 | @interfaces = (k for k, v of @view.adapters) | 3 | @parse() |
4 | @objectPath = [] | 4 | @objectPath = [] |
5 | @tokens = Rivets.KeypathParser.parse @keypath, @interfaces, @view.config.rootInterface | ||
6 | @root = @tokens.shift() | ||
7 | @key = @tokens.pop() | ||
8 | @target = @realize() | 5 | @target = @realize() |
9 | 6 | ||
7 | parse: => | ||
8 | interfaces = (k for k, v of @view.adapters) | ||
9 | |||
10 | if @keypath[0] in interfaces | ||
11 | root = @keypath[0] | ||
12 | path = @keypath.substr 1 | ||
13 | else | ||
14 | root = @view.config.rootInterface | ||
15 | path = @keypath | ||
16 | |||
17 | @tokens = Rivets.KeypathParser.parse path, interfaces, root | ||
18 | @key = @tokens.pop() | ||
19 | |||
10 | update: => | 20 | update: => |
11 | unless (next = @realize()) is @target | 21 | unless (next = @realize()) is @target |
12 | @callback @target = next | 22 | prev = @target |
23 | @target = next | ||
24 | @callback @, prev | ||
13 | 25 | ||
14 | realize: => | 26 | realize: => |
15 | current = @model | 27 | current = @model | ... | ... |
-
Please register or sign in to post a comment