8000 off unbinds the correct bound function by angus-c · Pull Request #192 · flightjs/flight · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

off unbinds the correct bound function #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,13 @@ define(

callback = originalCb.bind(this);
callback.target = originalCb;

// if the original callback is already branded by jQuery's guid, copy it to the context-bound version
if (originalCb.guid) {
callback.guid = originalCb.guid;
}
callback.context = this;

$element.on(type, callback);

// get jquery's guid from our bound fn, so unbinding will work
originalCb.guid = callback.guid;
// store every bound version of the callback
originalCb.bound || (originalCb.bound = []);
originalCb.bound.push(callback);

return callback;
};
Expand All @@ -148,6 +145,17 @@ define(
type = arguments[0];
}

if (callback) {
//set callback to version bound against this instance
callback.bound && callback.bound.some(function(fn, i, arr) {
if (fn.context && (this.identity == fn.context.identity)) {
arr.splice(i, 1);
callback = fn;
return true;
}
}, this);
}

return $element.off(type, callback);
};

Expand Down
24 changes: 24 additions & 0 deletions test/spec/events_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ define(['lib/component'], function (defineComponent) {
describe("(Core) events", function () {
var Component = (function () {
function testComponent() {
this.exampleMethod = jasmine.createSpy();
this.after('initialize', function () {
this.testString || (this.testString = "");
this.testString += "-initBase-";
this.on(document, 'exampleDocumentEvent', this.exampleMethod);
});
}

Expand All @@ -33,12 +35,15 @@ define(['lib/component'], function (defineComponent) {

beforeEach(function () {
window.outerDiv = document.createElement('div');
window.anotherDiv = document.createElement('div');
window.innerDiv = document.createElement('div');
window.outerDiv.appendChild(window.innerDiv);
document.body.appendChild(window.outerDiv);
document.body.appendChild(window.anotherDiv);
});
afterEach(function () {
document.body.removeChild(window.outerDiv);
document.body.removeChild(window.anotherDiv);
window.outerDiv = null;
window.innerDiv = null;
Component.teardownAll();
Expand Down Expand Up @@ -120,6 +125,17 @@ define(['lib/component'], function (defineComponent) {
expect(spy).not.toHaveBeenCalled();
});

it('retains one binding when another is removed for multiple registered events for the same callback function using "off"', function () {
var instance1 = (new Component).initialize(window.outerDiv);
var spy = jasmine.createSpy();
instance1.on(document, 'event1', spy);
instance1.on(document, 'event2', spy);
instance1.off(document, 'event1', spy);
instance1.trigger('event2');
expect(spy).toHaveBeenCalled();
});


it('does not unbind those registered events that share a callback, but were not sent "off" requests', function () {
var instance1 = (new Component).initialize(window.outerDiv);
var spy = jasmine.createSpy();
Expand All @@ -130,6 +146,14 @@ define(['lib/component'], function (defineComponent) {
expect(spy).toHaveBeenCalled();
});

it('does not unbind event handlers which share a node but were registered by different instances', function () {
var instance1 = (new Component).initialize(window.outerDiv);
var instance2 = (new Component).initialize(window.anotherDiv);
instance1.off(document, 'exampleDocumentEvent', instance1.exampleMethod);
instance1.trigger('exampleDocumentEvent');
expect(instance2.exampleMethod).toHaveBeenCalled();
});

it('bubbles custom events between components', function () {
var instance1 = (new Component).initialize(window.innerDiv);
var instance2 = (new Component).initialize(window.outerDiv);
Expand Down
0