As most of my problems, this is probably obvious! :] But I decided to ask, as I can’t find answer myself since yesterday.
This is a piece of my JS code, that I publish to page (view) contents directly like this:
echo('<script type="text/javascript">'."\n");
echo("
function configTreeClickHandler(nodeNumId)
{
$('.".$this->_dummyClass."').hide();
$('#' + nodeNumId + '-config-section').show();
return false;
}
var idsArray = ".CJavaScript::encode($this->_idsArray).";
configTreeClickHandler(idsArray[".$startID."]);
");
echo('</script>'."\n");
It works fine, but - as supposed - it is being published exactly, where I publish it - i.e. inside view contents.
When I’m trying to publish exactly the same piece of code with CClientScript:
Yii::app()->clientScript->registerScript('ctch',
"
function configTreeClickHandler(nodeNumId)
{
$('.".$this->_dummyClass."').hide();
$('#' + nodeNumId + '-config-section').show();
return false;
}
var idsArray = ".CJavaScript::encode($this->_idsArray).";
configTreeClickHandler(idsArray[".$startID."]);
");
it fails! Click handler is not being found (although code exists, only this time it is published in the bottom of page, along with other JS codes published with CClientScript) and clicking on items that has this handler attached in onClick ends with console message saying: "[b]Error: configTreeClickHandler is not defined
File: myaapp/admin/config.html Line: 1[/b]".
Got no idea, what is going on. Why browser can’t find code, if it really is inside page source? I thought that there is no different between publishing code with CClientScript and directly with echo().