PHP MVC 与 jQuery/javascript 代码应该是什么样子?

发布于 2024-12-01 19:00:20 字数 369 浏览 0 评论 0原文

好吧,我已经阅读了本教程,如果我可以说: http://www.symfony-project.org/book/1_1/02-Exploring-Symfony-s-Code

实际上,我编写的代码非常相似。但我的问题是,我应该在哪里插入 jQuery 代码?我确信它应该在视图的部分,但是有什么好的例子说明我应该如何将它结合起来制作“实时”网站?

编辑:所谓的“实时”是指,例如,通过 Ajax 发送 POST 请求并获取信息等。所以,人们没有刷新。

谢谢。

Well, I've read this tutorial if I could say: http://www.symfony-project.org/book/1_1/02-Exploring-Symfony-s-Code

And, actually, I write my code very similiary. But my question, where should I insert my jQuery code? I am sure it should be in the part of the View, but are there any good examples on how should I combine it to make "live" websites?

Edit: By saying live, I mean, for example, send POST request through Ajax and get information and similar. So, no refreshes for people.

Thank you.

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

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

发布评论

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

评论(2

权谋诡计 2024-12-08 19:00:20

jQuery 作为 javascript 资源的一部分,应包含在 html.headin-place 中,具体取决于 jquery 应该做什么以及代码是否可重用于更多视图。

因此它必须是 View 的一部分,您可以选择将其设置在 layoutaction view 本身中

jQuery as a part of javascript resources should be included in html.head or in-place, depending on what should jquery do and if the code is reusable for more views.

So it has to be part of View and you're choice is to set it up in layout or action view itself

半衾梦 2024-12-08 19:00:20

如果您在每个页面上都需要 javascript,则将其添加到您的主视图中,如果不需要,则只需将其包含到特定的视图文件中。

在 codeigniter 的上下文中:
我使用 MY_Controller 扩展控制器,并将属性初始化为数组,该数组将保存动态添加到视图的脚本。
例如。

var $templateData['scripts'] = array();

然后控制器将 $this->templateData 传递给视图
并且视图按照 head 标签中控制器的指示加载额外的脚本(可用作 $scripts)。

这样您甚至可以避免在不需要时加载主 jquery 文件。
仅为需要它的控制器加载 jquery.js。

If you need the javascript on every page then add it to your master view if not then just include it to the particular view files.

In context to codeigniter:
I extend the controller with MY_Controller and initialize a property as array which would hold the scripts that are added dynamically to the view.
eg.

var $templateData['scripts'] = array();

The controllers then pass $this->templateData to the views
And the views load the extra scripts( available as $scripts) as directed by the controllers in the head tag

This way you can even avoid loading the main jquery file when not needed.
Loading jquery.js only for the controller that need it.

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