I'm trying to find a good organization practice of my javascript (jquery) files when using CodeIgniter. I do a lot of ajax sites and many of my views are loaded in various controllers, I'd like to only load the necessary javascript for those views without duplicate code. How do y'all handle this issue?
+1
A:
Try using phil sturgeon's template library
http://github.com/philsturgeon/codeigniter-template/blob/master/libraries/Template.php
add a method called add_js and have it put itself as a variable just before build. Call this add_js method in each controller (in each method if needed) and define the js you want to be included.
then in your view do
<? foreach($js as $item): ?>
<script src="<?=$item?>"></script>
<? endif; ?>
Tom Schlick
2010-03-06 04:11:35
I haven't heard of this and will have to look into it. If it works out i'll check this as the answer, but right now I'd like to understand the concept behind it first.
ocdcoder
2010-03-06 21:40:56
basically it just abstracts all the things your likely to need into one nice library so you dont have to think of solutions manually for each page.
Tom Schlick
2010-03-06 23:54:24
i've decided this is acceptable enough for me to consider as the answer :-) thanks for the help!
ocdcoder
2010-03-07 08:05:35
A:
You could just create a js
folder at the same level as the system
folder. Then just include your JavaScript files from there, as you would on any web page.
Justin Ethier
2010-03-06 04:17:48
i do that now, but I'm having trouble deciding what js files to create. Should I split them up by views or by pages? Or possibly create some script to only include the js needed per page.
ocdcoder
2010-03-06 21:28:04