JavaScript API Scope from Web AppBuilder Widget.js

841
1
10-03-2017 03:12 PM
ChadStidham1
New Contributor

I'm new to customizing widgets and haven't been able to access some methods that are specific to the JavaScript API that according to a development summit session should be accessible.

I'm working on a custom widget that was created from the CustomWidget template directory.  For learning purposes I'm trying to simply show or hide a layer using the snippet below that was included in a developer summit session:

this.projectsLayer = this.map.getLayer('LayerName');

this.projectsLayer.show();

This is contained within the onOpen attribute / event.

onOpen: function(){
this.inherited(arguments);
console.log('onOpen');
this.projectsLayer = this.map.getLayer('LayerName');
this.projectsLayer.show();

},

In the developer summit session it was stated that since this widget is inheriting from BaseWidget it should have access to the map instance.  When I run this application in Chrome the developer tools console shows the error: TypeError: Cannot read property 'show' of undefined.  this.map in the console displays the HTML of the map.  Trying this.map.getBasemap() or this.map.getLayersVisibleAtScale() both trigger TypeError: this.map.getBasemap or  this.map.getLayersVisibleAtScale is not a function.

I'm unsure if these methods are outside the scope of BaseWidget or if I am somehow missing a reference.

The beginning of Widget.js is unchanged:

define(['dojo/_base/declare', 'jimu/BaseWidget'],
function(declare, BaseWidget) {

Any help with this is appreciated.

0 Kudos
1 Reply
RobertScheitlin__GISP
MVP Emeritus

Chad,

  When I put this in a widgets code it works fine for me:

      onOpen: function () {
        var visLyrs = this.map.getLayersVisibleAtScale();
        console.info(visLyrs);
        ...
      }

Can you share your whole Widget.js?

0 Kudos