jquery.turbolinks icon indicating copy to clipboard operation
jquery.turbolinks copied to clipboard

Does not work with Rails 5 / Turbolinks 5

Open andrewhamon opened this issue 8 years ago • 20 comments

It looks like the latest version of Turbolinks doesn't use the same event names as before, causing this gem to not work with Rails 5.

andrewhamon avatar Mar 20 '16 08:03 andrewhamon

+1

u007 avatar Mar 22 '16 17:03 u007

:+1:

dalpo avatar Mar 24 '16 22:03 dalpo

Motioning to deprecate this gem and not implement support for rails 5. Anyone in favor/opposed? @kossnocorp

On Friday, March 25, 2016, Andrea Dal Ponte [email protected] wrote:

[image: :+1:]

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/kossnocorp/jquery.turbolinks/issues/56#issuecomment-201045993

rstacruz avatar Mar 24 '16 22:03 rstacruz

If this is deprecated, what should we use instead? Change our listeners to turbolinks:load?

dsandstrom avatar Mar 24 '16 23:03 dsandstrom

Personally, I would love the rails 5 support :smiley:

dalpo avatar Mar 25 '16 09:03 dalpo

This seems to work, add after requires in application.js: $.turbo.use('turbolinks:load', 'turbolinks:request-start')

Except I'm getting some leftover js validation errors after re-visiting a form. But it quickly resets after load.

Edit The leftover errors are present because they are getting cached by turbolinks. Here are some possible workarounds:

  • Disable turbolinks caching on form pages.

Bottom of head tag on application.html.erb:

<%= content_for?(:head) ? yield(:head) : '' %>

Form views:

<% content_for :head do %>
  <meta name="turbolinks-cache-control" content="no-cache">
<% end %>
  • Remove form errors before caching.

Bottom of application.js:

var resetForms = function () {
  // this depends on your use
  // this is for foundation 6's abide
  $('form').each(function () {
    $(this).foundation('destroy');
  });
};

document.addEventListener("turbolinks:before-cache", function() {
  resetForms();
});

dsandstrom avatar Mar 25 '16 17:03 dsandstrom

i added a work around, does this help? its in my comment somewhere at the bottom

https://github.com/turbolinks/turbolinks/issues/9

u007 avatar Mar 26 '16 02:03 u007

Sorry, I'm not involved into Turbolinks world for three years already (I'm a React-hipster ¯_(ツ)_/¯) and it's hard to me to make an adequate conclusion regarding depreciation issue.

I completed trust @rstacruz, but I'll love to see jQuery Turbolinks moving forward while there are users. If there are people who want to take control over the project, please welcome (on any your conditions).

kossnocorp avatar Mar 28 '16 06:03 kossnocorp

jQuery Turbolinks simply makes $(function) work like $(document).on('turbolinks:load'). There's no provision to teardown whatever was initialized using $(function).

As of Turbolinks 5, teardowns need to happen on turbolinks:before-cache.

Making jQuery Turbolinks work with Turbolinks 5 is not going to be simple.

jQuery Turbolinks's reason-for-living is these two things, as far as I'm concerned:

  1. syntactic sugar to make $(function) easier to write.
  2. make legacy plugins work.

As for (1), the additional abstraction isn't worth the small benefit of a few less bytes to write. As for (2), well, they're legacy... the responsibility to make them Turbolinks compatible should probably be better handled in those projects rather than a shim.

With that said, it should be easy to make jQuery Turbolinks work for TL5 by simply changing the event names, as others have noted. However, you'll likely run into some problems along the way as a result of the concerns above.

rstacruz avatar Mar 28 '16 06:03 rstacruz

I'd like to offer an alternative to make jQuery + Turbolinks a little easier to deal with:

http://ricostacruz.com/onmount

It isn't a replacement for jquery.turbolinks, but it may address some of the reasons you might want to use jquery.turbolinks in the first place.

rstacruz avatar Mar 28 '16 06:03 rstacruz

@rstacruz it's possible to re-build jquery.turbolinks on top of onmount API to simplify migration?

kossnocorp avatar Mar 28 '16 06:03 kossnocorp

@kossnocorp I'm not sure that's possible... the two of them work rather differently.

rstacruz avatar Mar 28 '16 06:03 rstacruz

As of Turbolinks 5, teardowns need to happen on turbolinks:before-cache.

Thanks for the link. So what I need to do for the form validation is destroy on turbolinks:before-cache and re-initialize on turbolinks:load.

dsandstrom avatar Mar 28 '16 07:03 dsandstrom

We recently implemented turbolinks 5 at work and this https://github.com/wshostak/turbolinks-jquery is the solution we came up with for dealing with jQuery on and ready. It allowed us to keep working with our current jQuery scripts/ plugins with out having to make changes to them. Thought I would share since seems like a few others like us could use it.

wshostak avatar Jul 29 '16 08:07 wshostak

I wrote an easy solution here: https://github.com/kossnocorp/jquery.turbolinks/issues/61#issuecomment-246404796

Basically add this file: https://github.com/turbolinks/turbolinks/blob/master/src/turbolinks/compatibility.coffee

Lowryderch avatar Sep 12 '16 16:09 Lowryderch

@Lowryderch that worked for me, using Rails 5 and Turbolinks 5 - Thanks a lot 💃

ryanbelke avatar Oct 23 '16 04:10 ryanbelke

This is my solution, override jQuery.fn.ready:

jQuery.fn.ready = (fn)->
  $(this).on 'turbolinks:load', fn

xiaohui-zhangxh avatar Nov 18 '16 08:11 xiaohui-zhangxh

@Lowryderch it works on local environment, but somehow doesn't work on staging/production environment.

Turbolinks 5.0.1

any suggestion will be appreciated

drselump14 avatar Dec 08 '16 05:12 drselump14

@wshostak solution worked for me. But I updated the code to handle events that are not String type( I encounter problem with datepicker ).

if (typeof events === 'string' || events instanceof String){
      var splitEvents = events.split(' ');
    }
    else{
      var splitEvents = Object.keys(events);
    }

merialvarado avatar Mar 08 '17 02:03 merialvarado

Something that I did which seems to make them work properly on Rails 5 is the following:

function_name_here = ->

  #YOUR CODE HERE

$(document).ready(function_name_here)
$(document).on('page:load', function_name_here)


document.addEventListener('turbolinks:request-start', function_name_here)
document.addEventListener('turbolinks:load', function_name_here)

Not sure if this is a recommended way to do things, but it seems to work by putting the listener on.

I am running Rails 5.0 and turbolinks 5.0 in my gemfile.

aldefouw avatar May 05 '17 14:05 aldefouw