Auto linking of nodes

I have a pallete and grid . On drop of a node from pallete to the grid i want to auto link it with the already added node . how can i achieve this?
Is there any way of knowing last added node.

Implement an “ExternalObjectsDropped” DiagramEvent listener. The Diagram.selection collection provides the Parts that were created by the drop, and that you can modify or link with.



As seen i have only start node initially so my diagram details will be

var nodeDataArray = [ {category: “circle”, text:“Start”, “loc”:“175 0” ,figure:“circle”}];
var linkDataArray = [];
myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

and once the step diagram is dropped into the RHS grid

myDiagram.addDiagramListener(“ExternalObjectsDropped”,function(e) {

var sel = e.diagram.selection;
var elem = sel.first();

how should i get the already existing nodeDataArray and linkDataArray?
and if third node is added how will i link second node(at this time first node should be linked to second node) to third node as its added dynamically please help

e.diagram.model gets you the Model.

There are a lot of ways to choose which Node would be best to automatically link with. You need to decide the algorithm that you want your app to use. If you describe the conditions precisely, we can give you advice for how to implement it. But you should make sure that your algorithm will work correctly in all circumstances before you try to implement it. It is a lot easier to do thought experiments than it is to implement non-trivial code if you do not know precisely what you want to implement.