关于MVC架构的问题

发布于 2024-09-04 16:07:10 字数 601 浏览 3 评论 0原文

我开始编写一个相当复杂的 Web 应用程序,结果变得一团糟。 所以我想我应该尝试以更好的方式组织它。 MVC 似乎很合适。 我以前从未使用过 MVC,并且对它进行了研究,我试图巩固对它的更好的理解(我的问题显然反映了我认为到目前为止我学到的东西)。 我的问题稍微面向 JavaScript:

  1. 什么对象应该发出“AJAX”请求?控制器还是模型? (分离——模型应该只存储/操作数据,它应该不关心/知道数据来自哪里,还是应该由它来获取数据?)

  2. 模型是否应该调用视图函数,为它们提供数据参数还是视图应该查询(引用)自身内部的模型? (记住分离原则,“视图不应该关心/知道它从哪里获取数据”——这是正确的吗?)

  3. 一般来说,视图应该“知道”模型的存在,反之亦然?控制器是唯一将它们粘合在一起的东西还是这根本不正确? (我真的怀疑这种说法通常是正确的)

我很有可能想将其移植到桌面/移动应用程序中,所以我想以一种允许我完成该任务的方式分离组件,替换当前的数据源、HTTP 请求、数据库访问以及替换视图。

也许我询问的每一种方法仍然是“有效的”MVC,这取决于我的选择。我知道没有什么是一成不变的,我只是想在我的脑海中有一个(更好的)总体想法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画离情绘悲伤 2024-09-11 16:07:10
  1. 假设您指的是真正的 Ajax(即异步 Javascript 调用),我认为这实际上是视图的责任。 Javascript 是客户端,而控制器和模型是服务器端。
  2. 模型和视图不应该有任何交互。控制器是信息网关,是控制应用流程的粘合剂。
  3. 如上所述,视图不应与模型进行交互。正如您所说,控制器是中间人。对于大多数框架,都有一些方法可以使管道短路,但应该避免使用。
  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.
滿滿的愛 2024-09-11 16:07:10

1 - 视图发出 AJAX 请求,并且已由控制器处理。控制器知道要请求什么数据、何时以及如何将其打包以供视图使用,但不知道任何底层存储机制(例如,SQL、XML、NoSQL...);这是由模型抽象出来的。
2 - 两者都否。视图和模型不应该互相了解。
3 - 第一个不,第二个是。

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.

随波逐流 2024-09-11 16:07:10

我自己走了以下路径:

  1. 模型中所有与数据相关的处理,包括。 AJAX 请求和处理程序
  2. 控制器处理附加到模型和视图的侦听器
  3. 我确实喜欢控制器侦听和互连的概念。我确实认为这使事情变得更加简单

查看 Model-View- jQuery 中的控制器教程介绍了这种方法。它使我的生活变得更加轻松,并在很大程度上改进了代码。易于重构和重用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文