I have multiple javascript functions with ajax calls. At first I put them in view file like this
<?php
Yii::app()->clientScript->registerScript(
"test",
"function test() {
jQuery.ajax({
type: 'POST',
data: 'id=120',
url: '".CController::createUrl('person/getname')."',
success: function(name){
alert( name);
}
});
};
",
CClientScript::POS_READY
);
...
?>
It works, but I have many functions like this, so I'd like to put them in other file than view. I can put it in javascript file,
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/user.js');
but than I can't use the CController::createUrl('person/getname') to get the url.
What is the best approach to do it?
createUrl()is not a static method. You should always use$this->createUrl()orYii::app()->createUrl()instead.