0

I have a javascript file accessed from a url like "/js/someFile.js". However I would like Yii to render the javascript file when it has been requested so I can include relevant data in it e.g. a parsed list from a database table.

I understand that one solution is to simply have the file static, and request the data from the server on initialization, however I was hoping to not do this.

Does anyone know how to achieve this without putting the JS inline in the current view? Or any reason why this is a terrible idea?

1
  • Thanks to all who answered. Commented Apr 23, 2013 at 0:37

2 Answers 2

1

You could render a view as Twisted1919 showed. But i indeed think it's not a good idea to render javascript through Yii. Your question already implies, that the javascript file is almost static. So treat it as a static file so that your webserver can serve it which is much faster. And it's easier to cache this file on the clientside.

Then as you suggested try to write it in a way, that you only have to pass the dynamic parts as parameters to your javascript. You don't have to request that data from the server as you implied, though. You can add the configuration inline to your page:

<?php
$data = json_encode(array(
    'userId'    => Yii::app()->user->id,
    'whatever'  => $someThing,
));
Yii::app()->clientScript->registerScriptFile('my.js');
Yii::app()->clientScript->registerScript(
    'initJs',
    "myJs.init($data)", 
    CClientScript::POS_READY
);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

This is a useful little part of Yii's asset management that I didn't know about, thanks for bringing it to my attention.
1

You can have a special controller called js or javascript and call it instead of calling static js files :

class JsController extends Controller{
   public function init() {
       header('Content-Type: application/javascript');
   }

   public function actionSome_action(){
      $this->render('some_action_view_file');
   }
}

You can even define a rewrite rule for it so that you can have the .js extension for your url.

Next, from your controllers, say:

class SiteController extends Controller {
   public function actionIndex() {
      // register the script
      Yii::app()->clientScript->registerScriptFile($this->createUrl('js/some_action'));
      // do other things here...
   }
}

2 Comments

If this this is something I'm doing a lot, then you're right, as this is the most flexible and extensible way to achieve this. I accepted the other answer not because of "correctness", but due to my particular question. Thanks for your help.
@andy - no problem at all, glad you found a solution that works for you, this is all that matters. Maybe in the future my idea will also help you ;) It's good to have alternatives :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.