On binding dijit/Menu to a graphic

Discussion created by btfou on Nov 25, 2013
Latest reply on Nov 25, 2013 by Jian
I recently ran into some trouble binding menus to graphics and this is what I learned.

//create graphic
if (result.geometry.type != 'extent') {
  var graphic = new Graphic(result.geometry);
} else {
  var graphic = this._extentToPolygon(result.geometry);

//add graphic

//graphic menu = new Menu({
  contextMenuForWindow: false,
  leftClickToOpen: false

//add some menu items MenuItem({
  label: 'Move to Front',
  onClick: lang.hitch(this, function() {
})); MenuItem({
  label: 'Move to Back',
  onClick: lang.hitch(this, function() {

//bind the menu to graphics dom node;

I was creating graphics, each with a custom menu based on geometry type and such, then binding the menu to the graphics dom node.  The menu was initially bound to graphic's dom node, however any number of actions appear to magically unbind the menu.  These include, but are not limited to, panning/zooming the map and editing the graphic's symbol or geometry.  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.

Turns out there there is a lot going on with graphics and graphic layers; way more than I had expected.  While the layer object contains all of its graphics in the DOM, a graphic may not have a dom node in the GFX surface.  This makes sense when you think about a layer being hidden or a graphic being outside the extent of the map (the GFX surface).  The jsapi is destroying and creating shapes (vector objects) whenever a layer refreshes or a graphic's geometry/style properties are updated.  As a result the initial dom node the menu was bound to no longer exists.

So how to keep the menu bound to the dom node which is currently representing our graphic?  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.  I ended up using the layer's "mouse-over" event to bind the menu to whatever dom node is representing the graphic.  After having spent a day diagnosing the problem and coming up with a solution I happened upon the Display context menu in the samples.  The sample also uses the "mouse-out" event to unbind the menu, which is best practice for dom nodes that may be destroyed.

on(this.layer, 'mouse-over', function(evt) {;
on(this.layer, 'mouse-out', function(evt) {;

In conclusion: 1) Always check the samples. There's a chance it may save you time and hassle.  2) Check out dojox/gfx. The demos and test folders here are great for learning about GFX and will enlighten even long time jsapi developers.