Yii Framework Forum: Why use registerScrip? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Why use registerScrip? Rate Topic: -----

#1 User is offline   Joe Storm 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 159
  • Joined: 17-April 10

Posted 11 July 2010 - 06:53 AM

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">


#2 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 11 July 2010 - 09:48 AM

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.

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,017
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 11 July 2010 - 12:27 PM

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.

#4 User is offline   allankliu 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 09-July 10

Posted 15 July 2010 - 05:32 PM

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.

#5 User is offline   mech7 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 220
  • Joined: 26-March 09

Posted 15 July 2010 - 10:57 PM

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

Share this topic:

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users