Adding data into model with some conditions

When i drag and drop from palette the data in the diagram model’s nodedataarray added automatically . I want to add data into diagram model with some condition using a function . how would i do that?

If I understand you correctly, you want to implement an “ExternalObjectsDropped” DiagramEvent listener that modifies the dropped nodes. There are many examples of this if you search for “ExternalObjectsDropped” throughout the samples.

In your case you probably want to call Model.setDataProperty on the Node.data of the newly copied Nodes held in DiagramEvent.subject, which is the Diagram.selection.

No, I don’t want to implement External Objects Dropped event. I want some event on which i can add nodes to diagram model based on some condition. if i write in External Objects dropped , nodeDataArray is already filled up till then .

You could delete the added node and then add whatever nodes you like.

Although the Macros example does this for a different purpose, I think the example is still instructive: Graphical Macros via Auto Ungrouping

there isn’t any event before filling up the nodeDataArray?

I suppose you could customize the DraggingTool. But it’s just much easier to implement a DiagramEvent listener.

So i have a scenario where when user drops into diagram from palette , before drooping it will go to the backend add that node to thebackend and then come up with an event if it says u can add , then add it / drop it to the gojs diagram. Can you please tell me what do i need to do to achieve it. i hope it simplifies my question.

First, you should try to implement any validation locally in your app. Users should not wait for a server response which might be indeterminately long in coming.

There is no way to stop a user from letting go of the mouse button where ever they want. But you might be able to show interactive feedback saying that a drop would not be permitted in some places, and you might be able to cancel the drag-and-drop operation when they try to drop anyway where your app knows apriori it would not be OK.

Second, for validation decisions that can only be performed on the server, I recommend that you implement optimistic behaviors. If most of the time it would be OK to drop somewhere, go ahead and allow it to happen. Tell the server, giving it the key or other identifier of the new node(s).

If/when the server eventually responds, it would normally say “OK” and everything is good. In some apps you will want to update the key/id of the new node to whatever value the server database had assigned – do that by calling Model | GoJS API.

If the server responds with a “Not Allowed” message, then you can tell the user what the problem is and then delete the node, which you can find using Model.findNodeDataForKey or Diagram.findNodeForKey.