Solved

MVC - where does View 'logic' go?

Posted on 2013-12-24
7
420 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 74

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 74

Expert Comment

by:käµfm³d 👽
ID: 39738507
Are you writing a web app, or a "windows" app?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:deleyd
ID: 39738917
Windows Forms.

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

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 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Using YubiKey with REST API application 2 82
noX challenge 17 76
wordappend challenge 8 85
When i run adoquery my application freezes 26 100
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

760 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

23 Experts available now in Live!

Get 1:1 Help Now