Auto save when model change

The problem I have is that I am getting unwanted event when using addModelChangedListener

the scenario is like this:

  • diagram.addModelChangedListener(this.modelChangeEventListener)
  • diagram.model = myModel
  • then event model change event is fired. (I did not change the model in any way), just setting the model creates the event

this event is no needed so I tried to workaround the problem by writing the next lines in the event handler

 if (event.isTransactionFinished) {
           if (event.oldValue == "Initial Layout") {
 
               return;
           }
       }
  • Question: is this a good way to workaround the problem?

note that in my code I change the model from time to time because I need to create different diagrams according to my UI logic.

so every time the model needs to be changed I am writing this code
diagram.model = myModel

and every time I am doing that I am getting the extra event.

  • what am I doing wrong?
  • is it ok to use event.oldValue == "Initial Layout"?

That’s normal, you aren’t doing anything wrong.

Ignoring “Initial Layout” is fine. Are you sure there are no changes happening, though? Do you use a layout? Are all your locations saved/loaded with the model?

All my locations are saved. My model is saved.
I am not using any special layout.
part of my link template is

    routing :go.Link.AvoidsNodes,
    link.curve : go.Link.JumpOver,
    link.curviness : 10,

The problem I have with event.oldValue == "Initial Layout,
is that I am only check for event.oldValue, this is not an event name, it is just a value.

if I look at the (event.object as Transaction).changes I can see that the propertyName = position property has changed.

is this still ok?
is there a constant for Initial Layout ?

I also noted the the new value is always null

That suggests that there may be some side effects changing data as a result of the initial layout when you are setting your models. It is probably safer not to skip the “Initial Layout” transaction, but ultimately it is up to you and what your app needs.

thanks .
is there a constant so I can use instead of hard coded literal Initial Layout ?

No, that’s just the transaction name, which is a string.