I see your demo Update Demo GoJS Sample
when 2 nodes are linked, How can I get the Key of the node and the portIn, portOut ?
I see that with the folowing code I get an GraphLinksModel object but I don’t see how to get the Key of source /dest node and the port id connected / removed
model.addChangedListener(e => {
if (e.isTransactionFinished) {
var tx = e.object;
if (tx instanceof go.Transaction && window.console) {
window.console.log(tx.toString());
tx.changes.each(c => {
like with the link validation but also with removed link:
function sameColor(fromnode, fromport, tonode, toport) {
return fromnode.data.color === tonode.data.color;
// this could look at the fromport.fill and toport.fill instead,
// assuming that the ports are Shapes, which they are because portID was set on them,
// and that there is a data Binding on the Shape.fill
}
// only allow new links between ports of the same color
diagram.toolManager.linkingTool.linkValidation = sameColor;
// only allow reconnecting an existing link to a port of the same color
diagram.toolManager.relinkingTool.linkValidation = sameColor;
$(go.Diagram, . . .,
{
// at the Diagram level, only when the user finishes drawing a new Link
"LinkDrawn": e => { // the "LinkDrawn" DiagramEvent is raised by the LinkingTool
const link = e.subject; // the new Link
console.log(link.fromNode.key, link.toNode.key); // show the connected Nodes' keys
},
// at the Model level, two choices:
// 1, as each ChangedEvent happens
// 2, at the end of the transaction
"ModelChanged": e => {
// choice 1:
if (e.change === go.ChangedEvent.Insert && e.propertyName === "linkDataArray") {
// if it's an Insertion into the GraphLinkModel.linkDataArray,
const data = e.newValue;
console.log(data); // show the new data object, which includes the connected node's keys
}
// choice 2:
if (e.isTransactionFinished) { // at the end of a transaction or undo or redo
const tx = e.object;
if (tx instanceof go.Transaction) {
tx.changes.each(c => { // review all of the ChangedEvents in the Transaction
if (c.change === go.ChangedEvent.Insert && c.propertyName === "linkDataArray") {
// if it's an Insertion into the GraphLinkModel.linkDataArray,
const data = c.newValue;
console.log(data); // show the new data object, which includes the connected node's keys
}
});
}
}
}
});