【发布时间】:2014-04-04 21:12:22
【问题描述】:
我正在尝试使用咖啡脚本来模拟这个asynchronously-loaded map。
这是我的咖啡脚本:
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] has no method 'initialize'
我知道我可能需要使initialize() 方法可以访问文档的范围,但是由于coffeescript 将所有模块包装在匿名函数中,使这项工作的最佳方法是什么?
【问题讨论】:
标签: javascript google-maps asynchronous coffeescript