Dynamically adding name to a link

Hi,

I am using a linking tool to create a new link between nodes. I used the following link for inspiration - LinkingTool | GoJS API.

I am drawing a link from the adornment in my node. I have two adornments on the node and would like to name the link same as the adornment from which link is drawn.

    var drawLink = function (e, obj) {
        var node = obj.part.adornedPart;
        var tool = e.diagram.toolManager.linkingTool;
        var name = obj.name;
        // name here holds the name of the adornment. Now all I need is to, display it as the "text" on the link.
        tool.startObject = node.port;
        e.diagram.currentTool = tool;
        tool.doActivate();
    };

The are many nodes, and I cannot use from, to and text as described in the link - GoJS Link Labels -- Northwoods Software

Please let me know how this can be achieved.

How is this drawLink function called? Is it a click event handler of a Button in an Adornment?

The value of obj.name in that context would be the GraphObject.name of the Button. I suppose you could have given the Button a name, although that is not very common. But assuming that you have given the Button a name, how were you planning on using that name to affect the LinkingTool that you start when you set Diagram.currentTool?

A small screenshot might help explain things so that I can understand.

Hi Walter,

Below is a screenshot of the panel and what I am trying to achieve.

Yes, drawlink is a click event handler of a button in the Adornment.

button.click = drawLink;

In the example, I have three Nodes and each node has two Adornments (Alpha and Beta), which show on hover of the node.

As you can see, I have linked the Node A to Node B from Adornment Alpha and Node A to Node C from Adornment Beta. I would like to have a display text for the link which would be same as the respective Adornment name from which the link is drawn.

You are probably using a GraphLinksModel, so each Link represents some link data object in the model.

So you can define the link template to have a link label that is a TextBlock that has a Binding of TextBlock.text from some data property. Let’s call that property “label”. Then when you call GraphLinksModel.addLinkData you can make sure the data that you pass it includes not only the “from” and “to” properties, but also the “label” property whose value is the name of the clicked Button.

I tried it using GraphLinksModel.addLinkData, but it doesn’t add the text.
var drawLink = function (e, obj) { var node = obj.part.adornedPart; var tool = e.diagram.toolManager.linkingTool; var newlink = { from: node.data.key, to: 0, text: "label"}; e.diagram.model.addLinkData(newlink); tool.startObject = node.port; e.diagram.currentTool = tool; tool.doActivate(); };

I also have a binding in the linkTemplate -
$$(go.TextBlock, new go.Binding("text"))

Let me know if I am missing something here.

Why are you starting the LinkingTool when you have already added a new Link? And shouldn’t the “to” key be dependent on the node, rather than the constant 0?