Permalink
Browse files

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/
  • Loading branch information...
1 parent e433266 commit 9b0661eed3bfb4626390466898267ac01fe46cb9 @rstacruz rstacruz committed Jan 24, 2013
Showing with 0 additions and 19 deletions.
  1. +0 −18 spec/jquery.turbolinks_spec.coffee
  2. +0 −1 src/jquery.turbolinks.coffee
@@ -46,24 +46,6 @@ describe '$ Turbolinks', ->
callback1.should.have.been.calledWith($)
- it '''
- should remove all events delegated to
- document after trigger fetch
- ''', ->
- id = getUniqId()
- selector = '#' + id
- addEl = ->
- $('body').empty()
- $('<div>').attr(id: id).appendTo('body')
-
- addEl()
- $(document).on('event_name', selector, callback1)
- $(selector).trigger('event_name')
- $(document).trigger('page:fetch')
- addEl()
- $(selector).trigger('event_name')
- callback1.should.have.been.calledOnce
-
describe '$.setReadyEvent', ->
beforeEach ->
@@ -24,7 +24,6 @@ turbolinksReady = ->
# Fetch event handler
fetch = ->
- $(document).off(undefined, '**')
$.isReady = false
# Bind `ready` to DOM ready event

0 comments on commit 9b0661e

Please sign in to comment.