8db554cd by Michael Richards

Merge pull request #154 from mikeric/view-context-updating

View context updating
2 parents 86b99ab0 01c164b5
...@@ -14,7 +14,7 @@ class Rivets.Binding ...@@ -14,7 +14,7 @@ class Rivets.Binding
14 # All information about the binding is passed into the constructor; the 14 # All information about the binding is passed into the constructor; the
15 # containing view, the DOM node, the type of binding, the model object and the 15 # containing view, the DOM node, the type of binding, the model object and the
16 # keypath at which to listen for changes. 16 # keypath at which to listen for changes.
17 constructor: (@view, @el, @type, @model, @keypath, @options = {}) -> 17 constructor: (@view, @el, @type, @key, @keypath, @options = {}) ->
18 unless @binder = @view.binders[type] 18 unless @binder = @view.binders[type]
19 for identifier, value of @view.binders 19 for identifier, value of @view.binders
20 if identifier isnt '*' and identifier.indexOf('*') isnt -1 20 if identifier isnt '*' and identifier.indexOf('*') isnt -1
...@@ -25,11 +25,9 @@ class Rivets.Binding ...@@ -25,11 +25,9 @@ class Rivets.Binding
25 @args.shift() 25 @args.shift()
26 26
27 @binder or= @view.binders['*'] 27 @binder or= @view.binders['*']
28 28 @binder = {routine: @binder} if @binder instanceof Function
29 if @binder instanceof Function
30 @binder = {routine: @binder}
31
32 @formatters = @options.formatters || [] 29 @formatters = @options.formatters || []
30 @model = @view.models[@key]
33 31
34 # Applies all the current formatters to the supplied value and returns the 32 # Applies all the current formatters to the supplied value and returns the
35 # formatted value. 33 # formatted value.
...@@ -125,6 +123,13 @@ class Rivets.Binding ...@@ -125,6 +123,13 @@ class Rivets.Binding
125 123
126 @view.config.adapter.unsubscribe model, keypath, @sync 124 @view.config.adapter.unsubscribe model, keypath, @sync
127 125
126 # Updates the binding's model from what is currently set on the view. Unbinds
127 # the old model first and then re-binds with the new model.
128 update: =>
129 @unbind()
130 @model = @view.models[@key]
131 @bind()
132
128 # A collection of bindings built from a set of parent elements. 133 # A collection of bindings built from a set of parent elements.
129 class Rivets.View 134 class Rivets.View
130 # The DOM elements and the model objects for binding are passed into the 135 # The DOM elements and the model objects for binding are passed into the
...@@ -182,17 +187,17 @@ class Rivets.View ...@@ -182,17 +187,17 @@ class Rivets.View
182 options.formatters = pipes 187 options.formatters = pipes
183 options.bypass = path.indexOf(':') != -1 188 options.bypass = path.indexOf(':') != -1
184 if splitPath[0] 189 if splitPath[0]
185 model = @models[splitPath.shift()] 190 key = splitPath.shift()
186 else 191 else
187 model = @models 192 key = null
188 splitPath.shift() 193 splitPath.shift()
189 keypath = splitPath.join '.' 194 keypath = splitPath.join '.'
190 195
191 if model 196 if @models[key]?
192 if dependencies = context.shift() 197 if dependencies = context.shift()
193 options.dependencies = dependencies.split /\s+/ 198 options.dependencies = dependencies.split /\s+/
194 199
195 @bindings.push new Rivets.Binding @, node, type, model, keypath, options 200 @bindings.push new Rivets.Binding @, node, type, key, keypath, options
196 201
197 attributes = null if attributes 202 attributes = null if attributes
198 203
...@@ -224,6 +229,12 @@ class Rivets.View ...@@ -224,6 +229,12 @@ class Rivets.View
224 publish: => 229 publish: =>
225 binding.publish() for binding in @select (b) -> b.binder.publishes 230 binding.publish() for binding in @select (b) -> b.binder.publishes
226 231
232 # Updates the view's models along with any affected bindings.
233 update: (models = {}) =>
234 for key, model of models
235 @models[key] = model
236 binding.update() for binding in @select (b) -> b.key is key
237
227 # Cross-browser event binding. 238 # Cross-browser event binding.
228 bindEvent = (el, event, handler, context) -> 239 bindEvent = (el, event, handler, context) ->
229 fn = (e) -> handler.call context, e 240 fn = (e) -> handler.call context, e
......