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
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.
```html
<span data-text="event:duration < start end"></span>
<span data-text="event:duration < .start .end"></span>
```
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`.
#### Iteration Binding
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() {
describe('with dependencies', function() {
beforeEach(function() {
binding.options.dependencies = ['fname', 'lname'];
binding.options.dependencies = ['.fname', '.lname'];
});
it('sets up observers on the dependant attributes', function() {
......
......@@ -80,8 +80,16 @@ class Rivets.Binding
@sync() if Rivets.config.preloadData
if @options.dependencies?.length
for keypath in @options.dependencies
Rivets.config.adapter.subscribe @model, keypath, @sync
for dependency in @options.dependencies
if /^\./.test dependency
model = @model
keypath = dependency.substr 1
else
dependency = dependency.split '.'
model = @view.models[dependency.shift()]
keypath = dependency.join '.'
Rivets.config.adapter.subscribe model, keypath, @sync
if @isBidirectional()
bindEvent @el, 'change', @publish
......@@ -197,7 +205,7 @@ class Rivets.View
publish: =>
binding.publish() for binding in @select (b) -> b.isBidirectional()
# Cross-browser event binding
# Cross-browser event binding.
bindEvent = (el, event, fn) ->
# Check to see if jQuery is loaded.
if window.jQuery?
......@@ -211,6 +219,7 @@ bindEvent = (el, event, fn) ->
event = "on" + event
el.attachEvent event, fn
# Cross-browser event unbinding.
unbindEvent = (el, event, fn) ->
# Check to see if jQuery is loaded.
if window.jQuery?
......