Continuing the discussion from Target and source elements are null in a "connect.end" listener:
This has been bothering me for a while, as the title says, I’m trying to add a custom rules that cancels a connection creation based on a condition that depends of the source and target Id.
I tried a lot of listeners but this is as close as I got:
- This is what I initially tried using
connect.end
, I could only get the source id but not the targets and I was able to cancel the creation of the connection by returning false with this listener:
eventBus.on('connect.end',2000, function(event) {
console.log(event);
let context=event.context;
if (context.source.id===context.target.id || !self.checkConnection(context)){
return false;
}
});
- Then I was advised to not use this listener by Niklas_Kiefer but use
commandStack.connection.create.postExecute
event instead. I was able to get the source and target id with this event but I was not able to cancel the connection by returning false.
eventBus.on("commandStack.connection.create.postExecuted", function (
event
) {
const context = event.context;
const source = context.source;
const target = context.target;
console.log("Source:", source);
console.log("Target:", target);
if (
context.source.id === context.target.id || !self.checkConnection(context) /*another condition*/
) {
return false;
}
});
Is there an event that allows me to get both source and target Id and still be able to cancel the connection from being madeb ased on them?
A detail I forgot to mention, I also tried using this rule in my customrulesprovider
this.addRule("connection.create", 2000, function (context) {
console.log(context);
return false;
});
with this rule which provides what I need (source and target ids + ability to cancel the connection) the only reason I didnt go with it and I went for eventBus listeners instead is that I need to get the xml representation which I will use to set my condition.
And since I’m getting the xml representation like this I need my modeler instance to get the xml representation from:
const result = await this.bpmnJS.saveXML();
const { xml } = result;
So If I’m able to get the xml representation inside the customrulesprovider I guess that could do it as well.
Another codesandboxcode for the second method.