I\'m using gmaps4rails to load a map on a \"clients\" show page. I\'ve integrated turbolinks to make the app speadier all together, but now I\'m hitting an issue where I hav
I had the same problem, but I did not want to turn off turbolinks and I couldn't get it right turbolink DOM listeners.
While fixing another problem and refactoring my javascript I resolved this by accident.
This is my functions.js
var init_map = function (allMarkers) {
handler = Gmaps.build('Google');
handler.buildMap({ provider: {maxZoom:17, minZoom:5}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(allMarkers());
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
};
var marker_check = function(){...};
var init_autocomplete = function () {...};
//function to call google maps functions
var init_google_api = function(function_name){
$(document).ready(function(){
google.maps.event.addDomListener(window, "load", function_name );
});
};
Since I am using also google places libary I have several functions that I can call with the init_google_api()
In each view where I want maps I just put a
<%= javascript_tag do %>
init_google_api(init_map(marker_check));
<% end %>
In the very end of the view. Fixed everything + keeps the code dry for wanting more than one map etc.
If I'm not mistaken, this approach goes around the turbolink problem, because each time a view is called, it executes the init_google_api function and the function that is passed in as the parameter ( this why I use function expressions). So the code is run each time the function is called.