Yii Framework Forum: Avoiding Multiple Ajax Calls From Cguidialog - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Avoiding Multiple Ajax Calls From Cguidialog Rate Topic: -----

#1 User is offline   gamilix 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 31-October 12

Posted 04 December 2012 - 03:48 AM

Hi,

I wrote a CGuiDialog based on this wiki http://www.yiiframew...axsubmitbutton/
In the dialog I have a dropdownlist which calls to ajax function on change event, and a submit function that execute javascript.

Here is the code:

<div>
<table class="cftable">
<tr>
<td>
<?php
$cf = new commonFunctions;
$text = "";
$dir = "xxx/images/" . $modelDir;
$list = $cf->buildFilesList($dir, true);

if (isset($list))
{
echo CHtml::dropdownlist('_cmchoosePicDDL','',
$list,
array('size' => 10,
'ajax' => array(
'type'=>'POST', 'url'=>CController::createUrl('ImgPreview'),
'data'=>array('bfileNum'=>'js:this.value', 'dirname' =>$dir),
'dataType'=>'json',
'success' => 'function(data) {
$("#_cmfileImage").html(data.fileImage);}',
)), array ('id' => '_cmchoosePicDDL'));
}
else
{
echo 'no files found';
}

?>


</td>
<td>
<div id="_cmfileImage"></div>
</td>
</tr>
</table>

<div class="row buttons">
<?php
echo CHtml::SubmitButton('select',
array('onclick'=>'var selectVal = $("#_cmchoosePicDDL option:selected").text();
alert(selectVal); $("#carModelPic1").val(selectVal); ' . '$("#carPicDialog1").dialog("close");'),
array('id'=>'closePicDialog' )) ; ?>
</div>

</div>


The problem is that the "change" event is executed multiple times.
One time at the first popup one 2 at the second 3 at the third etc….

I am working with firebug and I see that when ever I popup the dialog the script is reregister under assets/xxxxx/jquery.js/eval/seq/

But it only register the call for the dropdownlist and not the submitbutton

Here is the script (from the firefox):

/*<![CDATA[*/
jQuery('#carPicDialog1').dialog({'title':'choose Picture', ,'autoOpen':true,'modal':'true','width':'auto','height':'auto'});
$('body').on('change','#_cmchoosePicDDL',function(){jQuery.ajax({'type':'POST','url':'/carsforumAdmin/index.php?r=cfHpMiCarsmodel/ImgPreview','data':{'bfileNum':this.value,'dirname':'/xxxx/images/x3'},'dataType':'json','success':function(data) {
$("#_cmfileImage").html(data.fileImage);
},'cache':false});return false;});
/*]]>*/


This is the code for poping up the dialog.

Yii::app()->clientScript->scriptMap['jquery.js'] = false;
$this->renderPartial('PicDialog',
array('modelDir'=>$modelDir),false,true);


Any idea?

Thanks
0

#2 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 04 December 2012 - 06:30 AM

Hey,

I've had the same problem with an ajaxSubmitButton. This post helped me a lot.

More or less, it means that you have to unbind the event attached to your dropdown after having used it.

Good luck, ... and post your solution :D
Hey Ho !
Let's go !
0

#3 User is offline   gamilix 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 31-October 12

Posted 04 December 2012 - 08:21 AM

great thank!
it actually works.

I call undelegate function in "click" of my submit button.

echo CHtml::SubmitButton('select',
array('onclick'=>'var selectVal = $("#_cmchoosePicDDL option:selected").text();
$("#carModelPic'. $picItem. '").val(selectVal); ' .
'$("body").undelegate("#_cmchoosePicDDL" , "change");' .
'$("#carPicDialog'. $picItem. '").dialog("close");'),
array('id'=>'closePicDialog' )) ; ?>



thanks a lot i tried to get raid of it for at least couple of days!
0

#4 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 04 December 2012 - 08:28 AM

so do I until I've found the post I've mentionned !
Hey Ho !
Let's go !
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users