Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat(jqLite): provide support for element.one() #5269

Closed
wants to merge 1 commit into from

Conversation

matsko
Copy link
Contributor

@matsko matsko commented Dec 4, 2013

No description provided.

@mary-poppins
Copy link

Hi Matias :)

@ghost ghost assigned matsko Dec 4, 2013
@IgorMinar
Copy link
Contributor

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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)

Copy link
Contributor Author

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.

Copy link
Contributor

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!

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok cool. Merging.

@IgorMinar
Copy link
Contributor

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.
PS2: If you are a Googler, please sign the CLA as well to simplify the CLA verification process.

@caitp
Copy link
Contributor

caitp commented Dec 10, 2013

I think that script needs some work :s

@IgorMinar
Copy link
Contributor

@caitp I messed up. it's fixed now: angular/google-cla-verifier-for-github@04bfa13

sorry

@caitp
Copy link
Contributor

caitp commented Dec 10, 2013

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
[email protected]

On Dec 10, 2013, at 5:56 PM, Igor Minar [email protected] wrote:

@caitp I messed up. it's fixed now: angular/google-cla-verifier-for-github@04bfa13

sorry


Reply to this email directly or view it on GitHub.

@IgorMinar
Copy link
Contributor

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).

@matsko
Copy link
Contributor Author

matsko commented Dec 11, 2013

@IgorMinar is half man / half robot :)

@matsko
Copy link
Contributor Author

matsko commented Dec 19, 2013

MERGED

@matsko
Copy link
Contributor Author

matsko commented Dec 19, 2013

Landed as 937caab

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

Successfully merging this pull request may close these issues.

4 participants