Javascript 在CoffeeScript中调用全局函数
CoffeeScript有一个奇怪的行为:从加载的脚本中正确调用了Javascript 在CoffeeScript中调用全局函数,javascript,coffeescript,Javascript,Coffeescript,CoffeeScript有一个奇怪的行为:从加载的脚本中正确调用了initMap函数(&callback=initMap),但我在initMap()的最后一行触发了一个错误 我觉得有趣的是,另一个片段工作完美: $(document).on 'turbolinks:load', -> if $('#restaurant-map').length > 0 && page.included_google_maps_js_api == undefined goo
initMap
函数(&callback=initMap
),但我在initMap()的最后一行触发了一个错误
我觉得有趣的是,另一个片段工作完美:
$(document).on 'turbolinks:load', ->
if $('#restaurant-map').length > 0 && page.included_google_maps_js_api == undefined
google_maps_api_key = 'xxx'
$.getScript('https://maps.googleapis.com/maps/api/js?key=' + google_maps_api_key + '&callback=initMap')
page.included_google_maps_js_api = true
else if ($('#restaurant-map').length > 0)
initMap()
$.getScript
异步获取脚本。如果page.included\u google\u maps\u js\u api==undefined
为真,则在调用initMap
之前,您不必等待结果
您只需要一个else
(因为您正在使用&callback=initMap
在需要加载的情况下调用它):
$(文档)。在“TurboLink:load”上,->
如果$(“#餐厅地图”)。长度>0
如果page.included\u google\u maps\u js\u api==未定义
谷歌地图api密钥='xxx'
#从这里正确调用。。。
$.getScript('https://maps.googleapis.com/maps/api/js?key=“+google\u-maps\u-api\u-key+”&callback=initMap')
page.included_谷歌地图_js_api=true
否则#
$(document).on 'turbolinks:load', ->
if $('#restaurant-map').length > 0 && page.included_google_maps_js_api == undefined
google_maps_api_key = 'xxx'
$.getScript('https://maps.googleapis.com/maps/api/js?key=' + google_maps_api_key + '&callback=initMap')
page.included_google_maps_js_api = true
else if ($('#restaurant-map').length > 0)
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