开发者

Questions about the MVC architecture

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 开发者_如何学JAVAshould 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. 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜