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
# All information about the binding is passed into the constructor; the
# containing view, the DOM node, the type of binding, the model object and the
# keypath at which to listen for changes.
constructor: (@view, @el, @type, @model, @keypath, @options = {}) ->
constructor: (@view, @el, @type, @key, @keypath, @options = {}) ->
unless @binder = @view.binders[type]
for identifier, value of @view.binders
if identifier isnt '*' and identifier.indexOf('*') isnt -1
......@@ -25,11 +25,9 @@ class Rivets.Binding
@args.shift()
@binder or= @view.binders['*']
if @binder instanceof Function
@binder = {routine: @binder}
@binder = {routine: @binder} if @binder instanceof Function
@formatters = @options.formatters || []
@model = @view.models[@key]
# Applies all the current formatters to the supplied value and returns the
# formatted value.
......@@ -125,6 +123,13 @@ class Rivets.Binding
@view.config.adapter.unsubscribe model, keypath, @sync
# Updates the binding's model from what is currently set on the view. Unbinds
# the old model first and then re-binds with the new model.
update: =>
@unbind()
@model = @view.models[@key]
@bind()
# A collection of bindings built from a set of parent elements.
class Rivets.View
# The DOM elements and the model objects for binding are passed into the
......@@ -182,17 +187,17 @@ class Rivets.View
options.formatters = pipes
options.bypass = path.indexOf(':') != -1
if splitPath[0]
model = @models[splitPath.shift()]
key = splitPath.shift()
else
model = @models
key = null
splitPath.shift()
keypath = splitPath.join '.'
if model
if @models[key]?
if dependencies = context.shift()
options.dependencies = dependencies.split /\s+/
@bindings.push new Rivets.Binding @, node, type, model, keypath, options
@bindings.push new Rivets.Binding @, node, type, key, keypath, options
attributes = null if attributes
......@@ -224,6 +229,12 @@ class Rivets.View
publish: =>
binding.publish() for binding in @select (b) -> b.binder.publishes
# Updates the view's models along with any affected bindings.
update: (models = {}) =>
for key, model of models
@models[key] = model
binding.update() for binding in @select (b) -> b.key is key
# Cross-browser event binding.
bindEvent = (el, event, handler, context) ->
fn = (e) -> handler.call context, e
......