28fbf16b by Michael Richards

Set the KeypathObserver on the Rivets object. Add missing source documentation.

1 parent cd102889
...@@ -12,6 +12,7 @@ class Rivets.Binding ...@@ -12,6 +12,7 @@ class Rivets.Binding
12 @setBinder() 12 @setBinder()
13 @setObserver() 13 @setObserver()
14 14
15 # Sets the binder to use when binding and syncing.
15 setBinder: => 16 setBinder: =>
16 unless @binder = @view.binders[@type] 17 unless @binder = @view.binders[@type]
17 for identifier, value of @view.binders 18 for identifier, value of @view.binders
...@@ -25,8 +26,10 @@ class Rivets.Binding ...@@ -25,8 +26,10 @@ class Rivets.Binding
25 @binder or= @view.binders['*'] 26 @binder or= @view.binders['*']
26 @binder = {routine: @binder} if @binder instanceof Function 27 @binder = {routine: @binder} if @binder instanceof Function
27 28
29 # Sets a keypath observer that will notify this binding when any intermediary
30 # keys are changed.
28 setObserver: => 31 setObserver: =>
29 @observer = new KeypathObserver @view, @view.models, @keypath, (obs) => 32 @observer = new Rivets.KeypathObserver @view, @view.models, @keypath, (obs) =>
30 @unbind true if @key 33 @unbind true if @key
31 @model = obs.target 34 @model = obs.target
32 @bind true if @key 35 @bind true if @key
...@@ -95,7 +98,7 @@ class Rivets.Binding ...@@ -95,7 +98,7 @@ class Rivets.Binding
95 98
96 if @options.dependencies?.length 99 if @options.dependencies?.length
97 for dependency in @options.dependencies 100 for dependency in @options.dependencies
98 observer = new KeypathObserver @view, @model, dependency, (obs, prev) => 101 observer = new Rivets.KeypathObserver @view, @model, dependency, (obs, prev) =>
99 key = obs.key 102 key = obs.key
100 @view.adapters[key.interface].unsubscribe prev, key.path, @sync 103 @view.adapters[key.interface].unsubscribe prev, key.path, @sync
101 @view.adapters[key.interface].subscribe obs.target, key.path, @sync 104 @view.adapters[key.interface].subscribe obs.target, key.path, @sync
......
1 class KeypathObserver 1 # Rivets.KeypathObserver
2 # ----------------------
3
4 # Parses and observes a full keypath with the appropriate adapters. Also
5 # intelligently re-realizes the keypath when intermediary keys change.
6 class Rivets.KeypathObserver
7 # Performs the initial parse, variable instantiation and keypath realization.
2 constructor: (@view, @model, @keypath, @callback) -> 8 constructor: (@view, @model, @keypath, @callback) ->
3 @parse() 9 @parse()
4 @objectPath = [] 10 @objectPath = []
5 @target = @realize() 11 @target = @realize()
6 12
13 # Parses the keypath using the interfaces defined on the view. Sets variables
14 # for the tokenized keypath, as well as the end key.
7 parse: => 15 parse: =>
8 interfaces = (k for k, v of @view.adapters) 16 interfaces = (k for k, v of @view.adapters)
9 17
...@@ -17,12 +25,15 @@ class KeypathObserver ...@@ -17,12 +25,15 @@ class KeypathObserver
17 @tokens = Rivets.KeypathParser.parse path, interfaces, root 25 @tokens = Rivets.KeypathParser.parse path, interfaces, root
18 @key = @tokens.pop() 26 @key = @tokens.pop()
19 27
28 # Updates the keypath. This is called when any intermediate key is changed.
20 update: => 29 update: =>
21 unless (next = @realize()) is @target 30 unless (next = @realize()) is @target
22 prev = @target 31 prev = @target
23 @target = next 32 @target = next
24 @callback @, prev 33 @callback @, prev
25 34
35 # Realizes the full keypath, attaching observers for every key and correcting
36 # old observers to any changed objects in the keypath.
26 realize: => 37 realize: =>
27 current = @model 38 current = @model
28 39
...@@ -40,6 +51,7 @@ class KeypathObserver ...@@ -40,6 +51,7 @@ class KeypathObserver
40 51
41 current 52 current
42 53
54 # Unobserves any current observers set up on the keys.
43 unobserve: => 55 unobserve: =>
44 for token, index in @tokens 56 for token, index in @tokens
45 if obj = @objectPath[index] 57 if obj = @objectPath[index]
......