AnsweredAssumed Answered

Custom widget variable scope

Question asked by nart.tamash on Apr 18, 2016
Latest reply on Apr 19, 2016 by rscheitlin

Hi all,

 

Not a seasoned developer, so this might have a very simple answer that I'm failing to see/understand.

 

 

I'm trying to build a custom widget from scratch, and most of my code gets executed when a button is clicked in the widget interface (it's a portal map services search). I am using a global variable that needs to be manipulated at various stages (i.e. events). To keep this as short as possible my code looks something like this:

...

addedLayers: [], //This is the global variable

postCreate: ...

startup: ...

 

 

customFunctionOnButtOnclick: function() {

 

map = this.map;

var addedLayers = this.addedLayers;

...

       on(map, "layer-remove", function(event) {

            //do something to addedLayers

       });

       

       on(map, "layer-add", function(event) {

            //do something else to addedLayers

       });

},

...

 

Is there a reason why addedLayers can be accessed in the layer-add event but not in the layer-remove event?

 

I obviously only included the relevant parts of that main function. There are other bits that do different things, but none of which should affect addedLayers. Also the two map events, are in the same succession as presented here.

Outcomes