Skip to content

Commit

Permalink
Fixes #85
Browse files Browse the repository at this point in the history
  • Loading branch information
Dheeraj Kumar authored and mikeric committed Nov 9, 2012
1 parent e0a4fdc commit 6c26680
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
52 changes: 52 additions & 0 deletions spec/rivets/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ describe('Rivets.Binding', function() {
expect(rivets.config.adapter.subscribe).toHaveBeenCalledWith(model, 'name', binding.sync);
});

it("calls the binder's bind method if one exists", function() {
expect(function(){
binding.bind();
}).not.toThrow(new Error());

binding.binder.bind = function(){};
spyOn(binding.binder, 'bind');
binding.bind();
expect(binding.binder.bind).toHaveBeenCalled();
});

describe('with preloadData set to true', function() {
beforeEach(function() {
rivets.config.preloadData = true;
Expand All @@ -54,6 +65,17 @@ describe('Rivets.Binding', function() {
binding.bind();
expect(binding.set).toHaveBeenCalledWith('espresso');
});

it("calls the binder's bind method if one exists", function() {
expect(function(){
binding.bind();
}).not.toThrow(new Error());

binding.binder.bind = function(){};
spyOn(binding.binder, 'bind');
binding.bind();
expect(binding.binder.bind).toHaveBeenCalled();
});
});

describe('with dependencies', function() {
Expand All @@ -70,6 +92,36 @@ describe('Rivets.Binding', function() {
});
});

describe('unbind()', function() {
it("calls the binder's unbind method if one exists", function() {
expect(function(){
binding.unbind();
}).not.toThrow(new Error());

binding.binder.unbind = function(){};
spyOn(binding.binder, 'unbind');
binding.unbind();
expect(binding.binder.unbind).toHaveBeenCalled();
});

describe('with the bypass option set to true', function() {
beforeEach(function() {
binding.options.bypass = true;
});

it("calls the binder's unbind method if one exists", function() {
expect(function(){
binding.unbind();
}).not.toThrow(new Error());

binding.binder.unbind = function(){};
spyOn(binding.binder, 'unbind');
binding.unbind();
expect(binding.binder.unbind).toHaveBeenCalled();
});
});
});

describe('set()', function() {
it('performs the binding routine with the supplied value', function() {
spyOn(binding.binder, 'routine');
Expand Down
6 changes: 4 additions & 2 deletions src/rivets.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ class Rivets.Binding
# routines will also listen for changes on the element to propagate them back
# to the model.
bind: =>
@binder.bind?.call @, @el

if @options.bypass
@sync()
else
@binder.bind?.call @, @el
Rivets.config.adapter.subscribe @model, @keypath, @sync
@sync() if Rivets.config.preloadData

Expand All @@ -90,8 +91,9 @@ class Rivets.Binding

# Unsubscribes from the model and the element.
unbind: =>
@binder.unbind?.call @, @el

unless @options.bypass
@binder.unbind?.call @, @el
Rivets.config.adapter.unsubscribe @model, @keypath, @sync

if @options.dependencies?.length
Expand Down

0 comments on commit 6c26680

Please sign in to comment.