I want to update the link data object before a LinkDrawn event is triggered. Currently the only way Im able to add link metadata is through calling model.setDataProperty as shown below:
onLinkDrawn(e: go.DiagramEvent) {
const link = e.subject as go.Link;
const uniqId = `${Math.floor(Math.random() * 10000)}`;
this.diagram.model.commit(m => {
const linkData: LinkMetadata = {
modified: true,
displayName: '',
description: '',
type: 'seq',
};
m.setDataProperty(link.data, 'data', linkData);
m.setDataProperty(link.data, 'id', `prefix/data/id-${uniqId}`);
}, 'Link Drawn');
}
The issue with this approach is that Im getting a model ChangedEvent without the newly added data
property in the link data.
First, as documented, the “LinkDrawn” DiagramEvent listener is called during the link drawing transaction, so you don’t need to conduct your own transaction.
Second, the link.data
Object is indeed in the GraphLinksModel.linkDataArray at the time the listener is called. So there shouldn’t be any problems with modifying link data properties within the “LinkDrawn” listener.
Third, when do you want to modify the link data object?
If you want to do it before the LinkingTool starts running, you can modify the LinkingTool.archetypeLinkData at any time before it is running.
If you want to do it while the LinkingTool is running and before the “LinkDrawn” DiagramEvent listener, you’ll need to override a method of that tool. But I don’t know what you really want to do.
Okay so I have removed the encapsulated commit block. What i do want is that the ChangedEvent for the link gets called only after the link data (which is dynamic) has been populated.
this.diagram().addModelChangedListener(e => {
if (e.propertyName === 'nodeDataArray') {
this.nodeDataChange.emit(e);
} else if (e.propertyName === 'linkDataArray') {
// here (e.model as go.GraphLinksModel).linkDataArray does not contain the updated link data, as this gets called first before LinkDrawn event
this.linkDataChange.emit(e);
}
});
Basically onLinkDrawn method (given in the original post) gets called after the modelChangedListener runs, as the result of which the changedListener doesnt get the ink data which remains undefined.
Is there a way to get a pre-hook before the linking tool runs? I can then probably use LinkingTool.archetypeLinkData to populate the metadata for the link.
Override LinkingTool.insertLink, LinkingTool | GoJS API, to first modify the LinkingTool.archetypeLinkData, LinkingTool | GoJS API, to have whatever properties you like, and then call the super method.