Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One slight gotcha to be aware of is that the object system in OpenLayers uses a function called initialize() as the constructor, so in order to have CoffeeScript's super keyword work correctly when you extend OpenLayers classes, you'll need to decorate them. I use the following function for this:</p> <pre><code>window.CompatibleClass = (cls) -&gt; class Wrapped constructor: -&gt; # Call the OpenLayers-style constructor. cls::initialize.apply @, arguments # Copy prototype elements from OpenLayers class. Wrapped::[name] = el for name, el of cls:: Wrapped </code></pre> <p>Now you can extend an OL built-in like so:</p> <pre><code>class MySpecialFeature extends (CompatibleClass OpenLayers.Feature.Vector) constructor: -&gt; super new OpenLayers.Geometry.Point 0, 0 CLASS_NAME: "MySpecialFeature" </code></pre> <hr> <p>EDIT: Just to clarify, the two alternatives to wrapping the class like this are to use the OpenLayers class system as-is, and miss out on some of the syntax benefits of CoffeeScript, or to manually call the initialize function in each constructor, which feels more brittle, and spreads around the dependency, rather than concentrating it in a single decorator.</p> <p>Using the OpenLayers class system as-is, in CoffeeScript:</p> <pre><code>MySpecialFeature = OpenLayers.Class OpenLayers.Feature.Vector, initialize: -&gt; # Call super using apply, as is convention in OpenLayers OpenLayers.Feature::initialize.apply @, new OpenLayers.Geometry.Point 0, 0 ... ... </code></pre> <p>Alternatively, using CoffeeScript classes, but extending an OpenLayers class un-decorated:</p> <pre><code>class MySpecialFeature extends OpenLayers.Feature.Vector constructor: -&gt; # Call inherited initialize(). @initialize.apply @, new OpenLayers.Geometry.Point 0, 0 ... ... </code></pre> <p>Neither of these methods will be as idiomatic or recognizable to other developers, either of OpenLayers or CoffeeScript. I stand by my recommendation of a wrapper which allows native super() use in calling OpenLayers constructors.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload