<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: unbinding eventhandler from mapview in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unbinding-eventhandler-from-mapview/m-p/1216675#M78839</link>
    <description>&lt;P&gt;Hi &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/7384"&gt;@ReneRubalcava&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;I've created this pen to visualize the problem: as long as the checkbox is checked I would like to log the coordinates which are clicked on the map. If I disable the checkbox I would like to stop the eventhandler.&lt;/P&gt;&lt;P&gt;&lt;A href="https://codepen.io/butcher/pen/oNdpmML" target="_blank"&gt;https://codepen.io/butcher/pen/oNdpmML&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Here is the code without the pen:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;meta
      name="viewport"
      content="initial-scale=1,maximum-scale=1,user-scalable=no"
    /&amp;gt;
    &amp;lt;title&amp;gt;
      Intro to MapView - Create a 2D map | Sample | ArcGIS API for JavaScript
      4.24
    &amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    &amp;lt;/style&amp;gt;

    &amp;lt;link
      rel="stylesheet"
      href="https://js.arcgis.com/4.24/esri/themes/light/main.css"
    /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.24/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script&amp;gt;
      require(["esri/Map", "esri/views/MapView"], (Map, MapView) =&amp;gt; {
        const map = new Map({
          basemap: "topo-vector"
        });

        const view = new MapView({
          container: "viewDiv",
          map: map,
          zoom: 4,
          center: [15, 65] // longitude, latitude
        });
        console.log(document.getElementById("check").checked)
        // $('#check').on('click',myFunction);
        var checkbox = document.getElementById("check");
        document.getElementById('check').onclick = function(){
          myChange();
        };
        function myChange(){
        if (document.getElementById("check").checked){
            var handler = view.on('click', (event) =&amp;gt; {
            const point = `point: (${event.mapPoint.latitude}, ${event.mapPoint.longitude})`;
         console.log(point)
              handler &amp;amp;&amp;amp; handler.remove();
        });
         
        
      } else {
        handler?.remove();
      }
        }
        
      });
      
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    &amp;lt;div&amp;gt;&amp;lt;input type="checkbox" id="check"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 27 Sep 2022 18:56:23 GMT</pubDate>
    <dc:creator>RiccardoKlinger</dc:creator>
    <dc:date>2022-09-27T18:56:23Z</dc:date>
    <item>
      <title>unbinding eventhandler from mapview</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unbinding-eventhandler-from-mapview/m-p/1216641#M78834</link>
      <description>&lt;DIV class=""&gt;&lt;P&gt;As I am working with arcgis JS API 4.24. I want to bind an eventhandler to the view if a user clicks on the map:&lt;/P&gt;&lt;PRE&gt;mapStore.&lt;SPAN class=""&gt;mapView&lt;/SPAN&gt;?.&lt;SPAN class=""&gt;on&lt;/SPAN&gt;(&lt;SPAN class=""&gt;'click'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;(&lt;SPAN class=""&gt;event&lt;/SPAN&gt;) =&amp;gt;&lt;/SPAN&gt; {
            &lt;SPAN class=""&gt;const&lt;/SPAN&gt; point = &lt;SPAN class=""&gt;`point: (&lt;SPAN class=""&gt;${event.mapPoint.latitude}&lt;/SPAN&gt;, &lt;SPAN class=""&gt;${event.mapPoint.longitude}&lt;/SPAN&gt;)`&lt;/SPAN&gt;;
            &lt;SPAN class=""&gt;console&lt;/SPAN&gt;.&lt;SPAN class=""&gt;log&lt;/SPAN&gt;(&lt;SPAN class=""&gt;'click event1: '&lt;/SPAN&gt;, point);
            }&lt;/PRE&gt;&lt;P&gt;This eventhandler shall stay active as long as a condition is true. If the condition is false I would like to remove exactly this handler and I would like to only use the standard view on-click handler. I can disable the eventhandle exactly during its execution but I want to leave it active for the duration of the condition... the ad-hoc removal would look like:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;export&lt;/SPAN&gt; &lt;SPAN class=""&gt;const&lt;/SPAN&gt; clickhandler = (&lt;SPAN class=""&gt;active&lt;/SPAN&gt;: boolean): &lt;SPAN class=""&gt;&lt;SPAN class=""&gt;void&lt;/SPAN&gt; =&amp;gt;&lt;/SPAN&gt; {
    &lt;SPAN class=""&gt;const&lt;/SPAN&gt; { mapStore } = &lt;SPAN class=""&gt;Stores&lt;/SPAN&gt;.&lt;SPAN class=""&gt;getInstance&lt;/SPAN&gt;();
    &lt;SPAN class=""&gt;if&lt;/SPAN&gt; (active) {
        &lt;SPAN class=""&gt;const&lt;/SPAN&gt; handler = mapStore.&lt;SPAN class=""&gt;mapView&lt;/SPAN&gt;?.&lt;SPAN class=""&gt;on&lt;/SPAN&gt;(&lt;SPAN class=""&gt;'click'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;(&lt;SPAN class=""&gt;event&lt;/SPAN&gt;) =&amp;gt;&lt;/SPAN&gt; {
            &lt;SPAN class=""&gt;const&lt;/SPAN&gt; point = &lt;SPAN class=""&gt;`point: (&lt;SPAN class=""&gt;${event.mapPoint.latitude}&lt;/SPAN&gt;, &lt;SPAN class=""&gt;${event.mapPoint.longitude}&lt;/SPAN&gt;)`&lt;/SPAN&gt;;
            &lt;SPAN class=""&gt;console&lt;/SPAN&gt;.&lt;SPAN class=""&gt;log&lt;/SPAN&gt;(&lt;SPAN class=""&gt;'click event1: '&lt;/SPAN&gt;, point);
            handler?.&lt;SPAN class=""&gt;remove&lt;/SPAN&gt;();
        });
    } &lt;SPAN class=""&gt;else&lt;/SPAN&gt; {
        &lt;SPAN class=""&gt;console&lt;/SPAN&gt;.&lt;SPAN class=""&gt;log&lt;/SPAN&gt;(&lt;SPAN class=""&gt;'click event1: '&lt;/SPAN&gt;, &lt;SPAN class=""&gt;'disabled'&lt;/SPAN&gt;);
    }
};&lt;/PRE&gt;&lt;P&gt;I've seen some examples where they work with dojo/connect in arcgis JS API 3.x... Is there an equivalent in ArcGIS JS API 4?&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 27 Sep 2022 18:02:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unbinding-eventhandler-from-mapview/m-p/1216641#M78834</guid>
      <dc:creator>RiccardoKlinger</dc:creator>
      <dc:date>2022-09-27T18:02:20Z</dc:date>
    </item>
    <item>
      <title>Re: unbinding eventhandler from mapview</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unbinding-eventhandler-from-mapview/m-p/1216646#M78837</link>
      <description>&lt;P&gt;The ".on()" methods in the API return a Handler with a remove method you can use to remove the listener.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;let handler;

handler = view.on("click", () =&amp;gt; {
    console.log("click");
    handler &amp;amp;&amp;amp; handler.remove();
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRIKE&gt;Looks like something happened to the doc on this. I'll pass it on, thanks!&amp;nbsp;&lt;/STRIKE&gt;&lt;/P&gt;&lt;P&gt;Looks like it's not a specific type, it's just an Object with a remove() method as described here.&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#on" target="_blank"&gt;https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#on&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Sep 2022 18:11:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unbinding-eventhandler-from-mapview/m-p/1216646#M78837</guid>
      <dc:creator>ReneRubalcava</dc:creator>
      <dc:date>2022-09-27T18:11:48Z</dc:date>
    </item>
    <item>
      <title>Re: unbinding eventhandler from mapview</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unbinding-eventhandler-from-mapview/m-p/1216675#M78839</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/7384"&gt;@ReneRubalcava&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;I've created this pen to visualize the problem: as long as the checkbox is checked I would like to log the coordinates which are clicked on the map. If I disable the checkbox I would like to stop the eventhandler.&lt;/P&gt;&lt;P&gt;&lt;A href="https://codepen.io/butcher/pen/oNdpmML" target="_blank"&gt;https://codepen.io/butcher/pen/oNdpmML&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Here is the code without the pen:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;meta
      name="viewport"
      content="initial-scale=1,maximum-scale=1,user-scalable=no"
    /&amp;gt;
    &amp;lt;title&amp;gt;
      Intro to MapView - Create a 2D map | Sample | ArcGIS API for JavaScript
      4.24
    &amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    &amp;lt;/style&amp;gt;

    &amp;lt;link
      rel="stylesheet"
      href="https://js.arcgis.com/4.24/esri/themes/light/main.css"
    /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.24/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script&amp;gt;
      require(["esri/Map", "esri/views/MapView"], (Map, MapView) =&amp;gt; {
        const map = new Map({
          basemap: "topo-vector"
        });

        const view = new MapView({
          container: "viewDiv",
          map: map,
          zoom: 4,
          center: [15, 65] // longitude, latitude
        });
        console.log(document.getElementById("check").checked)
        // $('#check').on('click',myFunction);
        var checkbox = document.getElementById("check");
        document.getElementById('check').onclick = function(){
          myChange();
        };
        function myChange(){
        if (document.getElementById("check").checked){
            var handler = view.on('click', (event) =&amp;gt; {
            const point = `point: (${event.mapPoint.latitude}, ${event.mapPoint.longitude})`;
         console.log(point)
              handler &amp;amp;&amp;amp; handler.remove();
        });
         
        
      } else {
        handler?.remove();
      }
        }
        
      });
      
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    &amp;lt;div&amp;gt;&amp;lt;input type="checkbox" id="check"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 27 Sep 2022 18:56:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unbinding-eventhandler-from-mapview/m-p/1216675#M78839</guid>
      <dc:creator>RiccardoKlinger</dc:creator>
      <dc:date>2022-09-27T18:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: unbinding eventhandler from mapview</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unbinding-eventhandler-from-mapview/m-p/1216735#M78841</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/31752"&gt;@RiccardoKlinger&lt;/a&gt;&amp;nbsp;you're very close. You just need to adjust your handler declaration and how you handle it a bit.&lt;/P&gt;&lt;P&gt;&lt;A href="https://codepen.io/odoe/pen/bGMaJPW?editors=1001" target="_blank"&gt;https://codepen.io/odoe/pen/bGMaJPW?editors=1001&lt;/A&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;let handler;
function myChange() {
  if (document.getElementById("check").checked) {
    // check if null
    if (handler) {
      return;
    }
    handler = view.on("click", (event) =&amp;gt; {
      const point = `point: (${event.mapPoint.latitude}, ${event.mapPoint.longitude})`;
      console.log(point);
    });
  } else {
    // remove and set to null
    handler?.remove();
    handler = null;
  }
}&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 27 Sep 2022 20:36:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unbinding-eventhandler-from-mapview/m-p/1216735#M78841</guid>
      <dc:creator>ReneRubalcava</dc:creator>
      <dc:date>2022-09-27T20:36:43Z</dc:date>
    </item>
    <item>
      <title>Re: unbinding eventhandler from mapview</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unbinding-eventhandler-from-mapview/m-p/1216924#M78855</link>
      <description>&lt;P&gt;thanks for the first candidate. As I am working with react/typescript I needed to add some // &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/69004"&gt;@ts&lt;/a&gt;-ignore lines but it is working like a charme:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;import { Stores } from '../../../core/Stores';

// &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/69004"&gt;@ts&lt;/a&gt;-ignore
let handler;

export const clickHandler= (active: boolean): void =&amp;gt; {
    const { mapStore } = Stores.getInstance();
    
    if (active) {
        // &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/69004"&gt;@ts&lt;/a&gt;-ignore
        if (handler) {
            return;
          }
          handler = mapStore.mapView?.on("click", (event) =&amp;gt; {
            const point = `point: (${event.mapPoint.latitude}, ${event.mapPoint.longitude})`;
            console.log(point);
          });
        } else {
          // &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/69004"&gt;@ts&lt;/a&gt;-ignore
          handler?.remove();
          handler = null;
        }
};&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 28 Sep 2022 14:07:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unbinding-eventhandler-from-mapview/m-p/1216924#M78855</guid>
      <dc:creator>RiccardoKlinger</dc:creator>
      <dc:date>2022-09-28T14:07:45Z</dc:date>
    </item>
  </channel>
</rss>

