svelte-jsoneditor
svelte-jsoneditor copied to clipboard
Flag for programmatic vs UI changes
Hi Jos I'm a big fan of your original library, we used it to develop a collaborative JSON editor.
The key functionality we relied on was that setting the content programmatically didn't emit a onChange. This prevented loops in the 2 editors updating each other. When we set the content a onChange didn't happen, when the human made a change we picked that up and transmitted to the other editor and did a programatic update.
Is there anyway to enhance the Svelte component to maybe pass a flag letting us know if it was a change from called the set() or update() functions or if the change was a result of a UI edit.
Regards
I’d been struggling on this too while making json-editor-vue, I use two flags to solve this.
Thanks @cloydlau I'll go have a look at how you solved it
Thanks Werner, very good point.
For reference: the reason that I recently change the behavior from onChange
events on user changes only to all changes was the discussion here #128.
I agree that the old behavior is also very handy to prevent infinite loops and needless callbacks. So both cases can be useful, and your idea to be able to distinguish programmatic vs user changes makes sense.
For the time being: you can indeed set some flag before doing a programmatic update, and reset it after the update, and in the onChange
you can check for this flag. And alternatively, it may be possible to compare the updated content against the previous contents in onChange
, and if there is no difference ignore the change.
Thanks Jos, I'll do the flag thing for now.
In v0.22.0
we've changed the behavior of onChange
to not trigger anymore for programmatic changes. This resolves this issue indirectly, no flag needed anymore.