This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(jqLite): provide support for element.one() #5269
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand the reasoning for this, it doesn't appear to happen in jQuery, and element.off() shouldn't throw if it can't find the handler anyways right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you call
$(elm).one(ev, fn)
and then$(elm).off(ev, fn)
in jQuery then that works (it removes the event). If you only stuck to having one listener (by doing on() inside of this function) then$(elm).off(ev, fn)
won't find the matchingfn
function since you used your own internal function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right but what I mean is, does it really matter if .off() doesn't find the matching fn? I'm not sure I understand what this breaks (reading the jQuery code I don't see it adding the unwrapped function if one===1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't matter if you do
off(ev)
, but if you dooff(ev, fn)
and it doesn't find the matching fn then it isn't actually removing the event listener now is it? We didn't want to rip open the existing jqLite.on() method to pass in another param like jQuery does.It doesn't break anything, but if you're a developer adding one() and then off() right after on the same event + callback pair and it's not getting removed then it isn't working as you expect it to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I just mean, it wasn't added in the first place --- therefore what difference does it really make if it wasn't removed? that's the part I don't get. It's clear that this is making sense to you so I think I will have to accept that, but it just seems weird to me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@caitp I'm planning on merging this in today. Could you hop onto the hangouts chat so I can understand what you mean here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, but it's really just me not completely understanding the need to bind both events.
Wouldn't it be just as well to bind only one function to wrap the passed in function?
In my mind this should work and be a bit simpler, it probably doesn't make much difference so it just seemed unclear to me and I wanted to hear what the reasoning was for it -- the only reason I can think of really is the ordering of the unbinding, but that seems unlikely to break anything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here lies the problem. If we delegate
one()
directly toon()
then how can we specifically remove the event created byone()
(type
+fn
) from the element?This won't work:
Can you see why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I see what you're getting at. Thanks for the explanation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok cool. Merging.