views:

60

answers:

3

I started coding a considerably complicated web application, and it became quite a mess. So I figured I'd try to organize it in a better way. MVC seemed appropriate. I've never used MVC before, and researching about it I'm trying to consolidate a better perception of it (and my questions obviously reflect what I think I've learned so far). My questions are slightly JavaScript oriented:

  1. What object should make "AJAX" requests? The Controller or the Model? (seperation -- should the Model just store/manipulate the data, should it not care/know where the data came from, or should it be the one fetching it?)

  2. Should the Model call View functions providing them with data as arguments or should the View query (reference) the Model within itself? (seperation principles in mind, "the View shouldn't care/know where it gets the data from" -- is that correct?)

  3. In general, should the View "know" of the Model's existence, and vice-versa? Is the Controller the only thing gluing them together or is that simply incorrect? (I really doubt that statement is generally correct)

There's a good chance I'd want to port this into a desktop/mobile application, so I would like to seperate components in a way that will allow me to achieve that task, replacing the current source of the data, HTTP requests, with DB access, and replacing the View.

Maybe every approach that I've asked about is still "valid" MVC and it's just up to me to choose. I understand that nothing is set in stone, I'm just trying to have a (better) general idea in my head.

+1  A: 

1 - The View makes the AJAX request, and it has handled by the controller. The controller knows what data to ask for, and when, and how to package it up for the view, but does not know anything of the underlying storage mechanism (e.g., SQL, XML, NoSQL...); that is abstracted out by the model.
2 - No to both. The View and Model should not know of each other.
3 - No to the first, yes to the second.

RedFilter
The data received from the server by the AJAX requests needs to be saved in the Model. Why, and how, would the View make AJAX requests? It doesn't make any sense to me.Maybe I should've mentioned it, but the responses to the AJAX requests do not contain HTML, but JSON data that needs to go into the Model.
ah123
@ah123: `AJAX` requests are made by the client (the browser), and are typically `GETs` or `POSTs`. These are received by the controller, which can persist data, generate new `HTML` or `JSON`, etc., and then return the result back to the client. The big advantage is allowing the client to send/receive data with the server without requiring a full page refresh, which is much more bandwidth efficient, and provides more of a desktop application feel.
RedFilter
Yup, this isn't my first web application, I'm familiar with the HTTP protocol and the reasons you provided are the reasons why I use "AJAX". I've only got one page load.So basically, the Model takes care of the so-called "underlying storage mechanism", including AJAX requests in that case, and the View gets data from the Model BY THE CONTROLLER.
ah123
@ah123: The Model does **not** take care of the `AJAX` request; the Controller does, and if it needs to interface with the Model, it will, but this is not always necessary.
RedFilter
+2  A: 
  1. Assuming you mean true Ajax (that is, an asynchronous Javascript call), I'd argue that it's really the responsibility of the view. Javascript is client-side while your controller and model are server-side.
  2. The model and view should have no interaction whatsoever. The controller is the information gateway and the glue that controls the application flow.
  3. As mentioned above, the view should have no interaction with the model. As you state, the controller is the middle man. With most frameworks, there are ways to short circuit that conduit, but they should be avoided.
Rob Wilkerson
A: 

I've gone down the following path myself:

  1. All data-related handling in a Model, incl. AJAX requests and handlers
  2. Controller handles listeners that are attached to both Model and the View
  3. I do like this concept of a controller listening and interconnecting. I do think this makes things a lot simpler

Check out Model-View-Controller in jQuery tutorial for this approach. It has made my life a lot easier and improved the code in quite a way. Easy to refactor and reuse.

Ain