view.popup.watch being fired twice

2574
11
Jump to solution
06-07-2017 02:45 PM
EvonFranklin
New Contributor III

I am attempting to display my own custom popup when a user clicks on the MapImageLayer with the following code:

view.popup.watch("visible", function(visible) {
                view.popup.visible = false;
                if(visible){
                   var attr = view.popup.features[0].attributes;
                   $('.popup-table-container').remove();
                   var newPopup = formatPopup(attr);
                   $('body').append(newPopup);
                   $(newPopup).css({top: ($(window).height()/2)+'px',left: ($(window).width()/2)+'px'});
                }
            });‍‍‍‍‍‍‍‍‍‍

I try to close the default popup in the first line then second line I actually populate my own. I would like to stop this event from being fired twice with a single click, as a query is made twice which slows down the popup processing.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Almost:

watchUtils.whenTrue(view.popup, "visible", function() {

View solution in original post

11 Replies
RobertScheitlin__GISP
MVP Emeritus

Evon,

   You should use watchUtils and whenTrue:

watchUtils | API Reference | ArcGIS API for JavaScript 4.3 

That way you do not get your event handler fired when you set the popup visibility to false.

0 Kudos
EvonFranklin
New Contributor III

Thanks for the suggestion, I think this would work however I do want to watch for a popup not really an event per say

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

??? The method I suggested is no different from what you are doing but only looking for a true property instead of true or false.

0 Kudos
EvonFranklin
New Contributor III

Right I misunderstood there a bit lol, well is the whenTrue() available from "view.popup" ?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Evon,

   No there is not, that is the reason for a watchUtils class.

0 Kudos
EvonFranklin
New Contributor III

What action would I watch for in watchutils ?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

You would watch for the open property just like you were in your original code.

0 Kudos
EvonFranklin
New Contributor III

Oh I see now, like this ,

watchUtils.whenTrue(view, "visible", function() {

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Almost:

watchUtils.whenTrue(view.popup, "visible", function() {