29f7ffa2 by Michael Richards

Merge pull request #43 from mikeric/dependant-attributes-for-computed-properties

Computed properties
2 parents c368733e 71bab2f6
......@@ -26,7 +26,7 @@ describe('Rivets.Binding', function() {
it('subscribes to the model for changes via the adapter', function() {
spyOn(rivets.config.adapter, 'subscribe');
binding.bind();
expect(rivets.config.adapter.subscribe).toHaveBeenCalled();
expect(rivets.config.adapter.subscribe).toHaveBeenCalledWith(model, 'name', binding.set);
});
describe('with preloadData set to true', function() {
......@@ -38,8 +38,8 @@ describe('Rivets.Binding', function() {
spyOn(binding, 'set');
spyOn(rivets.config.adapter, 'read');
binding.bind();
expect(rivets.config.adapter.read).toHaveBeenCalledWith(model, 'name');
expect(binding.set).toHaveBeenCalled();
expect(rivets.config.adapter.read).toHaveBeenCalled();
});
});
......@@ -55,6 +55,19 @@ describe('Rivets.Binding', function() {
expect(binding.set).toHaveBeenCalledWith('espresso');
});
});
describe('with dependencies', function() {
beforeEach(function() {
binding.options.dependencies = ['fname', 'lname'];
});
it('sets up observers on the dependant attributes', function() {
spyOn(rivets.config.adapter, 'subscribe');
binding.bind();
expect(rivets.config.adapter.subscribe).toHaveBeenCalledWith(model, 'fname', binding.dependencyCallbacks['fname']);
expect(rivets.config.adapter.subscribe).toHaveBeenCalledWith(model, 'lname', binding.dependencyCallbacks['lname']);
});
});
});
describe('set()', function() {
......
......@@ -63,6 +63,18 @@ class Rivets.Binding
if Rivets.config.preloadData
@set Rivets.config.adapter.read @model, @keypath
if @options.dependencies?.length
@dependencyCallbacks = {}
for keypath in @options.dependencies
callback = @dependencyCallbacks[keypath] = (value) =>
@set if @options.bypass
@model[@keypath]
else
Rivets.config.adapter.read @model, @keypath
Rivets.config.adapter.subscribe @model, keypath, callback
if @type in @bidirectionals
bindEvent @el, 'change', @publish
......@@ -75,6 +87,11 @@ class Rivets.Binding
unbind: =>
Rivets.config.adapter.unsubscribe @model, @keypath, @set
if @options.dependencies?.length
for keypath in @options.dependencies
callback = @dependencyCallbacks[keypath]
Rivets.config.adapter.unsubscribe @model, keypath, callback
if @type in @bidirectionals
@el.removeEventListener 'change', @publish
......@@ -104,12 +121,16 @@ class Rivets.View
type = attribute.name.replace bindingRegExp, ''
pipes = (pipe.trim() for pipe in attribute.value.split '|')
path = pipes.shift().split(/(\.|:)/)
context = (ctx.trim() for ctx in pipes.shift().split '>')
path = context.shift().split /(\.|:)/
options.formatters = pipes
model = @models[path.shift()]
options.bypass = path.shift() is ':'
keypath = path.join()
if dependencies = context.shift()
options.dependencies = dependencies.split /\s+/
if eventRegExp.test type
type = type.replace eventRegExp, ''
options.special = "event"
......