c652ce48 by Michael Richards

Utilize a KeypathObserver for subscribing to dependencies so that all intermedia…

…ry keys are observed and re-realized as they change.
1 parent 43448dbf
...@@ -95,20 +95,15 @@ class Rivets.Binding ...@@ -95,20 +95,15 @@ class Rivets.Binding
95 95
96 if @options.dependencies?.length 96 if @options.dependencies?.length
97 for dependency in @options.dependencies 97 for dependency in @options.dependencies
98 interfaces = (k for k, v of @view.adapters) 98 observer = new KeypathObserver @view, @model, dependency, (obs, prev) =>
99 prefix = dependency[0] in interfaces 99 key = obs.key
100 root = if prefix then dependency[0] else @view.config.rootInterface 100 @view.adapters[key.interface].unsubscribe prev, key.path, @sync
101 path = if prefix then dependency.substr(1) else dependency 101 @view.adapters[key.interface].subscribe obs.target, key.path, @sync
102 tokens = Rivets.KeypathParser.parse path, interfaces, root 102 @sync()
103 key = tokens.pop()
104
105 model = @model
106
107 for token in tokens
108 model = @view.adapters[token.interface].read model, token.path
109 103
110 @view.adapters[key.interface].subscribe model, key.path, @sync 104 key = observer.key
111 @dependencies.push [model, key] 105 @view.adapters[key.interface].subscribe observer.target, key.path, @sync
106 @dependencies.push observer
112 107
113 # Unsubscribes from the model and the element. 108 # Unsubscribes from the model and the element.
114 unbind: (silent = false) => 109 unbind: (silent = false) =>
...@@ -116,15 +111,15 @@ class Rivets.Binding ...@@ -116,15 +111,15 @@ class Rivets.Binding
116 @view.adapters[@key.interface].unsubscribe(@model, @key.path, @sync) if @key 111 @view.adapters[@key.interface].unsubscribe(@model, @key.path, @sync) if @key
117 112
118 if @dependencies.length 113 if @dependencies.length
119 for dep in @dependencies 114 for obs in @dependencies
120 @view.adapters[dep[1].interface].unsubscribe dep[0], dep[1].path, @sync 115 key = obs.key
116 @view.adapters[key.interface].unsubscribe obs.target, key.path, @sync
121 117
122 @dependencies = [] 118 @dependencies = []
123 119
124 # Updates the binding's model from what is currently set on the view. Unbinds 120 # Updates the binding's model from what is currently set on the view. Unbinds
125 # the old model first and then re-binds with the new model. 121 # the old model first and then re-binds with the new model.
126 update: (models = {}) => 122 update: (models = {}) =>
127 @observer.update() if models[@observer.root.path]
128 @binder.update?.call @, models 123 @binder.update?.call @, models
129 124
130 # Rivets.ComponentBinding 125 # Rivets.ComponentBinding
......