在 MVC 应用程序中使用 Javascript
我是一门课程的助理,学生们在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
想必“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.
在客户端执行的 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.
我倾向于路由所有需要执行 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.
我在 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.