f2b6632e by Michael Richards

Make sure that we still have a reference to the model (observer.target now) even…

… though it may be undefined if keypath is unresolvable.
1 parent 27bc80be
...@@ -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 @dependencies = [] 11 @dependencies = []
12 @model = undefined
12 @setBinder() 13 @setBinder()
13 14
14 # Sets the binder to use when binding and syncing. 15 # Sets the binder to use when binding and syncing.
...@@ -49,7 +50,7 @@ class Rivets.Binding ...@@ -49,7 +50,7 @@ class Rivets.Binding
49 # with the suplied value formatted. 50 # with the suplied value formatted.
50 set: (value) => 51 set: (value) =>
51 value = if value instanceof Function and !@binder.function 52 value = if value instanceof Function and !@binder.function
52 @formattedValue value.call @observer.target 53 @formattedValue value.call @model
53 else 54 else
54 @formattedValue value 55 @formattedValue value
55 56
...@@ -57,6 +58,7 @@ class Rivets.Binding ...@@ -57,6 +58,7 @@ class Rivets.Binding
57 58
58 # Syncs up the view binding with the model. 59 # Syncs up the view binding with the model.
59 sync: => 60 sync: =>
61 @model = @observer.target
60 @set @observer.value() 62 @set @observer.value()
61 63
62 # Publishes the value currently set on the input element back to the model. 64 # Publishes the value currently set on the input element back to the model.
...@@ -78,10 +80,11 @@ class Rivets.Binding ...@@ -78,10 +80,11 @@ class Rivets.Binding
78 bind: => 80 bind: =>
79 @binder.bind?.call @, @el 81 @binder.bind?.call @, @el
80 @observer = new Rivets.Observer @view, @view.models, @keypath, @sync 82 @observer = new Rivets.Observer @view, @view.models, @keypath, @sync
83 @model = @observer.target
81 84
82 if @observer.target? and @options.dependencies?.length 85 if @model? and @options.dependencies?.length
83 for dependency in @options.dependencies 86 for dependency in @options.dependencies
84 observer = new Rivets.Observer @view, @observer.target, dependency, @sync 87 observer = new Rivets.Observer @view, @model, dependency, @sync
85 @dependencies.push observer 88 @dependencies.push observer
86 89
87 @sync() if @view.config.preloadData 90 @sync() if @view.config.preloadData
......