简体   繁体   English

如何使用coffeescript扩展google.maps.Map

[英]How can I extend google.maps.Map with coffeescript

I have tried standard way: 我已经尝试过标准方式:

class MyMap extends google.maps.Map
  constructor: (mapDiv, opts)->
    super(mapDiv, opts)

But in this case placeholder was empty. 但是在这种情况下,占位符为空。

This problem is a combo of the way CoffeeScript makes classes and the way that the Google Maps Javascript API is written/obfuscated/minified. 此问题是CoffeeScript制作类的方式与Google Maps Javascript API的编写/混淆/最小化方式的组合。

When CoffeeScript extends a class, it creates code something similar to this: CoffeeScript扩展类时,将创建类似于以下内容的代码:

customnamespace.CustomMap = (function(_super) {

    // '__extends' is a method that gets output at the 
    // top of every CoffeeScript compiled file
    __extends(CustomMap, _super);

    function CustomMap(mapDiv, opts) {
        CustomMap.__super__.constructor.apply(this, arguments);
    }

    return CustomMap;

})(google.maps.Map);

In most cases, and especially in cases where the "extendee" was written in CoffeeScript, this works great. 在大多数情况下,尤其是在用CoffeeScript编写“扩展名”的情况下,这非常有用。

But in the case of google.map.Maps there is (I suspect) a whole bunch of scope manipulation going on and it's kindof undoing the scope CoffeeScript tries to set. 但是在google.map.Maps的情况下(我怀疑)正在进行大范围的范围操作,这有点是撤销CoffeeScript尝试设置的范围。 Admittedly this is a guess. 诚然,这是一个猜测。

So in this case it's time to put on ye olde JavaScript hat and just do some plain old scope-locking on the constructor. 因此,在这种情况下,是时候戴上旧的JavaScript帽子,并在构造函数上执行一些简单的旧范围锁定了。 So drop your super and apply the function in the scope of the class with a line of JavaScript. 因此,删除您的superapply一行JavaScript将函数apply到类的范围中。 CoffeeScript will just wave, smile, and output the JavaScript line as is. CoffeeScript只会挥舞,微笑并按原样输出JavaScript行。

class MyMap extends google.maps.Map
    constructor: (mapDiv, opts)->
        google.maps.Map.apply(this, [mapDiv, opts]);

Make sense? 说得通?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM