Skip to content

support removing specific attached events #106

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
May 13, 2016
Merged

Conversation

jsdw
Copy link
Contributor

@jsdw jsdw commented May 13, 2016

This is a feature I needed to make ResizeSensor.js (as a standalone lib) useful for me at work; its only a small addition and seems like others would find it useful too :)

Basically, this allows you to pass the original event in to .detach methods as well as the element (fully optional) to only remove that event. from the queue.

if(typeof ev == "function"){
element.resizedAttached.remove(ev);
if(element.resizedAttached.length()) return;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? The whole element.resizedAttached should be deleted, so I don't understand why you would want to remove only one event.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the following possible:

var fn1 = function(){ console.log("resize 1") }
ResizeSensor(element, fn1);

var fn2 = function(){ console.log("resize 2") }
ResizeSensor(element, fn2);

// at this point, resizing el would log
//
// resize 1
// resize 2
// resize 1
// resize 2
//...

ResizeSensor.detach(element, fn2);

// now, resizing would log only
//
// resize 1
// resize 1
// resize 1
//...

This sort of approach mirrors the native addEventListener/removeEventListener interface, which allows you to remove specific events by passing a ref to the original function.

In the event that you call ResizeSensor.detach(element,fn) and the fn removed leaves none left in the event queue, we do the rest of the clean up as well, else we leave the rest since it's necessary for the remaining events.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright, thanks for explanation!

@marcj marcj merged commit 7f80a35 into marcj:master May 13, 2016
@marcj
Copy link
Owner

marcj commented May 13, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants