Popup Action calling function multiple times when pressed once

1307
2
Jump to solution
06-26-2018 10:12 AM
CarlosAlvarado
New Contributor

Dear ArcGIS community,

Our team is working with the  ArcGIS JavasScript API, we have implemented a popup window, that we are using to cycle through different versions of a polygon.

The problem that we are facing, is that the linked action is called multiple times when clicking the button once on the popup, and it only happens when using a popup.

If we call the function by itself it doesn't have that problem. We tried to use event.stopPropagation() on the event, but it doesn't have that method.

Here is the function that we use to show our polygon.

function prevSpot(id) {

        actualIndex = spots[id].index;

 

        if (actualIndex - 1 > -1) {

            prevIndex = actualIndex - 1;

            p = spots[id].list[prevIndex];

            temp.removeAll();

            temp.add(p);

            spots[id].index = prevIndex;

        }

    }

prevSpotAction = {

                                title: "Previous Spot",

                                id: "prevSpot"

                            };

                            view.popup.on("trigger-action", function (event) {

                                if (event.action.id === "prevSpot")

                                {

                                    prevSpot(spotId);                                   

                                }

                            });

 

                            var popUp =

                                {

                                    title: "Spot: " + String(name),

                                    content: "<div style='background-color:DarkGray;color:white'> Events: " + cntEvt + " events.</div>" +

                                        "<div style='background-color:White;color:DarkGray'> First Seen: " + fstSeen + "</div>" +

                                        "<div style='background-color:DarkGray;color:white'> Last Seen: " + lstSeen + "</div>" +

                                        "<div style='background-color:White;color:DarkGray'> Evolutions: " + n + "</div>",

                                    actions: [prevSpotAction, nextSpotAction]

                                };

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Usually it's because the event handler is being added multiple times. It's difficult to determine this from your code fragment, but is this handler code located inside a loop?

view.popup.on("trigger-action", function (event) {
  if (event.action.id === "prevSpot") {
    prevSpot(spotId);                                   
  }
});

View solution in original post

2 Replies
KenBuja
MVP Esteemed Contributor

Usually it's because the event handler is being added multiple times. It's difficult to determine this from your code fragment, but is this handler code located inside a loop?

view.popup.on("trigger-action", function (event) {
  if (event.action.id === "prevSpot") {
    prevSpot(spotId);                                   
  }
});
CarlosAlvarado
New Contributor

Hi Ken,

You are absolutely correct, the method was being set inside a $each loop.

Thank you!

Best

Carlos.

0 Kudos