How do you return values from javascript functions

Issue

I’m using the js library in Dart to access OpenLayers. Pertinent code looks like:

js.scoped(() {
  ol = js.retain(js.context.OpenLayers);
  var max_extent = new js.Proxy(ol.Bounds, -13652354.432172, 6026153.418145, -13574082.915218, 6065289.1766216);
  var restricted_extent = max_extent;

  if (controls == null){

    var options = js.map ({
      'maxExtent': max_extent,
      'restrictedExtent' : restricted_extent,
      'units' : 'm',
      'projection': new js.Proxy(ol.Projection, 'EPSG:900913'),
      'displayProjection' : new js.Proxy(ol.Projection, 'EPSG:4326'),
      'controls' : js.array([ new js.Proxy(ol.Control.Attribution),
                              new js.Proxy(ol.Control.Navigation),
                              new js.Proxy(ol.Control.ArgParser),
                              new js.Proxy(ol.Control.PanPanel),
                              new js.Proxy(ol.Control.ZoomPanel)
                             ])
    });
    _treemap = js.retain( new js.Proxy( ol.Map, map_div_id, options ) );
  }

  var roads = new MapLayer(ol, layer_type:'road').layer;
  var aerial = new MapLayer(ol, layer_type:'hybrid').layer;

  _treemap.addLayers(js.array([roads, aerial]));
  _treemap.setBaseLayer(roads);
  _treemap.zoomToMaxExtent();
  var result = _treemap.layers();
});

All works as expected except for the last line. _treemap.layers() is supposed to return an array of OpenLayer.Layer. When that line executes I get an error:

Exception: TypeError: Object [object Array] has no method ‘apply’

So, what is the correct way to get/handle return values from javascript functions in my Dart code?

Solution

layers is an array (see OpenLayers.Map.layers). So you should use :

var result = _treemap.layers;

With _treemap.layers() you was trying to call layers as it was a function.

One side note on your code : when you use js.map({}) you don’t need to use js.array or js.map in the object tree. You can simply give a JSON-like structure.

var options = js.map ({
  'maxExtent': max_extent,
  'restrictedExtent' : restricted_extent,
  'units' : 'm',
  'projection': new js.Proxy(ol.Projection, 'EPSG:900913'),
  'displayProjection' : new js.Proxy(ol.Projection, 'EPSG:4326'),
  'controls' : [ new js.Proxy(ol.Control.Attribution),
                 new js.Proxy(ol.Control.Navigation),
                 new js.Proxy(ol.Control.ArgParser),
                 new js.Proxy(ol.Control.PanPanel),
                 new js.Proxy(ol.Control.ZoomPanel)
               ])
});

Answered By – Alexandre Ardhuin

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *