-
Notifications
You must be signed in to change notification settings - Fork 46
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
Sugar for one-time listeners #10
Comments
It would be better if it didn't remove the event of all elements of the selector, just from the element which triggered the event. |
@appsforartists, this is more complex than your code sample lets on. Let's say you do this: var myElement = document.createElement('div');
var handleClick = function(event) {
console.log('Clicked!');
};
// Let's handle only the first click to myElement
Gator(myElement).onFirst('click', handleClick);
// We've changed our mind, we don't want to handle that click anymore
Gator(myElement).off('click', handleClick);
// But alas, when the user clicks, handleClick is still called! It doesn't work, because One solution to this problem would be to maintain a WeakMap so you could find the actual function given the user-provided function. However, I don't think this is a feature that Gator should be concerned with -- this is not event delegation, -1. With Gator in its current form, users can implement this functionality very easily using named functions: var myElement = document.createElement('div');
// Pass a named function to on()
Gator(myElement).on('click', function handleClick(event) {
console.log('Clicked!');
// handleClick will refer to this function, so we can easily remove the listener
Gator(myElement).off('click', handleClick);
}); |
@lazd That's because the |
@englishextra I think we're talking about two different things. Look at the code in the original issue, it passes |
@lazd Oh yeah Then I guess this has to be established as a good rule for that issue:
|
It would be nice for the Gator standard library to include sugar for one-time listeners, namely
onFirst
andonNth
. Here's a sample implementation of onFirst:If @ccampbell approves of this sugar, I'll open a pull request.
The text was updated successfully, but these errors were encountered: