Hi everyone,
I follow the great tutorial http://www.yiiframework.com/wiki/145/cjuidialog-for-create-new-model/ and It works like wanted. However I would like to put the script part in a file in order to regroup all functions I use.
So in my view I have :
<?php echo CHtml::link('<img id="addImg" src="/myNewApp/images/add.gif" height="15px" width="15px"/>',"",array('onclick'=>"{addElement(); $('#dialogElement').dialog('open');}"));?>
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
'id'=>'dialogElement',
'options'=>array(
'title'=>'Create Element',
'autoOpen'=>false,
'modal'=>true,
'width'=>550,
'height'=>470,
),
));?>
<div class="divForForm"></div>
<?php $this->endWidget();?>
<script type="text/javascript">
function addElement()
{
<?php echo CHtml::ajax(array(
'url'=>array('MyElem/create'),
'data'=> "js:$(this).serialize()",
'type'=>'post',
'dataType'=>'json',
'success'=>"function(data)
{
if (data.status == 'failure')
{
$('#dialogElement.div.divForForm').html(data.div);
// Here is the trick: on submit-> once again this function!
$('#dialogElement.div.divForForm form').submit(addElements);
}
else
{
$('#dialogElement.div.divForForm').html(data.div);
setTimeout(\"$('#dialogElement').dialog('close') \",3000);
}
} ",
))?>;
return false;
}
</script>
The above code works well. Now I wrote the function of the script part in a file at /js/functions.js and I replaced :
<script type="text/javascript">
function addElements()
{
<?php echo CHtml::ajax(array(
'url'=>array('MyElem/create'),
'data'=> "js:$(this).serialize()",
'type'=>'post',
'dataType'=>'json',
'success'=>"function(data)
{
if (data.status == 'failure')
{
$('#dialogElement.div.divForForm').html(data.div);
// Here is the trick: on submit-> once again this function!
$('#dialogElement.div.divForForm form').submit(addElements);
}
else
{
$('#dialogElement.div.divForForm').html(data.div);
setTimeout(\"$('#dialogElement').dialog('close') \",3000);
}
} ",
))?>;
return false;
}
</script>
by :
<?php Yii::app()->getClientScript()->registerScriptFile(Yii::app()->baseUrl."/js/functions.js"); ?>
but this doesn’t work. The dialog opens well but stays blank.
Do you have an idea about what’s wrong or how could I put the script part of my code in a file.
thanks.