PHP MVC 与 jQuery/javascript 代码应该是什么样子?
好吧,我已经阅读了本教程,如果我可以说: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery 作为 javascript 资源的一部分,应包含在
html.head
或in-place
中,具体取决于 jquery 应该做什么以及代码是否可重用于更多视图。因此它必须是
V
iew 的一部分,您可以选择将其设置在layout
或action view
本身中jQuery as a part of javascript resources should be included in
html.head
orin-place
, depending on what should jquery do and if the code is reusable for more views.So it has to be part of
V
iew and you're choice is to set it up inlayout
oraction view
itself如果您在每个页面上都需要 javascript,则将其添加到您的主视图中,如果不需要,则只需将其包含到特定的视图文件中。
在 codeigniter 的上下文中:
我使用 MY_Controller 扩展控制器,并将属性初始化为数组,该数组将保存动态添加到视图的脚本。
例如。
然后控制器将 $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.
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.