str*_*xel 2 javascript google-maps asynchronous coffeescript
我正在尝试使用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()
Run Code Online (Sandbox Code Playgroud)
编译为:
(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);
Run Code Online (Sandbox Code Playgroud)
然后我得到错误:
Uncaught TypeError: Object [object global] has no method 'initialize'
我知道我可能需要使该initialize()方法可以访问该文档的范围,但由于coffeescript将所有模块包装在匿名函数中,这使得这项工作的最佳方法是什么?