Hi Chris83,
Is there any reason why this code should not work?
Yii::app()->getClientScript()->registerScript(
'addManufacturer',
"function addManufacturer()
{
".CHtml::ajax(
array(
'url'=>array('manufacturer/create'),
'data'=> 'js:$(this).serialize()',
'type'=>'post',
'dataType'=>'json',
'success'=>"function(data)
{
if (data.status == 'failure')
{
$('#addManufacturer div.divLoader').fadeOut(500, function(){
$('#addManufacturer div.divForForm').html(data.div).fadeIn(500, function(){
// Here is the trick: on submit-> once again this function!
$('#addManufacturer div.divForForm form').submit(addManufacturer);
});
});
}
else
{
$('.alert').bootAlert('alert',data.flash.key,data.flash.message);
$('#addManufacturer div.divForForm').html(data.div);
$('#addManufacturer').delay(2000).fadeOut(500);
$('.modal-backdrop').delay(2000).fadeOut(500);
}
}",
)
)."
return false;
}",
CClientScript::POS_END
);
I’ve been following this tutorial http://www.yiiframework.com/wiki/145/cjuidialog-for-create-new-model/ about using ajax to create new manufacturers when I’m in my product create view.
Adding them works perfectly, but I cannot use $(’#addManufacturer’).bootModal(‘close’) or $(’.alert’).bootAlert(…).
With both of those functions it says “$(…).boot…(…)” is not a function (e.g. “$(’#addManufacturer’).bootModal(‘close’) is not a function”).
Cheers and thank you so much for your extension!
Edit: never mind, I fixed it. It was an issue with dynamically added javascript working in a different scope than your extensions, so it couldn’t find them. Changing my code to this works:
Yii::app()->getClientScript()->registerScript(
'addManufacturer',
"( function( $ ) {
$('#addManufacturerLink').click(function(){
addManufacturer();
$('#addManufacturer').bootModal('open');
return false;
});
function addManufacturer()
{
".CHtml::ajax(
array(
'url'=>array('manufacturer/create'),
'data'=> 'js:$(this).serialize()',
'type'=>'post',
'dataType'=>'json',
'success'=>"function(data)
{
if (data.status == 'failure')
{
$('#addManufacturer div.divLoader').fadeOut(500, function(){
$('#addManufacturer div.divForForm').html(data.div).fadeIn(500, function(){
// Here is the trick: on submit-> once again this function!
$('#addManufacturer div.divForForm form').submit(addManufacturer);
});
});
}
else
{
$('.alert').bootAlert('alert',data.flash.key,data.flash.message);
$('#addManufacturer div.divForForm').html(data.div);
$('#addManufacturer').bootModal('close');
}
}",
)
)."
return false;
}
} )( jQuery );",
CClientScript::POS_END
);
The only thing that happens now is that I get 2 “success” messages. It seems like the function is called twice, even though (when I debug the JavaScript) it’s not doing that…
Edit: Figured out it’s not called twice, but executed twice because jQuery gets registered 2 times. Once when the page is loaded and once when the bootModal form is rendered. I know how to stop all javascript from being rendered in the modal form, but I would like to keep JS validation there. Also it seems that overriding the “run” function of cActiveForm not to include jQuery is not enough… Is there any other place that loads it when BootActiveForm is run?