Binding *MaxLinks issues

I’m binding my fromMaxLinks and toMaxLinks in an itemTemplate to represent ports as I want to dynamically support changing the max number of links for a given port.

...
new go.Binding("fromMaxLinks", "services"),
...

This works great when trying to add new links (as the LinkingTool understands these properties), however, I’m wondering what should be done if I, for instance, add 5 links to a port, then reduce the number of services to 3. Currently, all 5 links stay connected, even though I cannot add new ones.

I would like to either have these links break (FILO order) or color the links (red) until I manually remove them to max the *MaxLinks value I’ve set.

Do you have any suggestions for this situation?

You seem to have very specific policies for what to do, so I hope it is not too surprising that GoJS does not impose any particular policy.

You’ll need to examine each such port and decide which of the connected Links you want to remove.

Yeah, that’s what I was assuming.

Presumably I have to be able to determine the connected links within a given binding - is there a suggested way to do it?

Oh, you should not be making changes within a Binding conversion function. You never know when or how often those bindings might be evaluated.

Ah yes, you’re right.

So, what if I want to color a given port (in an itemTemplate) based on the number of connected links for that port?

Something like this:

new go.Binding("fill", "services", (services: number) => {
  if(services < numberOfConnectedLinksToThisPort)
    return "red";
  else
    return "black";
}),

That’s typically what the Node.linkConnected and Node.linkDisconnected event handlers are for.

Those events aren’t the right time for this situation.

I’m using *MaxLinks to determine how many links can be added to a port (service is this max number of links on the nodeData)

The service value is modifiable by the user outside of the diagram. This means that the user can get into a state where there are more links connected to a given port than should be allowed. At this point, I want to highlight the offending links (all links connected to the offending port).

Node.linkConnected and Node.linkDisconnected will not be fired at this time, since a link has not been added or removed.

Ah, OK, so you can’t use either bindings or the link-connected event handlers.

You’ll have to iterate through all of the modified nodes and their ports to highlight connected links the way that you want. Perhaps you want to implement support for highlighting in your link template(s), GoJS Highlighting -- Northwoods Software. Then it’s just a matter of setting Link.isHighlighted to true or false, as appropriate.

Okay, thanks. I was hoping to avoid iterating through all links, but that’s fine.

Is that service property on an item object corresponding to each port? So if you updated a particular port’s service property, you just want to update all of the links connecting with that port?

If that’s the case for a small number of ports, it seems iterating over all of the Diagram.links is indeed very inefficient. You just need to find the Node for the port being updated, call Node.findLinksConnected with the particular portId, and then, depending on the count of that collection, update the appearance of those Links.

On the other hand, if you are updating that property on a lot of nodes at once, then it might be faster to iterate over all links in the diagram.

Yeah, you’re right. I forgot that I could pass a portId into the Node.findLinksConnected, thanks for the reminder! That makes the problem much more manageable, as I should only be check a handful of ports, each with just a few links.