Why use registerScrip?

why do we "Yii::app()->clientScript->registerScript" in the view when we can just


<?php $this->widget('zii.widgets.grid.CGridView', array(







<script language="javascript">




For example, if you have a component that uses javascript, then you’ll find this function helpful. You’ll don’t need to put javascript code in your views manually. Also you won’t need to delete this code if one day you’ll decide to turn off this component.

The reason, why i use clientScript to register all javascript: This javascript snippets are only required on initial page load. Later, when the grid is updated i don’t want that javascript to get included again. I also use separate partial views (_grid.php) for all my grid code, including javascript blocks. When a request for a grid update comes in from $.fn.yiiGridView.update(’#grid-id’); i can use renderPartial(’_grid’,array(‘model’=>$model)); to send as few data as possible (a.k.a. the rendered updated grid markup) back to the client.

In my opinion, Yii::app()->clientScript->registerScript has two major shortcomings:

A) Maintenence, since front-end and back-end code is mixed up, it becomes even worse when CSS is involved.

B) Time pause due to cloning large JS libraries like CKEditor, ExtJS … You can feel the back-end operation.

Personally, I prefer the traditionally simple way.

The assets dir should only have to copy the js 1 time though ;) after that it’s fast… but other problem is…

I agree about mixing serverside with js though ;) it is really a pain when writing longer scripts… One script I did was just a view with javascript (no layout and correct headers) So I can still use controller functionality in php ;) mostly needed to generate url’s.

And then I just register the url ;)

$cs->registerScriptFile( Yii::app()->request->baseUrl.’/controller/action/script’);

It might be slower but I’m willing to sacrifice that ;) and file caching should cure most of that :)

And when using3rd party script maybe some component has and old version of the script or one includes a minified and one includes a normal version of the script and you get a double script included… but I guess we should always take care of that by allowing to override some public var script => false :P