Search code examples
javascripteventsgojs

GoJS Diagram - Get the object which was Double Clicked


I am using the GOJS (http://www.gojs.net/latest/index.html) and have a diagram with some nodes on it which have their own data model structure.

I am attaching a "ObjectDoubleClicked" event like so:

    Diagram.addDiagramListener("ObjectDoubleClicked", function (e)
    {
        // Do something
    });

The event fires just fine but I am unable to get the subject (bring the node) of the event. The e.subject stays undefined.

As I have multiple node types which would be dropped on the diagram , I need to manage them.

What is the best way for me to get the node which was double clicked?

Thanks,


Solution

  • I made a JSFiddle with a basic example of what you are asking.

    HTML

    <div id="myDiagram"></div>
    

    CSS

    #myDiagram{
        width:200px;
        height:200px;
    }
    

    You must assign a width and height to the Diagram or it will not show up.

    JavaScript

    var $ = go.GraphObject.make;
    var myDiagram = $(go.Diagram, "myDiagram", {
        initialContentAlignment: go.Spot.Center,
        "undoManager.isEnabled": true
    });
    
    myDiagram.addDiagramListener("ObjectDoubleClicked", function (ev) {
        console.log(ev.subject); //Successfully logs the node you clicked.
        console.log(ev.subject.ie); //Successfully logs the node's name.
    });
    
    //(...) I've skipped the key adding, because it's not necessary to understand this code.
    

    Most of the code is taken from the go.js Diagram class and go.js DiagramEvent class documentation.

    Hope it's what you wanted!