1

I'm registering a script when a form is created:

Yii::app()->clientScript->registerScriptFile(
    Yii::app()->baseUrl . '/js/activity_package.js',
    CClientScript::POS_END
);

But I need to call renderPartial() when some records are created dynamically in the view, and it does the script load two or more times. How can I include my script so that there's certainty that it's loaded only one time?

8
  • 1
    Load it in the main view not on the renderPartial() view. Commented Apr 3, 2014 at 15:27
  • I'm calling registerScriptFile() from the controller, before the main view is rendered. Commented Apr 3, 2014 at 15:28
  • When you are adding records dynamically are you using an ajax call to the same controller/action? Commented Apr 3, 2014 at 15:35
  • @Pitchinnate Yes, the same controller, but different action. Must I use another controller? Isn't there a more flexible way? Commented Apr 3, 2014 at 15:48
  • Is the script registered inside the action function or just in the controller? I personally never register script files in the controller I have always done it in the view, I've never had this issue. Commented Apr 3, 2014 at 15:54

1 Answer 1

2

If you use ajax requtest with renderpartial() so you have to check if the app has not ajax request so register scripts:

if(Yii::app()->request->isAjaxRequest){
      $this->renderPartial('myView',array('data'=>$data),false,true);
}else{
      Yii::app()->clientScript->registerScript('myscript','script');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yii::clientScript() doesn't exist. Right? Did you mean registerClientScript()?
Sorry I edited the code I mean Yii::app()->clientScript

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.