我正在尝试模拟这个 asynchronously-loaded map使用 CoffeeScript 。
这是我的 CoffeeScript :
initialize = ->
mapOptions =
zoom: 8
center: new google.maps.LatLng(-34.397, 150.644)
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions)
return
loadScript = ->
script = document.createElement("script")
script.type = "text/javascript"
script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&" + "callback=initialize"
document.body.appendChild script
return
$(window).load ->
loadScript()
编译为:
(function() {
var initialize, loadScript;
initialize = function() {
var map, mapOptions;
mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644)
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
};
loadScript = function() {
var script;
script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&" + "callback=initialize";
document.body.appendChild(script);
};
$(window).load(function() {
return loadScript();
});
}).call(this);
然后我得到错误:
Uncaught TypeError: Object [object global] 没有方法 'initialize'
我知道我可能需要使 initialize()
方法可访问文档的范围,但由于 coffeescript 将所有模块包装在匿名函数中,因此实现此功能的最佳方法是什么?
最佳答案
window.initialize = ->
# ...
附言考虑给它起一个更独特的名称。
关于javascript - 谷歌地图 : asynchronous loading with coffeescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22153242/