JavaScript 在 Web 应用程序的 MVC 模式中处于什么位置?
在构建 Web 应用程序时,我仍然对 JavaScript 代码在 MVC 模式中的位置感到困惑。我以为它是这样工作的。
- 模型是数据库, 获取数据所需的类 输入/输出。
- 控制器将是我编写逻辑的类 即 Java servlet,它接受 Ajax请求然后调用 数据库;
- 视图是 JSP 页面,它是 通过 Ajax 请求返回 servlet(我的控制器)
因为 JavaScript 代码是在浏览器中编译的,所以我认为它是视图的一部分,但它处理用户输入,根据这些事件发出服务器请求,然后将数据返回到页面,所以这也使得控制器的一部分?
另外,当他们引用 MVC 中的领域模型时,这意味着什么?
I'm still confused about where JavaScript code sits in the MVC pattern when building a web application. I thought it worked like this.
- The Model is the database and the
classes required to get the data
in/out. - The Controller would be the Classes where I write my logic
that is, Java servlets, which accept an
Ajax request and then make a call to
the database; - The View is the JSP page which is
returned to the Ajax request via the
servlet (my Controller)
Because JavaScript code is compiled in the browser I think of it as part of the View, but it's handling user inputs, making server requests based on those events and then returning the data to page, so would that also make in part of the Controller?
Also, what does it mean when they refer to the Domain Model in MVC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
JavaScript 将主要是与 UI 相关的问题;您的视图正在向控制器发出 ajax 请求。控制器没有发出 ajax 请求;模型也不是。就所有意图和目的而言,ajax 请求与普通请求没有任何不同。只是浏览器不会挂起,直到您返回响应为止。
JavaScript 还在客户端的上下文中执行,在服务器的权限之外,因此它应该进入视图。
JavaScript is going to be primarily a UI related concern; your view is making an ajax request to the controller. The controller isnt making an ajax request; nor is the model. For all intents and purposes, an ajax request isnt anything different than a normal request; it's just that the browser isnt hanging until your response is returned.
JavaScript also executes in the context of your client, outside the purview of your server, so it should go into the view.
MVC只是一种模式。 JavaScript 代码本身可以实现此模式,因此我不认为它适合服务器端框架模式的其他部分。查看 Backbone,了解在 JavaScript 代码中使用 MVC 的好示例。
您可以根据与服务器端代码建模类似的概念来建模 JavaScript 代码。 JavaScript 代码本身将通过服务器端应用程序的视图提供服务,但除非您只是用 JavaScript 代码添加养眼的东西(事实上并非如此),否则 JavaScript 代码实际上是它自己的实体,并且不一定适合您的服务器端 MVC 范例。
尝试将 JavaScript 代码与服务器端的任何内容分开。只需将其视为一个“附加组件”,如果在浏览器中禁用它,不会中断您的应用程序的运行。我只是添加了一些细节以实现更好的交互等。实际上如何对 JavaScript 代码进行建模取决于您,(但我强烈推荐 Backbone)
也可以在仅由数据源支持的 JavaScript 中创建丰富的前端。在这种情况下,JavaScript 将再次负责维护模型、视图和控制器。
领域模型通常仅指应用程序的业务逻辑。可以说,大脑决定了你的应用程序中实际应该发生什么。这是一种抽象概念,封装了应用程序的所有业务逻辑。
MVC is just a pattern. JavaScript code itself can implement this pattern, so I don't think of it as fitting into some other portion of your server side framework's pattern. Check out Backbone for a good example of using MVC in JavaScript code.
You can model your JavaScript code off of similar concepts that you model your server side code with. The JavaScript code itself will get served up through the view of your server side application, but unless you're only adding eye candy with JavaScript code (which you're not) then the JavaScript code is really its own entity and doesn't necessarily fit into your server side MVC paradigm.
Try to separate the JavaScript code from anything server side. Just consider it an 'add on' that, if disabled in the browser, won't break your application from running. I just add some niceties to allow for better interaction, etc. How you actually model the JavaScript code is up to you, (but I do highly recommend Backbone)
One could also do a Rich frontend in javascript backed only by a data source. In this case, once again, javascript will be responsible for maintaining models, views and controllers.
Domain model generally just refers to the business logic of your application. The brains so to speak of what should actually happen in your app. It's kind of an abstract concept encapsulating all the business logic of an app.
Nick,我在 MVC 方面的个人经验,使用 Zend 或 Spring,我认为 JavaScript 代码将被视为视图的一部分,因为 JavaScript 代码正在帮助视图并直接与视图交互。
通过 Ajax 发送和接收数据可以被视为一个请求。
Nick, my personal experience in MVC, working with Zend or Spring, I think JavaScript code would be considered as a part of the View since JavaScript code is helping View and is directly interacting with the view.
Send and receive of data through Ajax can be considered as a request.
JavaScript 代码是视图的一部分。视图是向浏览器输出的内容,虽然 Javascript 代码不会自动具有视觉外观,但它可用于修改 DOM。
当您开始谈论 Ajax 时,很容易将 JavaScript 代码视为正常流程中的其他内容,但您应该分解 Ajax 请求的过程,看看它只是另一个 HTTP 请求。
有些人会拥有一个仅用于 Ajax 请求的控制器,而其他人可能会将参数传递给描述 Ajax 请求的控制器以修改输出。
无论哪种方式,JavaScript 代码都位于视图中,您可能需要学习一些有关 MVC 设置中的 Ajax 的其他设计策略。
JavaScript code is part of the View. The view is what gets output to the browser, and while Javascript code doesn't automatically have a visual appearance to it, it can be used to modify the DOM.
When you start talking about Ajax, it's easy to see JavaScript code as something else in the normal flow of things, but you should break down the process of an Ajax request to see it's just another HTTP request.
Some people will have a controller for just Ajax requests, while others may pass an argument to a controller depicting an Ajax request to modify the output.
Either way, JavaScript code sits in the View and you may need to learn some other design strategies regarding Ajax in an MVC setting.
如果您使用 JavaScript 来处理 DOM,那么是的,它是视图的一部分。但你仍然可以在服务器端使用JavaScript,在这种情况下它可能是与业务相关的代码的一部分。
If you use JavaScript to work with the DOM so yes, it is part of the View. But you can still use JavaScript on the server side, in this case it could be part of the code related to the business.