view pop up watcher skips first click

905
2
Jump to solution
10-23-2020 01:41 PM
RyanBohan
Occasional Contributor III

I have a script to grab the pop-up information when opened from the search bar. Only I get null results for the first opened pop-up and it's driving me crazy.

The second time a pop up everything works as expected.  I just can't capture the first pop up to open.


If there is a better way to know which pop up is opened, I would love to know.

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <title>Search Widget | Sample | ArcGIS API for JavaScript 4.16</title>

    <style>
        html,
        body,
        #viewDiv {
            padding0;
            margin0;
            height100%;
            width100%;
        }
    </style>

    <link rel="stylesheet" href="https://js.arcgis.com/4.16/esri/themes/light/main.css" />
    <script src="https://js.arcgis.com/4.16/"></script>

    <script>
        require([
            "esri/Map",
            "esri/views/SceneView",
            "esri/widgets/Search"
        ], function (MapSceneViewSearch) {
            var map = new Map({
                basemap: "satellite",
                ground: "world-elevation"
            });

            var view = new SceneView({
                scale: 123456789,
                container: "viewDiv",
                map: map
            });

            var searchWidget = new Search({
                view: view
            });

            
            //a watcher for when the pop up fires
            view.popup.watch("visible"function (popUpStatusChange) {
                if (popUpStatusChange == true) {
                    console.log('Pop-up watch has been fired')
                    console.log("Pop-up title is:"view.popup.title);  //returns the pop up title
                    console.log("Pop-up content is:"view.popup.content);  //returns the pop up content
                }
            });
            view.ui.add(searchWidget, {
                position: "top-right"
            });
        });

    </script>
</head>

<body>
    <div id="viewDiv"></div>
</body>

</html>
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

If you put in a timeout, then it does work properly on the first popup

view.popup.watch("visible", function (popUpStatusChange) {
  setTimeout(function(){   
    if (popUpStatusChange == true) {
      console.log('Pop-up watch has been fired')
      console.log("Pop-up title is:", view.popup.title);  //returns the pop up title
      console.log("Pop-up content is:", view.popup.content);  //returns the pop up content
    }
  }, 100); 
});‍‍‍‍‍‍‍‍‍

View solution in original post

2 Replies
KenBuja
MVP Esteemed Contributor

If you put in a timeout, then it does work properly on the first popup

view.popup.watch("visible", function (popUpStatusChange) {
  setTimeout(function(){   
    if (popUpStatusChange == true) {
      console.log('Pop-up watch has been fired')
      console.log("Pop-up title is:", view.popup.title);  //returns the pop up title
      console.log("Pop-up content is:", view.popup.content);  //returns the pop up content
    }
  }, 100); 
});‍‍‍‍‍‍‍‍‍
RyanBohan
Occasional Contributor III

Thank you Ken Buja‌ I increased the timeout to 1000 and it works! 

0 Kudos