【问题标题】:Google Maps: asynchronous loading with coffeescript谷歌地图:使用咖啡脚本进行异步加载
【发布时间】: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


    【解决方案1】:
    window.initialize = ->
      # ...
    

    附:考虑给它一个更独特的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多