LayerList load event in 4.3?

762
3
06-26-2017 01:46 PM
JimNoel
New Contributor III

I would like to run some code after a LayerList has loaded, in version 4.3.  In 3.2, there is a load event, however 4.3 doesn't seem to have this.  LayerList doesn't seem to implement promises, so .then() is not available.  And I couldn't find a property I can watch.  Is there a way to do this?

3 Replies
UndralBatsukh
Esri Regular Contributor

There is no load event for LayerList widget. You can wait for the view to be loaded by calling view.then. 

0 Kudos
JimNoel
New Contributor III

Thank you, Undral.  Actually, I had already tried using view.then.  Unfortunately, the LayerList hasn't actually loaded yet at the time of the view load event, or at least it hasn't updated with all the layer info.

For some context, what I'm trying to do is to have all the nodes of the LayerList expanded at startup.  I'm using the following function to do this:

  function layerList_ExpandAll(expand) {
    var ctSpans = document.getElementsByClassName("esri-layer-list__child-toggle");
      for (var i = 0; i < ctSpans.length; i++)
          ctSpans[i]["data-item"].open = expand;
  }

When the LayerList is fully loaded, there will be at least one item in ctSpans.  However, when called in view.then, these elements haven't been created yet, so the function doesn't work.

As a temporary fix, I'm applying the function on extent changes.  This works fine, but the user has to pan or zoom before the LayerList expands.

.

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Jim,

   Instead of the extent change you should use the layer view updating property:

//lyr is a specific layer in your map
view.whenLayerView(lyr).then(function(lyrView){
  watchUtils.whenFalseOnce(lyrView, "updating").then(function(){
    //now your layer is done drawing oin the view for the first time and your LayerList
    //should be ready to go by this point.
  });
});
0 Kudos