0

turbolinks (5.0.0)
Rails 4.2.2

Is there DRY to make every link_to in your app data: {turbolinks: false} ? or does that defeat the purpose of Turbolinks and suggest that it should just be removed?

A large number of my jquery scripts will not work correctly if a link is clicked unless it contains data: {turbolinks: false }, the scripts will work correctly when the page is reloaded.

I have my jquery wrapped in: $(document).on('turbolinks:load', function () { #something here});

Timmy Von Heiss
  • 2,160
  • 17
  • 39

1 Answers1

1

So, the question you asked was "how to DRY up my endless link annotations?" I'll answer that, first...

Yes, if you turbolinks: false everything, then yes, you should just remove Turbolinks. Not because it's DRY, but because then you're disabling Turbolinks everywhere and might as well remove it. It doesn't do anything but deal with link-clicking.

If I can be so bold, though, I think your real question is:

"How do I get these cool jQuery plugins to work? I want them, and I never really asked for Turbolinks."

Without getting into specific plugins, many of them latch onto jQuery's $ function, which attaches to DOMContentLoaded. Turbolinks fires a different event instead (in Turbolinks-Classic this was the page:load event... in Turbolinks 5 it's the turbolinks:load event, that event isn't emitted and those event handlers don't get reset.

One possible answer for how to structure your JavaScript initializers is here.

I personally have stuck with Turbolinks, and as one last pitch, I'll point you towards Nate Berkopec's article 100ms to Glass with Rails and Turbolinks. He makes a reasonable case for why Turbolinks is a Good Thing.

Community
  • 1
  • 1
Lanny Bose
  • 1,811
  • 1
  • 11
  • 16
  • I have tested using `var ready = function() { }`
    `$(document).on('turbolinks:load', ready);` and it still does not work.. however, I have not placed any wrapper at all on the jquery plugin `masonry.pkgd.min.js` and `imagesloaded.pkgd.min.js`and I am wondering if that is the issue?
    – Timmy Von Heiss Jan 01 '17 at 21:49
  • I tried to wrap those plugins up with `var ready = function(){ }`
    `$(document).on('turbolinks:load', ready);` and `$(document).on('turbolinks:load', function(){ });` and both resulted on errors that prevented the scripts from working.
    – Timmy Von Heiss Jan 01 '17 at 21:55
  • btw, there was small debate from another (now deleted) answer of whether there is any difference at all between these two wrappers. From my testing, they don't seem to be any different. – Timmy Von Heiss Jan 01 '17 at 21:56
  • It may be a question then of individual plugins and how they're initialized. Maybe it would make sense to debug one specific one to see what it's doing? – Lanny Bose Jan 01 '17 at 23:47