I realize if I set undoManager.isEnabled to false in my diagram, I will no longer get go.ChangedEvent.Insert from model changedListener fired? Actually, evt.object is always null. If I mark isEnable to true, everything just works fine. Are they related?
if (!evt.isTransactionFinished) return;
let txn = evt.object; // a Transaction
if (txn === null) return;
// iterate over all of the actual ChangedEvents of the Transaction
txn.changes.each((e) => {
if (e.change === go.ChangedEvent.Insert) {
if (e.modelChange === "nodeDataArray") { // add node
................
} else if (e.modelChange === "linkDataArray") { // add link
................
}
}
That’s correct – when the UndoManager is disabled, or when Diagram.skipsUndoManager or Model.skipsUndoManager are true, no ChangedEvents are recorded.
Actually, I use ChangedEvents in order to capture add/remove/relink/rename happens on diagram, which will cause an update to my model (not gojs model, but other data source). So in this case, do you think what I am doing is right or not?
I see – so listening for Changed events on the Model still makes sense. It’s just that you cannot depend on the UndoManager batching up sets of ChangedEvents in Transactions, because the “Transaction” ChangedEvent raised when a transaction finishes won’t have an actual Transaction with ChangedEvents in it.
I have another question. How should I know undo/redo happen in ChangedEvent so that I can delete or add corresponded data to my other model as well? Just like I listen on go.ChangedEvent.Insert and I will add something to my other model once I get into this condition. However, undo/redo seams not fire this event. What is the right approach for my purpose?
Yes, it does – that’s what I just mentioned about ChangedEvent.TransactionChangedEvents, rather than the usual ChangedEvent.Property, ChangedEvent.Insert or ChangedEvent.Remove enum values of ChangedEvent.change.