<?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: On binding dijit/Menu to a graphic in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/on-binding-dijit-menu-to-a-graphic/m-p/733759#M67983</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Great job, Ben.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Just a note, you can always call graphic.getNode() to get the raw DOM element, which could be SVG or canvas depending on browsers. Calling getDojoShape() is a good idea since dojo is doing a great job to eliminate the discrepancies among browsers. I totally agree with you, explore GFX and take advantage of it whenever you can. Manipulating DOM element directly is very hard and error-prone.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 26 Nov 2013 04:02:20 GMT</pubDate>
    <dc:creator>JianHuang</dc:creator>
    <dc:date>2013-11-26T04:02:20Z</dc:date>
    <item>
      <title>On binding dijit/Menu to a graphic</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/on-binding-dijit-menu-to-a-graphic/m-p/733757#M67981</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I recently ran into some trouble binding menus to graphics and this is what I learned.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;//create graphic
if (result.geometry.type != 'extent') {
&amp;nbsp; var graphic = new Graphic(result.geometry);
} else {
&amp;nbsp; var graphic = this._extentToPolygon(result.geometry);
}

//add graphic
this.layer.add(graphic);

//graphic menu
graphic.menu = new Menu({
&amp;nbsp; contextMenuForWindow: false,
&amp;nbsp; leftClickToOpen: false
});

//add some menu items
graphic.menu.addChild(new MenuItem({
&amp;nbsp; label: 'Move to Front',
&amp;nbsp; onClick: lang.hitch(this, function() {
&amp;nbsp;&amp;nbsp;&amp;nbsp; graphic.getDojoShape().moveToFront();
&amp;nbsp; })
}));
graphic.menu.addChild(new MenuItem({
&amp;nbsp; label: 'Move to Back',
&amp;nbsp; onClick: lang.hitch(this, function() {
&amp;nbsp;&amp;nbsp;&amp;nbsp; graphic.getDojoShape().moveToBack();
&amp;nbsp; })
}));

//bind the menu to graphics dom node
graphic.menu.bindDomNode(graphic.getDojoShape().getNode());
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I was creating graphics, each with a custom menu based on geometry type and such, then binding the menu to the graphics dom node.&amp;nbsp; The menu was initially bound to graphic's dom node, however any number of actions appear to magically unbind the menu.&amp;nbsp; These include, but are not limited to, panning/zooming the map and editing the graphic's symbol or geometry.&amp;nbsp; Looking for answers led me to explore regions of the jsapi I had never ventured into before, as well as, getting to know dojox/gfx. GFX being the vector graphics api that powers esri graphics and graphic layer types.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Turns out there there is a lot going on with graphics and graphic layers; way more than I had expected.&amp;nbsp; While the layer object contains all of its graphics in the DOM, a graphic may not have a dom node in the GFX surface.&amp;nbsp; This makes sense when you think about a layer being hidden or a graphic being outside the extent of the map (the GFX surface).&amp;nbsp; The jsapi is destroying and creating shapes (vector objects) whenever a layer refreshes or a graphic's geometry/style properties are updated.&amp;nbsp; As a result the initial dom node the menu was bound to no longer exists.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So how to keep the menu bound to the dom node which is currently representing our graphic?&amp;nbsp; My first thought was to rebind the menu when updating the geometry and style, and by iterating through graphics on layer events, which seemed excessive and messy.&amp;nbsp; I ended up using the layer's "mouse-over" event to bind the menu to whatever dom node is representing the graphic.&amp;nbsp; After having spent a day diagnosing the problem and coming up with a solution I happened upon the &lt;/SPAN&gt;&lt;A href="https://developers.arcgis.com/en/javascript/jssamples/graphics_contextmenu.html" rel="nofollow noopener noreferrer" target="_blank"&gt;Display context menu&lt;/A&gt;&lt;SPAN&gt; in the samples.&amp;nbsp; The sample also uses the "mouse-out" event to unbind the menu, which is best practice for dom nodes that may be destroyed.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;on(this.layer, 'mouse-over', function(evt) {
&amp;nbsp; evt.graphic.menu.bindDomNode(evt.graphic.getDojoShape().getNode());
});
on(this.layer, 'mouse-out', function(evt) {
&amp;nbsp; evt.graphic.menu.unBindDomNode(evt.graphic.getDojoShape().getNode());
});&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In conclusion: 1) Always check the samples. There's a chance it may save you time and hassle.&amp;nbsp; 2) Check out &lt;/SPAN&gt;&lt;A href="http://dojotoolkit.org/reference-guide/1.9/dojox/gfx.html" rel="nofollow noopener noreferrer" target="_blank"&gt;dojox/gfx&lt;/A&gt;&lt;SPAN&gt;. The demos and test folders &lt;/SPAN&gt;&lt;A href="http://download.dojotoolkit.org/release-1.9.1/dojo-release-1.9.1/dojox/gfx/" rel="nofollow noopener noreferrer" target="_blank"&gt;here&lt;/A&gt;&lt;SPAN&gt; are great for learning about GFX and will enlighten even long time jsapi developers.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:17:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/on-binding-dijit-menu-to-a-graphic/m-p/733757#M67981</guid>
      <dc:creator>BenFousek</dc:creator>
      <dc:date>2021-12-12T07:17:54Z</dc:date>
    </item>
    <item>
      <title>Re: On binding dijit/Menu to a graphic</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/on-binding-dijit-menu-to-a-graphic/m-p/733758#M67982</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Nice summary, Ben. Thank you very much for taking time to share your experience.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Nov 2013 02:26:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/on-binding-dijit-menu-to-a-graphic/m-p/733758#M67982</guid>
      <dc:creator>JasonZou</dc:creator>
      <dc:date>2013-11-26T02:26:10Z</dc:date>
    </item>
    <item>
      <title>Re: On binding dijit/Menu to a graphic</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/on-binding-dijit-menu-to-a-graphic/m-p/733759#M67983</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Great job, Ben.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Just a note, you can always call graphic.getNode() to get the raw DOM element, which could be SVG or canvas depending on browsers. Calling getDojoShape() is a good idea since dojo is doing a great job to eliminate the discrepancies among browsers. I totally agree with you, explore GFX and take advantage of it whenever you can. Manipulating DOM element directly is very hard and error-prone.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Nov 2013 04:02:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/on-binding-dijit-menu-to-a-graphic/m-p/733759#M67983</guid>
      <dc:creator>JianHuang</dc:creator>
      <dc:date>2013-11-26T04:02:20Z</dc:date>
    </item>
  </channel>
</rss>

