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.

[Feature Request] Support for nested objects in ViewModel?

See original GitHub issue

I’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 values to undefined and set taking an object representing the data entry and setting all the corresponding values.

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:closed
  • Created 7 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
JabXcommented, Oct 7, 2016

This is the solution I came up with:

function createViewModel(model: DataEntry) {
    const viewModel = observable(toJS(model));

    const reset = () => viewModel.set(toFlatValues(model));

    viewModel.reset = action(reset);
    viewModel.submit = action(() => _.merge(model, viewModel));
    viewModel.subscribe = () => {
        if (!viewModel.isSubscribed) {
            const disposer = autorun(reset);
            viewModel.unsubscribe = () => {
                disposer();
                viewModel.isSubscribed = false;
            };
            viewModel.isSubscribed = true;
        }
    };

    viewModel.subscribe();
    return viewModel;
}

toFlatValues() is a method I use to recursively transform my {$metadata, value} objects into value, just like toJS() 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 (in componentWillUnmount() in my case). But this allows to control the interaction model -> viewModel, which might come handy someday.

0reactions
mweststratecommented, Nov 21, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

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