2

In most of the posts I've seen on StackOverflow surrounding the MVC architecture, people have said that the model should only be communicated with from the controller, and not from the view.

I'm a little confused about this, because when you Google MVC: -http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller -http://i.msdn.microsoft.com/dynimg/IC108622.gif the graphics seem to indicate that there is communication from the view directly to the model.

The specific application I'm wondering about this for is an Ajax/jQuery hierarchical select box like the one here http://kotowicz.net/jquery-option-tree/demo/demo.html. After each selection I was thinking of getting the next box's data from the model. I'm using the Codeigniter framework.

Just after some expertise around the best practice here.

tereško
  • 58,060
  • 25
  • 98
  • 150
routeburn
  • 1,156
  • 1
  • 12
  • 27
  • 2
    See http://martinfowler.com/eaaDev/uiArchs.html for a discussion of the evolution and different meanings of MVC – DNA Oct 31 '12 at 22:11
  • 3
    I'd still personally call a controller function to return the data. – Rick Calder Oct 31 '12 at 22:13
  • The what you use would be MVP, thought, to implement that pattern, you need a fully functional view. In codeigniter you have only dumb templates, which are just called "views". – tereško Oct 31 '12 at 22:14
  • This might not the exact answere you are looking for but it will somehow help you to understand the mvc architecture http://programmers.stackexchange.com/questions/140406/advantages-of-using-business-logic-in-model/140408#140408 – Code Prank Nov 01 '12 at 04:37

2 Answers2

2

When you have an XHR-centric application, it makes sense to actually have two triads of MVC. One for front-end and one for back-end. Front-end would treat the back-end MVC as just a source of data (same way as data abstraction structures in back-end's model layer treat database, cache and other forms of storage).

Basically, your "ajax application" is not view. It is should be a fully realized application. Of course, there is point in doing this if you are actually creating a large XHR-powered application.

Usually, what you see in the browser is NOT the view. It is just the response that view created. In context of the web, the user of MVC application is web-browser.


enter image description here source: wikipedia

The diagram above represents simplified flow of information in classical MVC and Model2 MVC (also known as "Web MVC"). The difference between them comes from nature of web: each time the user sends input, it expects to receive a response. This means that there is not ambiguity about timing. All the parts of MVC triad exists only till the response has been sent. There is no point for view to observe model layer.

View in you web application is actually the instance, which contains all the UI logic and is responsible for sending the response to the browser. The response, which it creates can be either a HTML (made by combining multiple templates), JSON or XML file. Or maybe just HTTP location header.

P.S. you might find this answer to be relevant for your studies.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
0

I think the image on the second link is wrong 'cause the right way should be controller comunication with the model. If the view have directly comunication with the model means that html code is mixed with php arge sentences and that is a mistake.

Mario Rojas
  • 1,313
  • 1
  • 9
  • 15