1

Rails 4.2.2
gem turbolinks
//= require turbolinks

I have had success using $(document).on('page:change', function () { // }); but this script is being difficult. I do not use the jquery-turbolinks gem.

  1. Unlike other scripts in my app, it will not work if I place it inside of the assets/javascripts folder. It only works if the script is placed in the html.erb view file.

  2. It works correctly only the first time. Meaning, I want it run and then run again assigning new variable values, because all of the variable values will change after it is run the first time. Instead, when the function is initiated a second time (by clicking on var craw = $('.glyphicon-chevron-right').last()) it acts like it is being clicked on for the first time and adds the same page again and opens the same modal again because the variable values have not been updated.

    $(document).on('page:change', function () {
      var xyz = $('.nextinst').last().attr('href')
      var mod = $('.modal'+xzy)
      var turn = $('.pagination .next_page a').attr('href')    
      var craw = $('.glyphicon-chevron-right').last()
      $(craw).on('click', function() {
         if (!$(mod).size() > 0) {
           $.getScript(turn); 
         }
         setTimeout (function() {
          $(xyz).modal()
         }, 1000);
      });
    });
    
Timmy Von Heiss
  • 2,160
  • 17
  • 39
  • See http://stackoverflow.com/a/18834209/2144445 – inye Sep 12 '16 at 18:46
  • Possible duplicate of [Rails 4: how to use $(document).ready() with turbo-links](http://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links) – inye Sep 12 '16 at 18:48
  • Rails/Turbolinks versions? – ArtOfCode Sep 12 '16 at 19:08
  • @ArtOfCode I am currently using `Rails 4.2.2` and `gem 'turbolinks' = 2.3.0` – Timmy Von Heiss Sep 12 '16 at 20:55
  • @inye `$(document).on('page:change', function () { // });` was the solution to the `$(document).ready()` problem. I have upgraded to `gem turbolinks = 5.0` using `$(document).on('turbolinks:load', function)` but the script is still not running correctly. is there a way to force the script to reload the variables before it restarts? – Timmy Von Heiss Sep 13 '16 at 17:52
  • @TimmyVonHeiss you have try with this gem https://github.com/kossnocorp/jquery.turbolinks?? – inye Sep 13 '16 at 23:23

1 Answers1

1

It turned out that the problem was in the java script itself and not related to Turbolinks. Either $(document).on('page:change', function () { // }); with older version of Turbolink or $(document).on('turbolinks:load', function) with newer versions is the way to get jquery scripts to run with Turbolinks.

Timmy Von Heiss
  • 2,160
  • 17
  • 39