Solved

MVC - where does View 'logic' go?

Posted on 2013-12-24
7
427 Views
Last Modified: 2014-01-02
Model-View-Controller.

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?
0
Comment
Question by:deleyd
  • 3
  • 2
  • 2
7 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 39738479
Your controller should determine what view to display, not what that view displays. Your model (as in the "M" in MVC) should know nothing about your view (e.g. what HTML controls it uses, styles, etc.). It is used to represent data objects within your problem domain. The view uses the model in order to present the data to the user in a meaningful display.

Who should keep track of the "currently selected menu item"?
IMO, you would pass back the selected item (via a hidden field, perhaps) to the controller; let the controller do it's thing, and then pass back the current selection as a part of the model to the view. The view can determine if it wants to still show that item as selected (most likely it does). Generally speaking, your views will tend to be very dumb.
0
 

Author Comment

by:deleyd
ID: 39738505
So if the user presses the DOWN arrow to move to the next MenuItem, who handles that?

The Controller gets the message that the user pressed the DOWN arrow key. Overall we know this means we should move to the next "currently selected menu item".

Someone has to know what the currently selected menu item is.
Someone has to determine what the "next" menu item is.
Somewhere is stored a "list" of menu items for this particular view.

The data is still stored in the Model. However, "menu items" is sort of a metadata thing.

If "menu items" are a View thing, where the View gets to decide what to display, then the View is no longer dumb but gets complicated, keeping track of of what the currently selected item is, defining what the next selected item will be.

But if I have a model for the screen, then that model is specific to that screen.

Perhaps there are two models? One for the data, another for the specific screen? In which case do I create different screen models for different screens, one for large screens, one for small screens, which in turn can display many menu items, or just a few menu items?

Then I have a "screen model" asking the "data model" for data, formatting that data for display on that particular screen? So "screen model" is tightly dependent on a particular screen?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39738507
Are you writing a web app, or a "windows" app?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:deleyd
ID: 39738917
Windows Forms.

(Actually, unmanaged C++ application, which we test with Windows Forms.)
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 400 total points
ID: 39740196
the menu should be part of the controller as kaufmed says. the reason is that that the menu may contain items which rule the visual aspects (of this view or other views) , but it also may contain items which control the model part, for example which file should be used for to store the model. another reason is that menu items may be offered in different ways, for example as element of a drop-down menu or as a toolbar button or as a shortcut. obviously some of these elements may not be handled by the view. shortcuts, which relate to model functions, normally should work regardless of the view and the current model (if any). also toolbar buttons rarely are child controls of the view but are part of some kind of frame window which is parent of the view.

Someone has to know what the currently selected menu item is.
Someone has to determine what the "next" menu item is.
Somewhere is stored a "list" of menu items for this particular view.
you can implement menu items such that actually no one knows the functioniality it invokes beside of the target that implemented the handler. when a menu item was clicked it is a WM_LBUTTONDOWN message sent to the window which displays the drop-down menu. this window catches the message and transforms it to a new message sent to the parent of the menu - normally a frame window, where the id of the menu item was passed as additional information in the message. at this point you could implement a mechanism where you stored a function pointer for each menu item id and where this function now was called. you may say that this 'list' where a menu item id is associated to a function (pointer) is the list you were talking of, but actually the list already could be at the target object, for example at the view side, and not at the controller side, when the controller decided to not handle the message itself but pass redirect to (defined) other targets. a possible target also is the model or even the application. while it is true that each menu has a list of its menu items, it is also a fact that this list doesn't contain much more than an identifier for each item, and some piece of text or icon bitmap. so it is very "dumb" and does not handle more than basic mouse operations.

Sara
0
 

Author Comment

by:deleyd
ID: 39742912
If the menu is defined in the controller, then does the controller know about the specifics of the screen size? Such as how many lines of text the screen can display?

(Assume an old fashioned text based screen.)

Then that controller would be specific to whatever specific screen size it was written for.

Then, if later I have a new screen size, I need to write a new controller for that new screen size? And I rewrite the menus all over again?
0
 
LVL 33

Accepted Solution

by:
sarabande earned 400 total points
ID: 39745701
If the menu is defined in the controller, then does the controller know about the specifics of the screen size?
the controller is responsible for the menus and their contents. it is not responsible for the visual aspect of showing a menu and it is not responsible for the user interface to such a menu.

Sara

p.s.
an old-fashioned text menu is not dependent on the size of the screen. it would use the "scrolling" properties of the "console".
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best book to learn C++ 4 70
java  and programming certification ? 4 86
Header of docx file 17 97
Which Linux flavors will this run on? 6 76
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now