Skip to content

Instantly share code, notes, and snippets.

@gabeno
Created February 13, 2014 11:01
Show Gist options
  • Save gabeno/8973233 to your computer and use it in GitHub Desktop.
Save gabeno/8973233 to your computer and use it in GitHub Desktop.
remove callback bug in backbone?
'use strict';
var chai = require('chai'),
expect = chai.expect,
sinon = require('sinon'),
sinonChai = require('sinon-chai');
var Backbone = require('backbone');
var _ = require('lodash/dist/lodash.underscore');
chai.use(sinonChai);
describe('Backbone.Events', function() {
var myObj;
beforeEach(function() {
myObj = {};
_.extend(myObj, Backbone.Events);
});
it('can remove custom events from objects', function() {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
var spy3 = sinon.spy();
myObj.on('foo', spy1);
myObj.on('bar', spy1);
myObj.on('foo', spy2);
myObj.on('foo', spy3);
// unbind a single callback for the event
myObj.off('foo', spy1);
myObj.trigger('foo');
expect(spy1).to.have.callCount(0);
expect(spy2).to.have.callCount(1);
expect(spy3).to.have.callCount(1);
// remove all 'foo' callbacks
myObj.off('foo');
myObj.trigger('foo');
expect(spy1).to.have.callCount(0);
expect(spy2).to.have.callCount(0); // spy called once, why??
expect(spy3).to.have.callCount(0); // spy called once, why??
});
});
@gabeno
Copy link
Author

gabeno commented Feb 13, 2014

How come spy2and spy3 are still around even after unbinding them from foo event?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment