How to know if any node is not connected with any other node?

I have a palette and user can create “n” number of nodes and link with each other.
I want to know if all the nodes are connected at least with 1 other node? And if any node is not connected with any other node in the diagram, I want to know which node that is and a border around that node with red color.

Can someone please tell me how to do it?

The literal answer to your question is to evaluate:

  myDiagram.nodes.all(function(n) { return n.linksConnected.count > 1; })

For highlighting unconnected nodes you could do something like:

  myDiagram.nodes.each(function(n) {
    var border = n.findObject(...);
    if (border !== null) border.stroke = (n.linksConnected.count === 0) ? "red" : null;
  });

This assumes that your node template has a main element of an “Auto” Panel with some name (that I did not specify above) that acts as the highlighting border, and that the border when not highlighting has a stroke of null or “transparent”.

However, executing the above code is inconvenient (when would you call it?) and inefficient (it operates on all of the nodes of the diagram). Another possibility is to define Node.linkConnected and Node.linkDisconnected event handlers. http://gojs.net/latest/api/symbols/Node.html#linkConnected

The Pipes sample demonstrates using these event handlers for automatically hiding/showing ports:

          // hide a port when it is connected
          linkConnected: function(node, link, port) {
            port.visible = false;
          },
          linkDisconnected: function(node, link, port) {
            port.visible = true;
          }

But you could use these event handlers for setting the border’s Shape.stroke to either “red” or null. Note especially that these event handlers do not allow adding/removing/reconnecting any links.

Thanks for the help.
Actually I want to validate the diagram in the sense that if a user drags multiple node into the diagram from Palette and mistakenly miss few nodes to connect with other nodes then the Application should validate it and a pop-up message should come and those nodes should get highlighted in red and user should not go ahead and save the diagram.

OK, so you want to do what I first suggested, rather than implement the linkConnected and linkDisconnected event handlers.

Still, I think it is generally nicer to the user if you are continually updating the validity of the diagram instead of making them invoke some command and show a list of problems.

In either case, you might want to consider the approach used by searching in the Org Chart (static) sample: http://gojs.net/latest/samples/orgChartStatic.html. The advantage of that design is that the node templates can decide how to implement its appearance when Part.isHighlighted is true, and other code can independently call Diagram methods dealing with “highlighted”. That makes the latter functionality not have to know about how the former is implemented.

Thanks.!! That was much help. :)