4
4
4itosik2015-06-19 12:23:33
ruby
4itosik, 2015-06-19 12:23:33

Rails 4 + turbolins + yandex map, How to make the map load from turbolinks too?

Actually the question is in the title, now the map works if you reload the page, I tried to use:

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

but then 2 cards are loaded (
var ready = ymaps.ready(init);
var myMap, myPlacemark;
function init(){
var myGeocoder = ymaps.geocode('#{@user.full_address}');
myGeocoder.then(
function (res) {
var coords = res.geoObjects.get(0).geometry.getCoordinates();
myPlacemark = new ymaps.Placemark(coords, {
hintContent: '#{@user.show_nickname}',
balloonContent : '#{@user.show_nickname}; Phone: #{@user.show_phone};'
});
myMap = new ymaps.Map("map", {
center: coords,
zoom: 15
});
myMap.geoObjects. add(myPlacemark);
});
}

Here is the code that initializes the map

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
elikru, 2015-12-07
@4itosik

ymaps.coffee:

window.ya_init = ->
  $('.ymap').not('.done').addClass('done').each ->
    $m = $(this)
    ymaps.geocode($m.data('address'), {results: 1}).then (res) ->
      firstGeoObject = res.geoObjects.get(0)
      coords = firstGeoObject.geometry.getCoordinates()
      map = new ymaps.Map($m[0],
        center: coords
        zoom: 15,
        behaviors: ['default'] #, 'scrollZoom']
      )
      map.controls.add('zoomControl', { left: 5, top: 5 })
      myPlacemark = new ymaps.Placemark(coords)
      map.geoObjects.add(myPlacemark)

$ ->
  return if $('.ymap').not('.done').length == 0
  if window.ymaps
    ya_init()
  else
    $.getScript( "//api-maps.yandex.ru/2.1/?load=package.full&lang=ru-RU&onload=ya_init")

somepage.html.slim:
Peeped in rocket_cms

J
Jeiwan, 2015-06-19
@Jeiwan

Throw out the fuck turbolinks. Premature optimization is evil.

E
Evgeny G, 2018-09-11
@gorborukov

Hello from 2018
after the map initialization code:

document.addEventListener("turbolinks:before-cache", function() {
    myMap.destroy();
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question