-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connecting two view models to a single view #610
Comments
This is not possible. A single ViewModel can be used by multiple Views but each View can only have a single ViewModel. |
But you can inject other ViewModels into the one ViewModel of the View and pass properties through the main ViewModel so that indirectly the View is working together with multiple ViewModels. How can I achieve this elegantly? The problem I have now is: I have a lot of functions in a single interface, and I can't write them all on the same viewmodel. I need a form similar to fragments. |
The injection itself isn't depending on mvvmFX but depends on your dependency-injection framework. I'm not sure what you mean with "fragments". I have the feeling that your issues are not a mvvmfx issue but a more general software architecture/design issue. Having lots of functions in a single interface might be not a good idea (possibly a violation of interface segregation principle). The best way that OOP provides is composition: Implement the functions that belong to each other in one or multiple classes, create/inject instances of these classes in your ViewModel to use the functions. But the actual design highly depends on your specific use-case. |
Sorry, I may not express it clearly. What I mean is that I want a view to be divided into multiple fragments, and then there are multiple viewmodels to manage each fragment, that is, one view corresponds to multiple viewmodels. |
Ok, I understand. This is possible with mvvmFX the way I explained above. In mvvmFX, normally there is a one-to-one connection between View and ViewModels. This is the usual case but it's also possible to have one ViewModel which is used by multiple Views. A small example: // before
// after
This way you can re-use the "PersonViewModel" in the example. The intersting point is, how you get the instance of the other viewModel. This is were dependency injection comes into play but this is beyond mvvmFX. mvvmFX doesn't provide general DI but only for objects managed by the framework itself. |
I am trying to find a way to bind two view models to a single view, however, I cannot find a way to achieve my goal.
The text was updated successfully, but these errors were encountered: