21933b07 by Michael Richards

Only perform reads and observing on @key if it exists, otherwise assume the @mod…

…el is the root value to be used.
1 parent 25c5bf2f
...@@ -92,7 +92,10 @@ class Rivets.Binding ...@@ -92,7 +92,10 @@ class Rivets.Binding
92 92
93 # Syncs up the view binding with the model. 93 # Syncs up the view binding with the model.
94 sync: => 94 sync: =>
95 @set @view.adapters[@key.interface].read @model, @key.path 95 @set if @key
96 @view.adapters[@key.interface].read @model, @key.path
97 else
98 @model
96 99
97 # Publishes the value currently set on the input element back to the model. 100 # Publishes the value currently set on the input element back to the model.
98 publish: => 101 publish: =>
...@@ -112,7 +115,7 @@ class Rivets.Binding ...@@ -112,7 +115,7 @@ class Rivets.Binding
112 # to the model. 115 # to the model.
113 bind: => 116 bind: =>
114 @binder.bind?.call @, @el 117 @binder.bind?.call @, @el
115 @view.adapters[@key.interface].subscribe @model, @key.path, @sync 118 @view.adapters[@key.interface].subscribe(@model, @key.path, @sync) if @key
116 @sync() if @view.config.preloadData 119 @sync() if @view.config.preloadData
117 120
118 if @options.dependencies?.length 121 if @options.dependencies?.length
...@@ -135,7 +138,7 @@ class Rivets.Binding ...@@ -135,7 +138,7 @@ class Rivets.Binding
135 # Unsubscribes from the model and the element. 138 # Unsubscribes from the model and the element.
136 unbind: => 139 unbind: =>
137 @binder.unbind?.call @, @el 140 @binder.unbind?.call @, @el
138 @view.adapters[@key.interface].unsubscribe @model, @key.path, @sync 141 @view.adapters[@key.interface].unsubscribe(@model, @key.path, @sync) if @key
139 142
140 if @dependencies.length 143 if @dependencies.length
141 for dep in @dependencies 144 for dep in @dependencies
...@@ -147,9 +150,9 @@ class Rivets.Binding ...@@ -147,9 +150,9 @@ class Rivets.Binding
147 # the old model first and then re-binds with the new model. 150 # the old model first and then re-binds with the new model.
148 update: (models = {}) => 151 update: (models = {}) =>
149 if models[@rootKey.path] 152 if models[@rootKey.path]
150 @view.adapters[@key.interface].unsubscribe @model, @key.path, @sync 153 @view.adapters[@key.interface].unsubscribe(@model, @key.path, @sync) if @key
151 @setModel() 154 @setModel()
152 @view.adapters[@key.interface].subscribe @model, @key.path, @sync 155 @view.adapters[@key.interface].subscribe(@model, @key.path, @sync) if @key
153 @sync() 156 @sync()
154 157
155 @binder.update?.call @, models 158 @binder.update?.call @, models
...@@ -214,8 +217,6 @@ class Rivets.ComponentBinding extends Rivets.Binding ...@@ -214,8 +217,6 @@ class Rivets.ComponentBinding extends Rivets.Binding
214 class Rivets.TextBinding extends Rivets.Binding 217 class Rivets.TextBinding extends Rivets.Binding
215 # Initializes a text binding for the specified view and text node. 218 # Initializes a text binding for the specified view and text node.
216 constructor: (@view, @el, @type, @keypath, @options = {}) -> 219 constructor: (@view, @el, @type, @keypath, @options = {}) ->
217 interfaces = (k for k, v of @view.adapters)
218 tokens = Rivets.KeypathParser.parse(@keypath, interfaces, '.')
219 @formatters = @options.formatters || [] 220 @formatters = @options.formatters || []
220 @setModel() 221 @setModel()
221 222
......