25c5bf2f by Michael Richards

Implement smarter nested dependency paths, originating from the target object.

1 parent 87f02d3e
...@@ -9,6 +9,7 @@ class Rivets.Binding ...@@ -9,6 +9,7 @@ class Rivets.Binding
9 constructor: (@view, @el, @type, @keypath, @options = {}) -> 9 constructor: (@view, @el, @type, @keypath, @options = {}) ->
10 @formatters = @options.formatters || [] 10 @formatters = @options.formatters || []
11 @objectPath = [] 11 @objectPath = []
12 @dependencies = []
12 @setBinder() 13 @setBinder()
13 @setModel() 14 @setModel()
14 15
...@@ -116,32 +117,31 @@ class Rivets.Binding ...@@ -116,32 +117,31 @@ class Rivets.Binding
116 117
117 if @options.dependencies?.length 118 if @options.dependencies?.length
118 for dependency in @options.dependencies 119 for dependency in @options.dependencies
119 if /^\./.test dependency 120 interfaces = (k for k, v of @view.adapters)
121 prefix = dependency[0] in interfaces
122 root = if prefix then dependency[0] else '.'
123 path = if prefix then dependency.substr(1) else dependency
124 tokens = Rivets.KeypathParser.parse path, interfaces, root
125 key = tokens.pop()
126
120 model = @model 127 model = @model
121 keypath = dependency.substr 1
122 else
123 dependency = dependency.split '.'
124 model = @view.models[dependency.shift()]
125 keypath = dependency.join '.'
126 128
127 @view.adapters['.'].subscribe model, keypath, @sync 129 for token in tokens
130 model = @view.adapters[token.interface].read model, token.path
131
132 @view.adapters[key.interface].subscribe model, key.path, @sync
133 @dependencies.push [model, key]
128 134
129 # Unsubscribes from the model and the element. 135 # Unsubscribes from the model and the element.
130 unbind: => 136 unbind: =>
131 @binder.unbind?.call @, @el 137 @binder.unbind?.call @, @el
132 @view.adapters[@key.interface].unsubscribe @model, @key.path, @sync 138 @view.adapters[@key.interface].unsubscribe @model, @key.path, @sync
133 139
134 if @options.dependencies?.length 140 if @dependencies.length
135 for dependency in @options.dependencies 141 for dep in @dependencies
136 if /^\./.test dependency 142 @view.adapters[dep[1].interface].unsubscribe dep[0], dep[1].path, @sync
137 model = @model
138 keypath = dependency.substr 1
139 else
140 dependency = dependency.split '.'
141 model = @view.models[dependency.shift()]
142 keypath = dependency.join '.'
143 143
144 @view.adapters['.'].unsubscribe model, keypath, @sync 144 @dependencies = []
145 145
146 # Updates the binding's model from what is currently set on the view. Unbinds 146 # Updates the binding's model from what is currently set on the view. Unbinds
147 # the old model first and then re-binds with the new model. 147 # the old model first and then re-binds with the new model.
......