question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested Model Question

See original GitHub issue

This is a question not an issue, but I would be glad to get your comments.

If we have to model a one-to-many relation, where both models have specific actions, let’s say:

const childModel = {
  id: "",
  parent: "",
  items: {},
  addItem: action( (state, payload) => {
    state.items[payload.id} = payload;
  ),
  removeItem: action( (state, id) => {
    delete state.items[id};
  )
};

const parentModel = {
  id: "",
 children: {},
 addChild: action( (state, payload) => {
   state.children[payload.id] = {...childModel, ...payload, parent:state.id}; // :)
 })
};

This doesn’t work as it is. I am using something like this:

const parentModel = {
  id: "",
 children: {},
 addChild: action( (state, payload) => {
   state.children[payload.id] = createStore( {...childModel, ...payload, parent:state.id} );
 })
};

That allows me to write things like:

  parent.children[id].addItem({id: 1, name: "pees"})

which is nice!

However the state tree has to be computed:

Object.values(parent.children).map(child => child.getState())

which feels wrong 😕

If anyone has a similar issue solved more elegantly I would love to see it!

Thank you!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ctrlplusbcommented, Apr 14, 2020

Hmmmm, you certainly have me thinking. I will continue to do so and if I think of anything interesting I’ll feed back to you here. 🙏

1reaction
andreiQuantcommented, Mar 15, 2020

Hey @ctrlplusb,

Here is a sample showing the difference between nested models added at “design time” and those added at “run-time” :

https://codesandbox.io/s/easy-peasy-nested-1d1rw

Read more comments on GitHub >

github_iconTop Results From Across the Web

Newest 'nested-models' Questions - Cross Validated
Is it safe to enter the consulate/embassy of the country I escaped from as a refugee? Why don't courts punish time-wasting tactics? What...
Read more >
F-tests and Nested Models - Rose-Hulman
Nested Models : A core concept in statistics is comparing nested models. ... The fundamental question with respect to this model is whether...
Read more >
Nested Model, ANOVA and Factors - Statistics How To
What is a Nested Model? Very simply, “nested” means that one model is a subset of another. For example, take a model for...
Read more >
Model Selection Tests For Nested And Non-nested ...
The nature of the model selection test depends on whether the two models A and B are nested versus non-nested. If model B...
Read more >
198 questions with answers in NESTED | Science topic
Im struggling to find what statistical test to use, and how to set up the data. Do I do individual tests for each...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found