MVC - where does View 'logic' go?
Posted on 2013-12-24
I have a Menu, consisting of MenuItems, and I want to display this menu, and keep track of the "currently selected menu item".
So in the Model I create a menu and some menu items, and put the menu items in the menu, and the menu keeps track of the "currently selected menu item".
However, the Model starts making assumptions about the View: How many MenuItems the View can display. And the View code starts making assumptions about the Menu: How many MenuItems there are, and what type of MenuItem each MenuItem is.
So I start to think all this really should go in the View. The View determines what gets displayed and how it's displayed.
But then I have the View keeping track of what the "Currently selected menu item" is, and the controller asking the view what the "Currently selected menu item" is so it can decide what to do based on that information, which doesn't sound right either.
So either the Model knows a whole lot about the View, how large the screen is, where everything is positioned on the screen, what the currently selected item is, which tightly couples my Model to a particular View;
Or the View determines what gets displayed on the screen and how everything is positioned, but also ends up keeping track of the "currently selected item", and a bunch of logic migrates to the View where I don't think it belongs.
Who should keep track of the "currently selected menu item"?
If I have a Model of the screen, how big it is, and where everything is displayed, then how do I decouple that so the Model doesn't care so much about the View?