e7c2fedb by Michael Richards

Update Rivets.Binding to use the new keypath observers. [#233]

1 parent 9db97f68
...@@ -10,7 +10,6 @@ class Rivets.Binding ...@@ -10,7 +10,6 @@ class Rivets.Binding
10 @formatters = @options.formatters || [] 10 @formatters = @options.formatters || []
11 @dependencies = [] 11 @dependencies = []
12 @setBinder() 12 @setBinder()
13 @setObserver()
14 13
15 # Sets the binder to use when binding and syncing. 14 # Sets the binder to use when binding and syncing.
16 setBinder: => 15 setBinder: =>
...@@ -29,14 +28,6 @@ class Rivets.Binding ...@@ -29,14 +28,6 @@ class Rivets.Binding
29 # Sets a keypath observer that will notify this binding when any intermediary 28 # Sets a keypath observer that will notify this binding when any intermediary
30 # keys are changed. 29 # keys are changed.
31 setObserver: => 30 setObserver: =>
32 @observer = new Rivets.KeypathObserver @view, @view.models, @keypath, (obs) =>
33 @unbind true if @key
34 @model = obs.target
35 @bind true if @key
36 @sync()
37
38 @key = @observer.key
39 @model = @observer.target
40 31
41 # 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
42 # formatted value. 33 # formatted value.
...@@ -62,7 +53,7 @@ class Rivets.Binding ...@@ -62,7 +53,7 @@ class Rivets.Binding
62 # with the suplied value formatted. 53 # with the suplied value formatted.
63 set: (value) => 54 set: (value) =>
64 value = if value instanceof Function and !@binder.function 55 value = if value instanceof Function and !@binder.function
65 @formattedValue value.call @model 56 @formattedValue value.call @observer.target
66 else 57 else
67 @formattedValue value 58 @formattedValue value
68 59
...@@ -70,10 +61,7 @@ class Rivets.Binding ...@@ -70,10 +61,7 @@ class Rivets.Binding
70 61
71 # Syncs up the view binding with the model. 62 # Syncs up the view binding with the model.
72 sync: => 63 sync: =>
73 @set if @key 64 @set @observer.value()
74 @view.adapters[@key.interface].read @model, @key.path
75 else
76 @model
77 65
78 # Publishes the value currently set on the input element back to the model. 66 # Publishes the value currently set on the input element back to the model.
79 publish: => 67 publish: =>
...@@ -86,41 +74,28 @@ class Rivets.Binding ...@@ -86,41 +74,28 @@ class Rivets.Binding
86 if @view.formatters[id]?.publish 74 if @view.formatters[id]?.publish
87 value = @view.formatters[id].publish value, args... 75 value = @view.formatters[id].publish value, args...
88 76
89 @view.adapters[@key.interface].publish @model, @key.path, value 77 @observer.setValue value
90 78
91 # Subscribes to the model for changes at the specified keypath. Bi-directional 79 # Subscribes to the model for changes at the specified keypath. Bi-directional
92 # routines will also listen for changes on the element to propagate them back 80 # routines will also listen for changes on the element to propagate them back
93 # to the model. 81 # to the model.
94 bind: (silent = false) => 82 bind: =>
95 @binder.bind?.call @, @el unless silent 83 @binder.bind?.call @, @el
96 @view.adapters[@key.interface].subscribe(@model, @key.path, @sync) if @key 84 @observer = new Rivets.Observer @view, @view.models, @keypath, @sync
97 @sync() if @view.config.preloadData unless silent
98 85
99 if @options.dependencies?.length 86 if @observer.target? and @options.dependencies?.length
100 for dependency in @options.dependencies 87 for dependency in @options.dependencies
101 observer = new Rivets.KeypathObserver @view, @model, dependency, (obs, prev) => 88 observer = new Rivets.Observer @view, @observer.target, dependency, @sync
102 key = obs.key
103 @view.adapters[key.interface].unsubscribe prev, key.path, @sync
104 @view.adapters[key.interface].subscribe obs.target, key.path, @sync
105 @sync()
106
107 key = observer.key
108 @view.adapters[key.interface].subscribe observer.target, key.path, @sync
109 @dependencies.push observer 89 @dependencies.push observer
110 90
91 @sync() if @view.config.preloadData
92
111 # Unsubscribes from the model and the element. 93 # Unsubscribes from the model and the element.
112 unbind: (silent = false) => 94 unbind: =>
113 unless silent
114 @binder.unbind?.call @, @el 95 @binder.unbind?.call @, @el
115 @observer.unobserve() 96 @observer.unobserve()
116 97
117 @view.adapters[@key.interface].unsubscribe(@model, @key.path, @sync) if @key 98 observer.unobserver() for observer in @dependencies
118
119 if @dependencies.length
120 for obs in @dependencies
121 key = obs.key
122 @view.adapters[key.interface].unsubscribe obs.target, key.path, @sync
123
124 @dependencies = [] 99 @dependencies = []
125 100
126 # Updates the binding's model from what is currently set on the view. Unbinds 101 # Updates the binding's model from what is currently set on the view. Unbinds
......