Hi @NilsBabel1, since we cannot prevent the "listItemCreatedFunction" handler from being called over and over, my only thought was to have it exit if it's already been called for the current layer.
So my example points to a function (ex: "layerListActions") when defining the "new LayerList":
listItemCreatedFunction: layerListActions
This function is called whenever a list item is created, whether it's the 1st...18th...or 999th time for that same layer. If it's the first time, the layer's (unique) title is added to this array to essentially check it off the list. If we find the layer's title is already added to the array [ arrListItemCreated.indexOf(event.item.title) > -1 ] then we exit the handler completely. Basically don't do anything and move on, since it's already had some code applied.
Here's my full function, if it means anything more. I'm just checking for a certain layer to know if it's turned on (visible). If so, I'm programatically turning on a hidden feature layer. Not a typical case for most people, but it was necessary because I'm pulling in data on the fly from another source.
Anyway, hope this explains the use of the array and my logic.
function layerListActions(event) {
if (arrListItemCreated.indexOf(event.item.title) > -1) {
return false;
} else {
arrListItemCreated.push(event.item.title);
}
if (event.item && event.item.layer) {
const item = event.item;
const layer = item.layer;
item.open = item.visible;
let lyrVEPByExerciseDate = app.Functions.getLayerFromTitle(app.mapImgLyrUtilMap, "VEP by Exercised Date");
if (!uniqueParentItems.indexOf(item.title) > -1) {
uniqueParentItems.push(item.title);
item.watch("visible", function (event) {
if (app.dynamicFeatLyrVEP && (item.title === "VEP by Exercised Date" || item.title === "Valve Exercise Program")) {
if (event == true) {
if (lyrVEPByExerciseDate.visible == true) {
app.dynamicFeatLyrVEP.visible = true;
}
} else {
app.dynamicFeatLyrVEP.visible = false;
}
}
});
}
}
}