I’ve been struggling on understanding what and why my Js gets broken up between different page loads.
Currently I have a rails app with 4 different layouts ( The layouts are for special scripts and features based on the section of the app. For example Application Layout is different from my index lay out.
Even with the different layouts they load the application js and application cs files the same
My header
<%= javascript_include_tag "application", async: true %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
All of my layouts are loaded like this except for my front page that uses a seperate js file.
<%= javascript_include_tag "home", async: true %>
Navigating through the site it looks like all is well, but very quickly it seems turbo links breaks some js features on page change.
Currenly my application js looks as listed
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery-ui
//= require bootstrap-sprockets
//= require_self
//= require global
//
//= require browser_timezone_rails/set_time_zone
//= require jstz
//= require js.cookie
//= require cocoon
//= require bootstrap-tagsinput
//= require datetime_picker_input
//= require twitter/typeahead
//= require twitter/typeahead.min
//= require intlTelInput
//= require libphonenumber/utils
//= require underscore
//= require gmaps-auto-complete
//= require jquery.wookmark
//= require jquery.facebox
//= require social-share-button
//= require chosen-jquery
//= require raphael
//= require morris
//= require gmaps/google
//= require turbolinks
//= require sitewide
Sitewide and Global are the only custom files within my application js
Sitewide.js
var App = {
onLoadFns: [],
onPageLoad: function(callback) {
this.onLoadFns.push(callback);
},
load: function() {
_.each(this.onLoadFns, function(callback) {
callback.call(this);
}, this);
}
};
$(function() {
Turbolinks.enableProgressBar();
// Called everytime turbolinks loads a new page
$(document).on("page:load", function() {
App.load();
});
// Called on initial full page load
//
// defer is used to allow all features to register
// their page load callbacks before App.load runs
_.defer(function() {
App.load();
});
});
$(document).on('page:fetch', function() {
$(".loading-indicator").show();
});
$(document).on('page:change', function() {
$(".loading-indicator").hide();
});
And Global contains the code for gmaps auto complete
jQuery(function() {
var completer;
completer = new GmapsCompleter({
inputField: '#gmaps-input-address',
errorField: '#gmaps-error'
});
completer.autoCompleteInit({
country: "us"
});
});
My issues are the js breaks when changing pages, I don’t know if my issue is more deeper than the arrangement of the application js but it seems like from all the tutorials. After searching through almost all I can find that includes how to handle Jquery turbolinks with turbolinks. including linked below
Rails 4: how to use $(document).ready() with turbo-links
But none seems to be helping my issue. I’m also having the thoughts that organizing according to turbolinks is half the issue , andd that I also need to organize imported js from gems . Which I can’t find anything with on how to organize imported gems to work with turbolinks and jquery turbolinks. ANy help would be seriously much appreciated. I’ve been dealing with this issue and winning short battles for it only to come bite me in the butt later.
So any help on how to arrange my application js or anything I’m sort of doing wrong with my layouts because I am lost.
Thanks friends!
2
Answers
I encountered the same problem and i was scratching my head what i did to break all pages that have js. The problem started when i updated my
Turbolinks from 2.x
toTurbolinks 5
The reasons for this is some of the events are different names or have been updated from the previous version. Thankfully, they included a shims that make old events mapped to the new events. you can look it hereor copy and paste the code below, create a file say:
compatibility.coffee
and put it underjavascripts/compatibility.coffee
Hope this helps you. good luck.
Try this approach.
http://brandonhilkert.com/blog/organizing-javascript-in-rails-application-with-turbolinks/
Found that author is using this method to build complex rails app with turbolinks.