28fbf16b by Michael Richards

Set the KeypathObserver on the Rivets object. Add missing source documentation.

1 parent cd102889
......@@ -12,6 +12,7 @@ class Rivets.Binding
@setBinder()
@setObserver()
# Sets the binder to use when binding and syncing.
setBinder: =>
unless @binder = @view.binders[@type]
for identifier, value of @view.binders
......@@ -25,8 +26,10 @@ class Rivets.Binding
@binder or= @view.binders['*']
@binder = {routine: @binder} if @binder instanceof Function
# Sets a keypath observer that will notify this binding when any intermediary
# keys are changed.
setObserver: =>
@observer = new KeypathObserver @view, @view.models, @keypath, (obs) =>
@observer = new Rivets.KeypathObserver @view, @view.models, @keypath, (obs) =>
@unbind true if @key
@model = obs.target
@bind true if @key
......@@ -95,7 +98,7 @@ class Rivets.Binding
if @options.dependencies?.length
for dependency in @options.dependencies
observer = new KeypathObserver @view, @model, dependency, (obs, prev) =>
observer = new Rivets.KeypathObserver @view, @model, dependency, (obs, prev) =>
key = obs.key
@view.adapters[key.interface].unsubscribe prev, key.path, @sync
@view.adapters[key.interface].subscribe obs.target, key.path, @sync
......
class KeypathObserver
# Rivets.KeypathObserver
# ----------------------
# Parses and observes a full keypath with the appropriate adapters. Also
# intelligently re-realizes the keypath when intermediary keys change.
class Rivets.KeypathObserver
# Performs the initial parse, variable instantiation and keypath realization.
constructor: (@view, @model, @keypath, @callback) ->
@parse()
@objectPath = []
@target = @realize()
# Parses the keypath using the interfaces defined on the view. Sets variables
# for the tokenized keypath, as well as the end key.
parse: =>
interfaces = (k for k, v of @view.adapters)
......@@ -17,12 +25,15 @@ class KeypathObserver
@tokens = Rivets.KeypathParser.parse path, interfaces, root
@key = @tokens.pop()
# Updates the keypath. This is called when any intermediate key is changed.
update: =>
unless (next = @realize()) is @target
prev = @target
@target = next
@callback @, prev
# Realizes the full keypath, attaching observers for every key and correcting
# old observers to any changed objects in the keypath.
realize: =>
current = @model
......@@ -40,6 +51,7 @@ class KeypathObserver
current
# Unobserves any current observers set up on the keys.
unobserve: =>
for token, index in @tokens
if obj = @objectPath[index]
......