[Feature Request] Support for nested objects in ViewModel?
See original GitHub issueI’ve read the source and I understand perfectly how it works and why nested objects aren’t supported by createViewModel
, but what would it take to add support for that?
I’m creating ViewModels for objects like this
type DataEntry = {
[key: string]: {
$metadata; {}
value: any
}
} & {
clear(): void
set(data: {}): void
}
With clear
setting all value
s to undefined
and set
taking an object representing the data entry and setting all the corresponding value
s.
Without support for nested objects I can’t use either methods or set a value
directly, with severely limits my use of the ViewModel.
I’m totally open to work on this myself, but I’d like to make sure that it makes sense and that it’s doable before jumping in.
Thanks.
Issue Analytics
- State:
- Created 7 years ago
- Comments:13 (4 by maintainers)
Top Results From Across the Web
Remote ViewModel validation of nested objects not working
This is the recommended way of dealing with remote validation of objects and might be a little easier to think about than using...
Read more >Design Patterns - Problems and Solutions with Model-View ...
In this article I'll explain how the ViewModel works, and discuss some benefits and issues involved in implementing a ViewModel in your code....
Read more >Create ViewModels with dependencies - Android Developers
Following dependency injection's best practices, ViewModels can take dependencies as parameters in their constructor.
Read more >Bind to nested (navigation) properties in complex objects
The parameterless constructor of the main view-model can instantiate the nested objects so they are not null . You can use an EditorTemplate...
Read more >Stop using MVVM for SwiftUI | Apple Developer Forums
When you define the model as POJOs (simple structs) you end with many logic inside the ViewControllers / ViewModels. VC and VM should...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is the solution I came up with:
toFlatValues()
is a method I use to recursively transform my{$metadata, value}
objects intovalue
, just liketoJS()
does for observables.The only pain point is that I kinda have to handle the model reactions manually, meaning that I have to call
unsubscribe()
before I destroy my viewModel (incomponentWillUnmount()
in my case). But this allows to control the interaction model -> viewModel, which might come handy someday.I do see the use case for this feature, but I think mobx-state-tree will in the end provide a more generic solution to this problem. Therefor closing this issue for now.