- Notifications
You must be signed in to change notification settings - Fork638
Splitting the concept of references into two? (Tree reference and Identifier Reference)#1995
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
One of the features that I both love and struggle the most is references. And I think the main reason is because it was designed to reference a node from the tree, but has potential to become much more, and be misused exactly because of its potential. My main problem comes from trying to use references with data passed from the server. While Let's imagine that the server response returns the following Todo response: {"id":2,"tittle":"hello world","authorId":"3342"}Current problems in using
A way that I've been using to bypass this issue is to make constTodo=types.model({id:types.identifier,title:types.string,authorId:types.string,}).views(getauthor(){returnresolveIdentifier(User,getRootStore(self),self.authorId)},) What I would love to be able to do, is have both client tree references, and general identifier references, that can later be used to reference a model in the tree. constTodo=types.model({id:types.identifier,title:types.string,authorId:types.identifierReference(User)// Reference to a User identifier, which would validate the type (string/number)author:types.safeReference("authorId")// Reference based on the object property "authorId"})constTodoStore=types.model({todos:types.array(Todo),selectedTodo:types.reference(Todo)// A reference to a node in the tree}) In this case Are there any other ways of solving this issue? |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 2 comments 2 replies
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Hey@sergioisidoro - I think I see where you're coming from with regard to the many ways references can and do get used, but I wonder if a call to the existing Consider this code: import{getSnapshot,types}from"mobx-state-tree";constTodo=types.model({id:types.identifier,title:types.string});constTodoStore=types.model({todos:types.array(Todo),selectedTodo:types.reference(Todo)// A reference to a node in the tree});constt1=Todo.create({id:"1",title:"First"});constt2=Todo.create({id:"2",title:"Second"});conststore=TodoStore.create({todos:[t1,t2],selectedTodo:"1"});console.log(store.selectedTodo);// {id: "1", title: "First"}constsnapshot=getSnapshot(store);console.log(snapshot.selectedTodo);// "1" You can see the output and play around with it in Codesandbox here:https://codesandbox.io/s/vigilant-sea-tpwquv?file=/src/index.ts Just logging out When I fetch data from the network and either put it into, or take it out of, MST, I usually do some kind of serialization with What do you think? Does that get at the core of what you're thinking? Or is there something else you feel like you need from an additional identifier/reference type? |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Hi@coolsoftwaretyler ! Thanks for taking the time to answer :) Yes, indeed your suggestion is a quick way to get the primitive. But my issue extends to the async loading of data, and wanting to keep my api response and state as close as possible to each other, without having to write much glue code. Coming back to my initial example, if my Todo API returns: and I have I could inline serialise author, but then we need to create glue code, where I have to 2 step update the store, first adding the user to the Maybe my main issue is losing the data with This leaves me with custom references where I have to do a bit of glue code for loading missing references during resolution. I think this is the canonical way of solving my problem. But still very feels very boilerplatey, and doesn't solve the problem of not having permission to load that particular resource from the server Sorry for the rambling, just putting my thoughts into words. |
BetaWas this translation helpful?Give feedback.
All reactions
-
No apologies necessary - I think the rambling provides good details! I do think you've sort of found the conventional way to do this. I would offer two pieces of advice to either you or someone else who finds this thread:
Other than that, I don't know if I have a better answer to your original prompt about the conceptual difference between identifier and tree nodes as references. Maybe someone else will! |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Agreed this is a huge issue... I always end up doing something like this but it's so ugly |
BetaWas this translation helpful?Give feedback.