Repository navigation
Splitting the concept of references into two? (Tree reference and Identifier Reference) #1995
Replies: 2 comments 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? |
|
Agreed this is a huge issue... I always end up doing something like this but it's so ugly |
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
safeReferencespartially 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:
{ "id": 2, "tittle": "hello world", "authorId": "3342" }Current problems in using
authorIdas a reference is:undefinedA way that I've been using to bypass this issue is to make
authorIda property, and create aviewthat 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 (authorIdreturns 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.authorIdwould return"3342", keeping the consistency of the name, and allow fortodo.authorsafe reference to returnundefiendif the node is not in the tree. This would work almost identically to many databases, whereidentifierReferencewould 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.
All reactions