mobxjs / mobx-state-tree

Full-featured reactive state management without the boilerplate
https://mobx-state-tree.js.org/
MIT License
6.98k stars 641 forks source link

Getting parent of a reference (not a real node) #1284

Open terrysahaidak opened 5 years ago

terrysahaidak commented 5 years ago

Question

I'm building an application with a lot of references to the same model. So I had to store it separately in some sort of entities store normalized. But it makes it impossible to fetch the parent from reference because both getParent and getParentOfType returns me the Entities store but not the store I'm referencing from.

Is it really possible to get the store which has a reference to my model?

Here is a little example: https://codesandbox.io/s/ox183w4o75

xaviergonz commented 5 years ago

Not at the moment unfortunately, since right now as soon as a reference is accessed what you get is actually the target node.

I created a few days ago a RFC to rethink the references API https://github.com/mobxjs/mobx-state-tree/issues/1282

I think that by using that API it would be possible, since then the references would not be "dereferenced" automatically

terrysahaidak commented 5 years ago

Thanks, @xaviergonz, sounds really cool.

As a workaround currently I'm using recursive references - I'm adding a reference to my Application model pointing to Job model which has reference to that Application model. Sounds really bad, but at least it works. Looking forward to a new API - it's the only thing I'm missing right now from MST.

Both you and @mweststrate did great work with MST. Thanks!