带涡轮连杆和窗口负载的导轨 4 [英] rails 4 with turbolinks and window load

查看:22
本文介绍了带涡轮连杆和窗口负载的导轨 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Rails 4.

I'm using Rails 4.

我正在尝试使用此脚本来显示谷歌地图.我有一个包含不同地方的页面,每个地方都有一个地址.它显示在谷歌地图中.
所以我使用分页,在每一页上我有 4 个位置.gmap 的 4 个脚本.
但此脚本仅在页面重新加载(ctrl+R 或 F5)时初始化,这是因为 turbolinks.

I'm trying to use this script to show google maps. I got a page with different places and each place got an address. It shows in google maps.
So i'm using pagination and on each page I got 4 places. 4 scripts of gmap.
But this scripts initializes only on page reload (ctrl+R or F5), that's because of turbolinks.

我怎样才能让它以最简单的方式工作?

How can i make it work the easiest way?

<script>
  function initialize() {
    var myLatlng = new google.maps.LatLng(<%= place.latitude %>, <%= place.longitude %>); 
    var mapOptions = {
      zoom: 16,
      center: myLatlng
    };
    var map = new google.maps.Map(
      document.getElementById("map-canvas-<%= place.id %>"),
      mapOptions);
    var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: '<%= place.title %>'
    });
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>

这就是剧本.每个地图的 div 如下所示:

that's the script. And div with each map looks like this:

<div id="map-canvas-<%= place.id %>"></div>

推荐答案

您需要通过 turbolinks 初始化脚本.查看这个ASCIIcast关于turbolinks.

You need to initialize scripts via turbolinks. Check this ASCIIcast about turbolinks.

一般来说,你需要改变...

In general, you need to change...

jQuery(function() {
  # your script
});

...到...

var ready;
ready = function() {
  # your script
};

$(document).ready(ready);
$(document).on('page:load', ready);

您可以在此处找到所有涡轮链接事件.

All turbo links events you can find here.

page:load 已更改为 turbolinks:load,因此您要编写:

page:load has changed to turbolinks:load, so instead you want to write:

var ready;
ready = function() {
  # your script
};

$(document).on('turbolinks:load', ready);

您不再需要指定 $(document).ready();,因为 turbolinks:load 会这样做.如需更多信息,请查看自述文件.

You no longer need to specify $(document).ready(); as turbolinks:load does that. For more info, check out the README.

这篇关于带涡轮连杆和窗口负载的导轨 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆