GMaps4Rails and Turbolinks not loading without full page refresh

前端 未结 6 1721
悲哀的现实
悲哀的现实 2020-12-28 09:32

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

6条回答
  •  执念已碎
    2020-12-28 09:43

    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.

提交回复
热议问题