Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the set method is loosing the reference. #431

Closed
m2omou opened this issue Jul 12, 2017 · 5 comments
Closed

the set method is loosing the reference. #431

m2omou opened this issue Jul 12, 2017 · 5 comments
Labels

Comments

@m2omou
Copy link

m2omou commented Jul 12, 2017

Hi,

Is there a way for JSONEditor.set method, to not loose the reference of my object ?

So basically when I change something in the editor, it should automatically reflect on my object, right now the only solution is to use JSONEditor.get and reset my object.

Thanks for the help!

@josdejong
Copy link
Owner

This is indeed the intended behavior, load an object via JSONEditor.set, listen for changes using onChange, get the changed object via JSONEditor.get.

Can you explain why you need to apply the changes to your existing object instance and why replacing the object is not an option?

@m2omou
Copy link
Author

m2omou commented Jul 12, 2017

I was just curious, since I was passing an object, I was expecting JSONEditor to update that same object so I don't have to call every time JSONEditor.get to refresh my object.

Btw thank you for that awesome json editor. 🏅

@josdejong
Copy link
Owner

Thanks, you're welcome.

Having JSONEditor mutate the JSON object that you pass it via set would lead to hard to debug code if say the same object is also managed by Angular or something. How can JSONEditor know that something was changed inside the object externally? How can external components know that JSONEditor changed anything in the object?

In general, the JavaScript community is moving away from shared objects which can are manipulated by different parts of an application at the same time, towards functional and immutable techniques. This leads to much easier to understand code, easier to understand flow of data through an application, and easier to test and debug code. I think that's great!

The new JSONEditor v6 version that I'm working on will have a new function which gives you JSONPatch changes via onPatch (replacing onChange), which allows patching an existing instance of a JSONObject or keep multiple editors in different browsers in sync and that sort of stuff. Still a long way to go for v6 though but we'll get there.

@m2omou
Copy link
Author

m2omou commented Jul 12, 2017

True what you are saying makes total sense. It's more safe.

Excited to try the v6! ;)

@josdejong
Copy link
Owner

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants