Splitting the concept of references into two? (Tree reference and Identifier Reference) #1995
Replies: 1 comment 2 replies
-
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";
const Todo = types.model({
id: types.identifier,
title: types.string
});
const TodoStore = types.model({
todos: types.array(Todo),
selectedTodo: types.reference(Todo) // A reference to a node in the tree
});
const t1 = Todo.create({
id: "1",
title: "First"
});
const t2 = Todo.create({
id: "2",
title: "Second"
});
const store = TodoStore.create({
todos: [t1, t2],
selectedTodo: "1"
});
console.log(store.selectedTodo); // {id: "1", title: "First"}
const snapshot = 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? |
Beta Was this translation helpful? Give feedback.
-
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
safeReferences
partially solves this problem, safe references were designed to remove the data from the tree/array/map when they are not found. Which reinforces the idea that references are designed as client side references, not server references.Let's imagine that the server response returns the following Todo response:
Current problems in using
authorId
as a reference is:undefined
A way that I've been using to bypass this issue is to make
authorId
a property, and create aview
that resolves the reference to the User model. However that feels a bit like a hack and a waste of the references potential. This also solves the semantics problem (authorId
returns the id, not the object), and allows for easy access to the referenceWhat 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.
In this case
todo.authorId
would return"3342"
, keeping the consistency of the name, and allow fortodo.author
safe reference to returnundefiend
if the node is not in the tree. This would work almost identically to many databases, whereidentifierReference
would be analogous to a foreign key.Are there any other ways of solving this issue?
Has anyone found a good way to use references with data passed from the server that might not yet (or even never - because of permissions) be in the state tree.
Beta Was this translation helpful? Give feedback.
All reactions