Timmy Von Heiss
Timmy Von Heiss

Reputation: 2218

how to get this jquery to work with Rails 4 and Turbolinks?

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);
      });
    });
    

Upvotes: 1

Views: 105

Answers (1)

Timmy Von Heiss
Timmy Von Heiss

Reputation: 2218

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.

Upvotes: 1

Related Questions