Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How to access another VM? #14

Closed
WangJiaJun8922 opened this issue Apr 30, 2020 · 4 comments
Closed

How to access another VM? #14

WangJiaJun8922 opened this issue Apr 30, 2020 · 4 comments

Comments

@WangJiaJun8922
Copy link

In many cases, business logic needs to access a value of another ViewModel, such as the selected Id to request data, how to solve ? thanks for answer

@FilledStacks
Copy link
Contributor

Anything that has to be shared between viewmodels should be in a service. Similar to how Blocs share logic between views we share logic between viewmodels using services. In general I have an information service that keeps things pertaining to all parts of the app. I set the value on there and then make it reactive so the viewmodel can react to those changes.

Reactivity is shown in the readme.

@YazeedAlKhalaf
Copy link
Contributor

I know that it is not the best way, but it worked once in an app I made.

To access another VM, just import it into that other file. For instance, View 1 uses ViewModel 1 and View 2 uses ViewModel 2 but needs a variable that is specific to ViewModel 1> The simple solution is to just import it.

It is simple but works 😅
I hope it makes sense, if not tell me.

The better way is as @FilledStacks said in this comment

Anything that has to be shared between view models should be in service. Similar to how Blocs share logic between views we share logic between view models using services. In general, I have an information service that keeps things pertaining to all parts of the app. I set the value on there and then make it reactive so the ViewModel can react to those changes.

Reactivity is shown in the readme.

@FilledStacks
Copy link
Contributor

Just to be clear I don't recommend what @YazeedAlKhalaf is saying above. It's not how it should be implemented. The long way around will make for more testable code and probably a bit less prone to introducing bugs from other viewmodels into an existing one.

@WangJiaJun8922
Copy link
Author

@YazeedAlKhalaf thanks for your solution ^_^ , i will try use services between viewmodels.
@FilledStacks thanks for make this powerful MVVM framework & shared~

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants