$.getScript
获取脚本
异步
. 你不会在打电话之前等待结果
initMap
在这种情况下
page.included_google_maps_js_api == undefined
是真的。
你只需要一个
else
(因为您正在使用
&callback=initMap
在需要加载的情况下调用):
$(document).on 'turbolinks:load', ->
if $('#restaurant-map').length > 0
if page.included_google_maps_js_api == undefined
google_maps_api_key = 'xxx'
# correctly called from here...
$.getScript('https://maps.googleapis.com/maps/api/js?key=' + google_maps_api_key + '&callback=initMap')
page.included_google_maps_js_api = true
else # <== Note the else
initMap() # so we only do this if it's loaded