-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
A fromJS
api (opposite of toJS)?
#297
Comments
Isn't that what observable does? It takes a plain object/array/value and will make every part of it observable. |
I think Anton is dealing with computed fields and such. So he'd want his On Thu, Jun 2, 2016 at 10:55 AM, Aria Fallah notifications@github.com
-Matt Ruby- |
@mattruby exactly. As per mobx-reactive2015-demo my store consist of nested classes with If we think of |
@antitoxic Yeah, it makes sense. But I don't think MobX is going to handle that out of the box. I've tried to match my data to the toJS output whenever I've needed that functionality. Then I just run the data through the same init code to create my MobX graph again. |
I don't quite get this. What do you mean by 'init code' ? Do you have an example I can try to learn from? |
@antitoxic Here's a simple example of what I'm talking about: https://jsfiddle.net/yxvz4wk6/ |
@mattruby I understand. So a factory pattern. However I would argue that this can be automatic because 99% if not 100% of use-cases the passed in object keys match properties of the observable. Why do you need a constructor and all the code? Your example proves this actually. I liked mobx because it removed a lot of boilerplate for me. This seems like a good opportunity for "mobx to the rescue" . Would a utility function that walks js object recursively and fills an observable be a good fit for mobx?
When a key match is found mobx checks if its a user-defined class (or array of such) and creates recursively continues setting properties from the Am I missing something / being naive (completely possible :) )? |
@antitoxic Yeah, that example had very little need of all that work. In my experience, things get hairy when you have the cyclical references: parent > child with child > parent relationships. The cyclical nature of that can be rough to serialize and de-serialize in a standard way. Regardless of that. I think your example would be possible. Not sure if it's good fit for the core. That would be on @mweststrate What I'm still not getting is if you're pumping the data into MobXed objects once, why not just do it again? |
True, but how often do you have those?
I'm never pumping data like this. The user actions change the data, push new objects or remove others - these are all small patches. The only time I'm creating a mobx observable from a large js object is on initializing. There might be another scenario but I can't think of it. |
Thanks for the suggestions and questions so far! Providing a standardized mechanism for |
+1. Here's something I found dealing with a similar but more generic problem: https://github.com/JohnWhiteTB/TypedJSON. |
@mweststrate Very interested in hearing your proposal for Another thing we need is a way to serialize a graph into JSON (string), and back again. Currently, Basically something like: const identity = compose(toJS, toJSONGraph, toJSON, fromJSON, fromJSONGraph, fromJS)
const state = new AppState(/* ... */)
assert.deepEqual(state, identity(state), 'must be isomorphic')) Any thoughts on the server-side rendering use case? |
Closing this issue as |
Hello and kudos for the new state-handling approach.
Having server-rendering and time-traveling in mind, is it not possible to make
fromJS
api that will read a plain object and create all necessary nested classes defined in the observable structure?The text was updated successfully, but these errors were encountered: