Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Multiple delegates #8

Closed
leoniddinershtein opened this Issue Nov 21, 2012 · 8 comments

Comments

Projects
None yet
4 participants

Hi,

I have a conceptual question.

With code like:

$(document).on(event, selector, handler)

handler delegates to selector multiply times. I wonder if it s a good idea to remove all delegates on page:load?

Currently I need to do $(document).off(event, selector) each time.

forgot to say, that $(document).on is nested in $(document).ready

glebm commented Nov 21, 2012

jQuery turbolinks should probably clear all event listeners on document
For now a workaround is not to attach this inside document ready!

kossnocorp added a commit that referenced this issue Nov 28, 2012

Owner

kossnocorp commented Nov 29, 2012

You problem should be solved by 6c75097.

I was released jquery-turbolinks v1.0.0-rc1, so can you confirm what everything works as expected?

Works like a charm

Thank you!

Owner

kossnocorp commented Nov 30, 2012

Great 😄. Thank you for reporting!

Collaborator

rstacruz commented Jan 19, 2013

forgot to say, that $(document).on is nested in $(document).ready

Curious, isn't the better solution to this to move it outside $(document).ready?

$(document).off(undefined, '**') will break some 3rd-party script behavior, like rails_ujs's. I reverted from v1.0.0-rc1 to v0.2.1 (which doesn't have this) to fix it.

rstacruz added a commit to rstacruz/jquery.turbolinks that referenced this issue Jan 24, 2013

Revert unbinding of all event delegates.
This reverts the change introduced in #8. This fixes #14 and #12.

The code `$(document).off(undefined, '**')` breaks 3rd-party
script behavior where events are delegated outside a `document.ready`
wrapper, like Rails's jquery_ujs.

For instance, having code that looks like this will fail after
navigating to a second page:

    // [A]
    $(document).on('click', 'button', function() { ... })

...this is exactly how scripts like jquery_ujs binds its events, and is
prescribed in jQuery's documentation for [fn.live].

To get around it, some people may misguidedly wrap it in a
`document.ready` wrapper like so. This is inefficient: events will then
be bound and un-bound repeatedly as you navigate through pages.

    // [B]
    $(function() {
      $(document).on('click', 'button', function() { ... })
    });

Therefore, [A] should be the recommended solution. This fix makes [A]
work again, but breaks the behavior of [B].

[fn.live]: http://api.jquery.com/live/
Owner

kossnocorp commented Jan 31, 2013

Now it's broken again and probably not be fixed in near future. Sorry but if you want to use latest version of jquery.turbolinks you should change your code and avoid document binding inside $(->) in future.

@kossnocorp kossnocorp reopened this Jan 31, 2013

@ghost ghost assigned kossnocorp Jan 31, 2013

rstacruz added a commit to rstacruz/jquery.turbolinks that referenced this issue Jan 31, 2013

Document caveats of binding document events
This describes the side-effects of jQuery Turbolinks in setups wherein events are bound to $(document).

Related: #8, #19.
Collaborator

rstacruz commented Feb 25, 2013

Hm, closing this—I don't think there's a solution to this. If anyone has further insight, just leave a comment or create a new issue.

@rstacruz rstacruz closed this Feb 25, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment