Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Commit

Permalink
Deprecate bindTo and unbindFromAll
Browse files Browse the repository at this point in the history
Methods are now proxies to Backbone's `listenTo` and `stopListening`.
Add a deprecation warning when using `bindTo` and `unbindFromAll`.

Cleanup tests, removing unneeded assertions and updating `Observer`s
tests to ensure they call through to `listenTo` and `stopListening`.

Closes #21.
  • Loading branch information
jridgewell authored and ecbypi committed Feb 16, 2015
1 parent 1e6b24e commit 383e318
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 48 deletions.
38 changes: 25 additions & 13 deletions lib/assets/javascripts/backbone-support/observer.js
@@ -1,16 +1,28 @@
Support.Observer = function() {};
(function() {

_.extend(Support.Observer.prototype, {
bindTo: function(source, event, callback) {
source.bind(event, callback, this);
this.bindings = this.bindings || [];
this.bindings.push({ source: source, event: event, callback: callback });
},
Support.Observer = function() {};

unbindFromAll: function() {
_.each(this.bindings, function(binding) {
binding.source.unbind(binding.event, binding.callback);
});
this.bindings = []
// `console` is not defined when the Developer Tools are not open in older
// versions of Internet Explorer
function deprecate(message) {
/* global console */
if ( console && console.warn ) {
console.warn(message);
}
}
});

_.extend(Support.Observer.prototype, {
bindTo: function(source, event, callback) {
deprecate("Using #bindTo has been deprecated. Use #listenTo instead.");
this.listenTo(source, event, callback);
},

unbindFromAll: function() {
deprecate(
"Using #unbindFromAll has been deprecated. Use #stopListening instead."
);

this.stopListening();
}
});
})();
16 changes: 0 additions & 16 deletions spec/javascripts/composite_view_spec.js
Expand Up @@ -263,19 +263,6 @@ describe("Support.CompositeView", function() {
});
});

describe("#bindTo", function() {
var view = new orangeView();
var callback = sinon.spy();
var source = new Backbone.Model({
title: 'Model or Collection'
});

it("calls the unbindFromAll method when leaving the view", function() {
view.bindTo(source, 'foobar', callback);
expect(view.bindings.length).toEqual(1);
});
});

describe("#unbindFromAll", function() {
it("calls the unbindFromAll method when leaving the view", function() {
var view = new orangeView();
Expand All @@ -287,9 +274,6 @@ describe("Support.CompositeView", function() {

view.render();
view.bindTo(source, 'foo', callback);

expect(view.bindings.length).toEqual(1);

view.leave();

expect(spy.called).toBeTruthy();
Expand Down
25 changes: 6 additions & 19 deletions spec/javascripts/observer_spec.js
Expand Up @@ -24,7 +24,7 @@ describe('Support.Observer', function() {
var view, spy, source, callback;
beforeEach(function() { Helpers.setup();
view = new normalView();
spy = sinon.spy(view, "bindTo");
spy = sinon.spy(view, "listenTo");
callback = sinon.spy();

source = new Backbone.Model({
Expand All @@ -36,17 +36,9 @@ describe('Support.Observer', function() {
view, spy, source, callback = null;
});

it("should add the event to the bindings for the view", function() {
view.bindTo(source, 'foobar', callback);
expect(view.bindings.length).toEqual(1);
});

it("binds the event to the source object", function() {
var mock = sinon.mock(source).expects('bind').once();

it("calls listenTo on this", function() {
view.bindTo(source, 'change:title', callback);

mock.verify();
expect(spy.called).toBeTruthy();
});
});

Expand All @@ -56,16 +48,15 @@ describe('Support.Observer', function() {
beforeEach(function() {
view = new normalView();
spy = sinon.spy(view, 'unbindFromAll');
stopListeningSpy = sinon.spy(view, 'stopListening');
callback = sinon.spy();
source = new Backbone.Model({
title: 'Model or Collection'
});
unbindSpy = sinon.spy(source, 'unbind');

view.render();
view.bindTo(source, 'foo', callback);
view.bindTo(source, 'bar', callback);
expect(view.bindings.length).toEqual(2);

view.leave();
});
Expand All @@ -74,12 +65,8 @@ describe('Support.Observer', function() {
expect(spy.called).toBeTruthy();
});

it("calls unbind on the source object", function() {
expect(unbindSpy.calledTwice).toBeTruthy();
});

it("removes all the views bindings attached with bindTo", function() {
expect(view.bindings.length).toEqual(0);
it("calls stopListening on this", function() {
expect(stopListeningSpy.called).toBeTruthy();
});
});

Expand Down

0 comments on commit 383e318

Please sign in to comment.