-
Notifications
You must be signed in to change notification settings - Fork 27.5k
feat(jqLite): provide support for element.one() #5269
Conversation
Hi Matias :) |
Lgtm |
|
||
//add the listener twice so that when it is called | ||
//you can remove the original function and still be | ||
//able to call element.off(ev, fn) normally |
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 matching fn
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 do off(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?
one: function(element, type, fn) {
jqLite(element).on(type, function wrapperFn(e) {
// unbind first --- this way, if fn() binds the event again, we don't undo their work
element.off(e.type, wrapperFn);
fn.apply(this, arguments);
});
}
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 to on()
then how can we specifically remove the event created by one()
(type
+ fn
) from the element?
This won't work:
element.one('click', clickFn);
element.off('click', clickFn);
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.
I'm sorry, but I wasn't able to verify your CLA signature. CLA signature is required for any code contributions to AngularJS. Please sign our CLA and ensure that the CLA signature email address and the email address in this PR's commits match. If you signed the CLA as a corporation, please let me know the company's name. Thanks a bunch! PS: If you signed the CLA in the past then most likely the email addresses don't match. Please sign the CLA again or update the email address in the commit of this PR. |
I think that script needs some work :s |
@caitp I messed up. it's fixed now: angular/google-cla-verifier-for-github@04bfa13 sorry |
No apology is necessary, I find it a bit interesting that the email address is used though, in place of the author name. I guess I’ll sign again real quick. Caitlin Potter On Dec 10, 2013, at 5:56 PM, Igor Minar [email protected] wrote:
|
CLA signature verified! Thank you! Someone from the team will now triage your PR and it will be processed based on the determined priority (doc updates and fixes with tests are prioritized over other changes). |
@IgorMinar is half man / half robot :) |
MERGED |
Landed as 937caab |
No description provided.