Yii Framework Forum: CJuiWidget::registerScriptFile - Yii Framework Forum

Jump to content

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

CJuiWidget::registerScriptFile Rate Topic: -----

#1 User is offline   jamesmoey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 17-July 09

Posted 25 March 2010 - 11:09 PM

Is there any reason why the fourth position parameter on registerScriptFile method is default to CClientScript::POS_END.

By default, the position parameter in registerScriptFile method in CClientScript is always self::POS_HEAD.

I understand putting the all script file at the end, help with the page rendering and it is a common practise on in Javascript world. But it will cause problem with ajax request in Yii.

Currently, to return a page from ajax request. We have to use renderPartial and then do a Yii::app()->clientScript->renderBodyEnd() to render javascript on that page without including the core file again.

For example,

  protected function beforeAction(CAction $action) {
    if (Yii::app()->request->getIsAjaxRequest()) {
      $this->layout = false;
      Yii::app()->clientScript->enableJavaScript = false;
    }
    return true;
  }

  protected function afterAction(CAction $action) {
    if (Yii::app()->request->getIsAjaxRequest()) {
      $output = '';
      Yii::app()->clientScript->renderBodyEnd($output);
      echo $output;
    }
    return true;
  }


The snippet will return the correct view content and script content without including the layout and jquery core script when you do an ajax request. But it will break once you start using any of CJui* widget because the registerScriptFile in CJuiWidget load script file onto the end. When
Yii::app()->clientScript->renderBodyEnd($output);
get executed, those javascript get included again because registerScriptFile include them in the end.

Is there a better way? I like to be able to handle both ajax and non-ajax transparently.

Attached File(s)


0

#2 User is offline   jamesmoey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 17-July 09

Posted 25 March 2010 - 11:19 PM

No worry, I resolve the issue.

by add
Yii::app()->clientScript->scriptMap['*.js'] = false;
in beforeAction. So no javascript file get included in ajax request.
0

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