Coffeescript - InvalidValueError:initMap不是函数

时间:2016-06-30 13:03:50

标签: javascript ruby-on-rails-4 coffeescript

我对咖啡脚本很新,我想用它来加载谷歌地图。 但是,它始终不断产生错误;

InvalidValueError: initMap is not a function

coffeescript代码看起来像这样;

$.ajax '/locations',
        type: 'GET'
        dataType: 'json'
        error: (jqXHR, textStatus, errorThrown) ->
            alert "error"
        success: (data, textStatus, jqXHR) ->
            initMap = ->
              map = new google.maps.Map(document.getElementById('map'),
                center:
                  lat: 0.324680
                  lng: 32.572633
                zoom: 10
              )

loadScript = ->
  script = document.createElement("script")
  script.type = "text/javascript"
  script.src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyDiX1sI3oEJBncfz3eFh4AV5zX9f9LGibo&callback=initMap"
  document.body.appendChild script
  return

#
# $(window).load ->

loadScript()

从下面的建议中,尝试了这个;

$.ajax '/locations',
        type: 'GET'
        dataType: 'json'
        error: (jqXHR, textStatus, errorThrown) ->
            alert "error"
        success: (data, textStatus, jqXHR) ->
            @buses = data

initMap = ->
  map = new google.maps.Map(document.getElementById('map'),
    center:
      lat: 0.324680
      lng: 32.572633
    zoom: 10
  )

loadScript = ->
  script = document.createElement("script")
  script.type = "text/javascript"
  script.src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyDiX1sI3oEJBncfz3eFh4AV5zX9f9LGibo&callback=initMap"
  document.body.appendChild script
  return

loadScript()

 # $(window).load ->

0 个答案:

没有答案