简体   繁体   中英

How PHP MVC should look like with jQuery/javascript code?

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.

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 V iew and you're choice is to set it up in layout or action view itself

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM