ce5d8b75 by Michael Richards

Merge pull request #62 from mikeric/object-specified-dependencies

Object specificity in dependent attribute declarations
2 parents 1e6b3edf c75e53fb
...@@ -153,9 +153,11 @@ Just use `model:property` instead of `model.property` inside your binding declar ...@@ -153,9 +153,11 @@ Just use `model:property` instead of `model.property` inside your binding declar
153 Computed properties are functions that get re-evaluated when one or more dependent properties change. Declaring computed properties in Rivets.js is simple, just separate the function from it's dependencies with a *<*. The following `data-text` binding will get re-evaluated with `event.duration()` when either the event's `start` or `end` attribute changes. 153 Computed properties are functions that get re-evaluated when one or more dependent properties change. Declaring computed properties in Rivets.js is simple, just separate the function from it's dependencies with a *<*. The following `data-text` binding will get re-evaluated with `event.duration()` when either the event's `start` or `end` attribute changes.
154 154
155 ```html 155 ```html
156 <span data-text="event:duration < start end"></span> 156 <span data-text="event:duration < .start .end"></span>
157 ``` 157 ```
158 158
159 The prepended `.` is a shorthand syntax for specifying dependencies that are the same object as the target object, so the above declaration is effectively the same as `event:duration < event.start event.end`.
160
159 #### Iteration Binding 161 #### Iteration Binding
160 162
161 Use the `data-each-[item]` binding to have Rivets.js automatically loop over items in an array and append bound instances of that element. Within that element you can bind to the iterated item as well as any contexts that are available in the parent view. 163 Use the `data-each-[item]` binding to have Rivets.js automatically loop over items in an array and append bound instances of that element. Within that element you can bind to the iterated item as well as any contexts that are available in the parent view.
......
...@@ -58,7 +58,7 @@ describe('Rivets.Binding', function() { ...@@ -58,7 +58,7 @@ describe('Rivets.Binding', function() {
58 58
59 describe('with dependencies', function() { 59 describe('with dependencies', function() {
60 beforeEach(function() { 60 beforeEach(function() {
61 binding.options.dependencies = ['fname', 'lname']; 61 binding.options.dependencies = ['.fname', '.lname'];
62 }); 62 });
63 63
64 it('sets up observers on the dependant attributes', function() { 64 it('sets up observers on the dependant attributes', function() {
......
...@@ -80,8 +80,16 @@ class Rivets.Binding ...@@ -80,8 +80,16 @@ class Rivets.Binding
80 @sync() if Rivets.config.preloadData 80 @sync() if Rivets.config.preloadData
81 81
82 if @options.dependencies?.length 82 if @options.dependencies?.length
83 for keypath in @options.dependencies 83 for dependency in @options.dependencies
84 Rivets.config.adapter.subscribe @model, keypath, @sync 84 if /^\./.test dependency
85 model = @model
86 keypath = dependency.substr 1
87 else
88 dependency = dependency.split '.'
89 model = @view.models[dependency.shift()]
90 keypath = dependency.join '.'
91
92 Rivets.config.adapter.subscribe model, keypath, @sync
85 93
86 if @isBidirectional() 94 if @isBidirectional()
87 bindEvent @el, 'change', @publish 95 bindEvent @el, 'change', @publish
...@@ -197,7 +205,7 @@ class Rivets.View ...@@ -197,7 +205,7 @@ class Rivets.View
197 publish: => 205 publish: =>
198 binding.publish() for binding in @select (b) -> b.isBidirectional() 206 binding.publish() for binding in @select (b) -> b.isBidirectional()
199 207
200 # Cross-browser event binding 208 # Cross-browser event binding.
201 bindEvent = (el, event, fn) -> 209 bindEvent = (el, event, fn) ->
202 # Check to see if jQuery is loaded. 210 # Check to see if jQuery is loaded.
203 if window.jQuery? 211 if window.jQuery?
...@@ -211,6 +219,7 @@ bindEvent = (el, event, fn) -> ...@@ -211,6 +219,7 @@ bindEvent = (el, event, fn) ->
211 event = "on" + event 219 event = "on" + event
212 el.attachEvent event, fn 220 el.attachEvent event, fn
213 221
222 # Cross-browser event unbinding.
214 unbindEvent = (el, event, fn) -> 223 unbindEvent = (el, event, fn) ->
215 # Check to see if jQuery is loaded. 224 # Check to see if jQuery is loaded.
216 if window.jQuery? 225 if window.jQuery?
......