Questions about MVC Architecture

I started coding a significantly sophisticated web application, and it got pretty messy. Therefore, I decided that I would try to organize it better. MVC seemed appropriate. I have never used MVC and studied it before. I am trying to consolidate a better perception of this (and my questions obviously reflect what I think I have learned so far). My questions are a bit JavaScript oriented:

  • Which object should execute AJAX requests? Controller or model? (seperation - should the Model just store / manipulate the data, should it not care / know where the data came from, or should it be selected?)

  • If the function of calling the model calls the functions that provide them with data as arguments, or should the request for the request (link) of the model in itself? (principles of separation in mind: "The view should not care / know where it gets the data from" - is that right?)

  • In general, should one “know” about the existence of the Model and vice versa? Is the only controller sticking them together or just the wrong one? (I really doubt the statement is generally true)

It is a good possibility that I want to transfer this to a desktop / mobile application, so I would like to separate the components in such a way that it would allow me to complete this task, replacing the current data source, HTTP requests, with access to the database and replacing the view.

Perhaps any approach I requested is still a "valid" MVC, and it is up to me. I understand that nothing has been done in stone, I'm just trying to have (better) a general idea in my head.

+4
source share
3 answers
  • Assuming you mean true Ajax (i.e. asynchronous Javascript call), I would say that this is really the responsibility of the submission. Javascript is the client side, while your controller and model are server side.
  • The model and presentation should not have any interaction. A controller is an information gateway and glue that controls the flow of an application.
  • As mentioned above, the view should not have any interaction with the model. As you say, the controller is the average person. With most frameworks, there are ways to short circuit this conduit, but they should be avoided.
+2
source

1 - The view makes an AJAX request and is processed by the controller. The controller knows what data to request and when and how to pack it for presentation, but does not know anything about the basic storage mechanism (for example, SQL, XML, NoSQL ...); which is abstracted by the model.
2 - No for both. View and model should not know each other.
3 - No to the first, but to the second.

+1
source

I myself went the following way:

  • All data processing in the model, incl. AJAX Queries and Handlers
  • The controller processes listeners that are connected to both the model and the view.
  • I like this concept of a controller that listens and connects. I really think it makes things a lot easier.

Check out Model-View-Controller in jQuery for this approach. This greatly facilitated my life and improved the code. Ease of refactoring and reuse.

0
source

Source: https://habr.com/ru/post/1312021/


All Articles