在 MVC 应用程序中使用 Javascript

发布于 2024-09-29 03:39:52 字数 294 浏览 5 评论 0原文

我是一门课程的助理,学生们在 Play! 的帮助下实现了一个网络应用程序(这是一个类似于 stackoverflow 的问答网站)。框架。这是一个基于 Java 的框架,依赖于 MVC 模式。模型和控制器是用 Java 编写的,视图是用 HTML / CSS 编写的,以及框架提供的一些扩展来访问模型的信息。

最近出现的问题是,可以在视图中使用多少 javascript 来更改模型中的某些内容(例如投票、评论等),以便不会过多违反 MVC 模式。或者是否应该避免所有实际更改模型中数据的 Javascript 函数,而是通过控制器进行路由?这有最佳实践吗?

I'm an assistant in a course where the students implement a web app (It's a Q&A site similar to stackoverflow) with the help of the Play! framework. This is a Java-based framework that relies on the MVC Pattern. The Model and Controller are written in Java where the view is written in HTML / CSS and some extensions that the framework provides to access informations of the model.

The question that came up recently is how much javascript can be used in the view to change something in the model (e.g. to vote, to comment, etc.) so that the MVC Pattern isn't violated too much. Or should all Javascript functions that actually change data in the model be avoided and instead be routed through the controller? Is there a best practice for this?

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

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

发布评论

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

评论(4

无人问我粥可暖 2024-10-06 03:39:52

想必“javascript”你的意思是更多AJAX? (而不是输出的 JS 修改......在这种情况下它只是视图的扩展)。您的 AJAX 没有理由不能/不应该像任何其他组件一样通过 MVC 进行路由。

当然,这可能会导致对原始 MVC 设计的更改,但也许这是课程中可以接受的部分。当然,当使用 AJAX 时,如果数据以 JSON 而不是 HTML 片段的形式提供,您的视图渲染可以由嵌入现有视图中的 JS 来处理。

Presumably by "javascript" you mean more AJAX? (Rather than JS modification of output... in which case it's just an extension of the view). There's no reason why your AJAX can't/shouldn't be routed through your MVC like any other component.

Of course that may precipitate changes to the original MVC design, but perhaps that's an acceptable part of the course. Of course when using AJAX your view rendering could handled by JS embedded in the existing view - if data's provided as JSON rather than HTML fragments.

独夜无伴 2024-10-06 03:39:52

在客户端执行的 JavaScript 代码怎么可能更改“模型中的数据”?您只是更改客户端对象的 JavaScript 表示中的属性,还是以某种方式从 JavaScript 调用服务器端方法(即调用 servlet)来更改数据?

如果是后者,那么这违反了 MVC——来自客户端的所有请求都应该通过控制器。

How is it possible that JavaScript code, executed on the client, can change "data in the model"? Are you merely changing properties in JavaScript representations of the objects client-side, or are you somehow invoking server-side methods from the JavaScript (i.e. calling into servlets) to change the data?

If it's the latter, then this is a violation of MVC - all requests from the client should go through the controller.

缘字诀 2024-10-06 03:39:52

我倾向于路由所有需要执行 CRUD 操作以将 GET 或 POST 请求发送到控制器中的函数的 AJAX 调用,并让它处理与模型的通信。

这就是 HTML/CSS 视图处理数据的方式(即,通过控制器从模型获取数据,并向控制器发送 GET 和 POST 请求),并且由于 JS 是客户端,因此不应对其进行任何区别对待。

I tend to route all AJAX calls that need to do any CRUD operations to send a GET or POST request to a function in the controller, and let it handle communicating w/ the model.

This is how HTML/CSS views process data(i.e., getting it from the model, via the controller, and sending GET & POST requests to the controller) and since JS is client-side, it shouldn't be treated any differently.

昔梦 2024-10-06 03:39:52

我在 CakePHP MVC 应用程序中过度使用 ajax。在这种情况下,除了将 html/javascript 页面传送到浏览器的静态控制器之外,控制器操作由 javascript 调用。

I'm excessively using ajax in my CakePHP MVC app. In this case, the controller actions get only called by javascript, beside one static controller which delivers the html/javascript pages to the browser.

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