The reason is that the value of Link.toNode is null at the time that both console.log calls happen.
But by the time that you look at the details of that Link object in the debugger, the value of Link.toNode has already changed to be a reference to some Node.
That appears to be because loading Links happens to set the Link.fromNode before it sets the Link.toNode. It’s got to set the two properties in some order, after all, and it’s not guaranteed that the toNode will be set at all after setting the fromNode, if the link is only partly connected.
I see - that makes sense. I have a conundrum then. At least for me with my limited knowledge of GoJS at this point. Here is my use case in a nutshell:
If a connection is made to a node specifically of type “X”, I need to look back at the originating node (“Y”) to get some node data properties from it.
Implement Link.fromPortChangedandLink.toPortChanged event handlers, only doing what you want when the Link.fromNode is of type “Y” and the Link.toNode is of type “X”.
Or implement a Node.linkConnected event handler. I don’t know if you’ll also need a Node.linkDisconnected event handler. Note that you’ll need this event handler on both Nodes, depending on the order in which links are connected.
There are other possibilities too, but these are the most natural when trying to put the behavior in the templates.
Hmm. That is what I was trying to do originally, using Link.fromPortChanged. But I kept running into that null issue. I will investigate linkConnected as well.
Unless I’m missing something, they shouldn’t be null … the handler gets called when I make the connection. Shouldn’t fromNode be node type “Y” in my example? Or is it saying that it is null because there was no previous node (in the case of a new connection being made)?
Oh, I get that. I’m just confused as to why the method even has thisLink as an argument, if it isn’t guaranteed to be ready for use … ? I may be missing something here, but doesn’t that invalidate using that method as a solution to the use case? Not sure if this is also the case with linkConnected or not - gonna try that one next :)
var link = ... a new Link that is in the Diagram but not yet connected to anything ...
link.fromNode = ... some Node in the Diagram ...
// would you not expect the event handlers to be called now?
// then
link.toNode = ... some Node in the Diagram ...
// would you not expect more event handlers to be called only now?
Ok, so looks like linkConnected gets called twice as well (once for each endpoint). Is there a way to get a hook into when the whole connection is made and intact?
My challenge in implementing two endpoint handlers to work together is that one of them has the data I need, and the other one needs to do something with it. Depending on what order they connect the link, they could fire in a different order. Just trying to make this as clean as possible. Trying to avoid a bunch of “if” statements in each handler trying to figure out who is who lol.
Yeah, that’s what I was hoping to avoid somehow. :) I figured it out finally and all is well in Mudville :)
I do think it would be a great feature to have a method/hook that would fire when the circuit is complete, though. It would be really handy to have the source node, destination node, and the final link in a single handler method.
Cheers, and thanks for putting up with me as I navigate GoJS :)