jQuery Turbolinks

Build Status

Do you like Turbolinks? It's easy and fast way to improve user experience of surfing on your website.

But if you have a large codebase with lots of $(el).bind(...) Turbolinks will surprise you. Most part of your JavaScripts will stop working in usual way. It's because the nodes on which you bind events no longer exist.

I wrote jquery.turbolinks to solve this problem in my project. It's easy to use: just require it immediately after jquery.js. Your other scripts should be loaded after jquery.turbolinks.js, and turbolinks.js should be after your other scripts.

Initially sponsored by Evil Martians.

This project is a member of the OSS Manifesto.

Important

This readme points to the master version (v2.0.0) of jQuery Turbolinks. For older versions, see v1.0.0rc2 README.

Usage

Gemfile:

gem 'jquery-turbolinks'

Add it to your JavaScript manifest file, in this order:

//= require jquery
//= require jquery.turbolinks
//
// ... your other scripts here ...
//
//= require turbolinks

And it just works!

API and Customization

$.turbo.use

By default, jQuery.Turbolinks is bound to page:load and page:fetch. To use different events (say, if you're not using Turbolinks), use:

$.turbo.use('pjax:start', 'pjax:end');

$.turbo.isReady

You can check if the page is ready by checking $.turbo.isReady, which will be either true or false depending on whether the page is loading.

Troubleshooting

Events firing twice or more

If you find that some events are being fired multiple times after using jQuery Turbolinks, you may have been binding your document events inside a $(function()) block. For instance, this example below can be a common occurrence and should be avoided:

/* BAD: don't bind 'document' events while inside $()! */
$(function() {
  $(document).on('click', 'button', function() { ... })
});

You should be binding your events outside a $(function()) block. This will ensure that your events will only ever be bound once.

/* Good: events are bound outside a $() wrapper. */
$(document).on('click', 'button', function() { ... })

Changelog

This project uses Semantic Versioning for release numbering.

2.0.0 (unreleased - master)

  • Rewritten, smaller, with a completely new API. This deprecates the old $.setFetchEvent and $.setReadyEvent API.

1.0.0 (April 5, 2013)

  • Add uglified version.

1.0.0-rc2 (January 31, 2013)

1.0.0-rc1 (November 28, 2012)

  • Set $.isReady to false after page:fetch #6;
  • add $.setFetchEvent function;
  • remove all delegated events after trigger fetch event #8.

1.0.0-rc (November 8, 2012)

  • Add turbolinks as dependency (kudos to @gbchaosmaster);
  • run callback after adding to waiting list if $.isReady #6.

0.2.1 (October 15, 2012)

  • Pass jQuery object to each callback #4

0.2.0 (October 10, 2012)

  • Change event: page:change -> page:load (kudos to @davydotcom);
  • added ability to change ready event via $.setReadyEvent

0.1.0 (October 3, 2012)

  • First, initial release

Contributors

Initial idea and code by @kossnocorp, with special thanks to @rstacruz and other the project's contributors.

License

The MIT License