vendredi 25 janvier 2019

How much view-agnostic is a ViewModel supposed to be in MVVM?

Let's imagine a very simple UI, where there's a list of checkboxes. When you click on any of them:

1) if it's not selected, it should create a workspace with this checkbox name (and more logic then is available in that workspace, but that's irrelevant to the question). The checkbox naturally then becomes selected;

2) If it's already selected, it should turn unselected and should destroy the associated workspace.

The straightaway form of implementing this is to have the View look at the state of the clicked checkbox and depending on being already selected or not do a viewModel.createWorkpace() or viewModel.destroyWorkspace(). But then that means that this logic is in the View -- I get the impression it'd be better to have it in the ViewModel.

I've read that the ViewModel for diverse reasons should not know the View. But one thing is not having a physical dependency to the View (an object reference, for instance) and another completely different is not even being aware that the View may have checkboxes, as in this example.

It gives me the impression that actually there's really not big of a point in making the View and ViewModel that much different, and in fact, the closer the ViewModel is to the View, the easier it is to bind data between the View and ViewModel.

How should I deal with this situation and why?

Thanks

Aucun commentaire:

Enregistrer un commentaire