Loading core in JavaScript files

#1
[eluser]mariogl[/eluser]
Hi. I'm trying to migrate my site to CodeIgniter. I have doubts about how to implement my js files.

In my previous system, I include separate js files (common.js, forms.js, lists.js) depending on which controller is being called. Actually, most of them are php files (forms.js.php) that generate javascript code, because I use some php classes and constants inside. Each js.php file has a php initialization code at start, which is the same one that use all the controllers.

Now, with CodeIgniter, I don't know which is the best way to do this. What I want is that those javascript files (included by <script src=...) load the core and all my initial stuff, that I have put in MY_Controller.

I've searched, I've read about render js files as views, but I can't see it clear.

Thanks for any help.

#2
[eluser]Twisted1919[/eluser]
The simplest and cleanest solution i think would be like so :
Create a controller that will serve your javascript files only, something like assets .
Then, depending on the param passed to this controller, load a view with php + js code and put the javascript headers so that the browser will see it like javascript .
In the head of your templates, you will have something like :
<s_cript src="http://mydomain.com/assets/my-js-view-file-that-has-php-within"></s_cript>
The assets controller will load the view you specify and you're good to go .
In this way, in your view you can specify and CI code and it will work .

This is just an idea that worth trying.

#3
[eluser]mariogl[/eluser]
Thanks, Twisted1919. So the solution is treating the js code as a view, I understand.

I'll try with this, thanks again.

#4
[eluser]cahva[/eluser]
One warning when using js(or whatever you call from controller). If you use flashdata from session library, those requests will erase flashdata(which are meant to live to the next request) also. You have to use keep_flashdata from those controllers to keep them alive for the "normal" requests.

Anyway, try to avoid using PHP to output JS (or CSS or whatever) as that will put a little more stress to your server consuming more memory than normal request w/o PHP in the middle. If its not "dynamic" js, its just better to save it to reqular js file and serve it as such.

#5
[eluser]mariogl[/eluser]
[quote author="cahva" date="1289943260"]One warning when using js(or whatever you call from controller). If you use flashdata from session library, those requests will erase flashdata(which are meant to live to the next request) also. You have to use keep_flashdata from those controllers to keep them alive for the "normal" requests.[/quote]

Ok.

[quote author="cahva" date="1289943260"]Anyway, try to avoid using PHP to output JS (or CSS or whatever) as that will put a little more stress to your server consuming more memory than normal request w/o PHP in the middle. If its not "dynamic" js, its just better to save it to reqular js file and serve it as such.[/quote]

But I have some js files that use "dynamism" (e.g. texts filtered by my translation class, pieces of code that execute conditionally). I suppose I have no chance, have I?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.