-
-
Notifications
You must be signed in to change notification settings - Fork 254
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
Comments
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. |
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 😅 The better way is as @FilledStacks said in this comment
|
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. |
@YazeedAlKhalaf thanks for your solution ^_^ , i will try use services between viewmodels. |
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
The text was updated successfully, but these errors were encountered: