sialcasa / mvvmFX

an Application Framework for implementing the MVVM Pattern with JavaFX
Apache License 2.0
489 stars 105 forks source link

Injecting 2 ViewModels #605

Closed svedie closed 4 years ago

svedie commented 4 years ago

Hello lestard,

I have implemented a toolbar with default buttons for a dataset view. But some views have additional buttons. Here I extend the View and ViewModel from the default toolbar, add the specific implementation, but I get this error:

Caused by: java.lang.RuntimeException: The View <class ToolbarKontoView> may only define one viewModel but there were <2> viewModel fields with the @InjectViewModel annotation!

Is there an way to inject the extended ViewModel instead the one from default toolbar? Or do I need to "duplicate" the code for the extended toolbar?

Greetings

manuel-mauky commented 4 years ago

Sorry for the late reply. Reusing code with inheritance is not a good idea and mvvmFX is not intended to be used with inheritance. Instead you should use composition. If you have a reusable View/ViewModel you can use an instance of this View/ViewModel in your other Views/ViewModels. This way you can reuse several things in a single View/ViewModel which wouldn't be possible with inheritance (as Java doesn't support multi-inheritance).

svedie commented 4 years ago

Hi lestard,

after some implementation tests I have discarded this idea and did a redesign of the toolbars.

Thank you for your answer.