LayerInfos events

643
4
01-17-2018 01:11 PM
AlfonsoYañez_Morillo
New Contributor III

Hello,

I'm trying to update the layers available for a custom widget. The layers needed are those in the layerList checked and I would like to update the information every  time the user check or uncheck a layer. I tried to use the LayerInfosObject and catch the layerInfosIsVisibleChanged or the layerInfosIsShowInMapChanged events, however I've observed a very weir behavior.

I have to types of layers, those are in the webmap and those uploaded with the Add Data widget. For the layers included in the webmap it's fine, when I check or uncheck the layer an event is trigger.  The problem raise with the other layers where the number of events depends on the order when the layer was uploaded.

For example, the Webmap has two layers (A and B) and I added an three extra layers (C, D and E, in that order). When I update the visibility in the LayerList for the layer A or B, I can observe only one event. The same occurs with the layer C which was the first loaded. For the layer D, two events are triggered and three for the layer E.

Here is the code I use to test the event:

    startup: function() {
      this.inherited(arguments);
      console.log('Demo::startup');

      this.counter = 0;
      LayerInfos.getInstance(this.map, this.map.itemInfo).then(lang.hitch(this, function(layerInfosObject){
        this.layerInfosObject = layerInfosObject;
        layerInfosObject.getLayerInfoArray().forEach(function(layerInfo) {
            console.log(layerInfo.title, layerInfo.id);
        });
        this.own(on(this.layerInfosObject, 'layerInfosIsVisibleChanged',
          lang.hitch(this, function(){
              this.counter += 1;
              console.log(this.counter);
          })));
      }));
    },

Is that normal?

Thank you,

Alfonso

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Alfonso,

   Sounds like the AddData widget is adding the "layerInfosIsVisibleChanged" event listener to each layer each time a layer is added, thus multiple events are being fired for each added layer.

AlfonsoYañez_Morillo
New Contributor III

Thanks Robert,

It is what I guessed, that there would be other listeners from somewhere . Fortunately I could manage this issue, but I wonder if there is a way to discriminate the event from its listener, I mean, only catch the event triggered from an specific listener.

Alfonso

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Have you looked at the contents of the event object that is passed to the listener function? It should have the layer in it.

0 Kudos
AlfonsoYañez_Morillo
New Contributor III

Yes, I have. The layers are in them, and they all are equal. What I did is to check some conditions to catch the first one and do nothing with the rest of the events.