Undelegate to Prevent Recursive Ajax

I looked at several different posts I found and it just seems I can’t get this working right. Maybe someone else will see what I am doing wrong.

Only relevant code will be shown in anything.

Form





<?php echo CHtml::dropDownList('category_dd','',$data,array(

			'prompt'=>'---- Select Category ----',

			'style' => 'width:300px',

			'onchange'=>"$('body').undelegate('#showSubcategoriesDialog','click')",

			'ajax' => array(

				'type'=>'POST', //request type

				'url'=>CController::createUrl('subcategories/dependents'), //url to call.

				'replace'=>'#subcategory_div', //selector to update

)));  ?>


<div class="row">

	<div id="subcategory_div"></div>

 	<div id="subcategoriesDialog"></div>

</div>




Subcategory Controller




public function actionDependents(){

//Set to false, true to force the ajax to redelegate and operate on the main form after the ajax call

     $this->renderPartial('subcategory', array('options'=>$options,'categoryid'=>$categoryid),false,true);


}



subcategory view





<?php


echo '<div id="subcategory_div">';

echo CHtml::dropDownList('subcategory_dd','',$options,array('prompt'=>'---- Select Category First ----',

				'style' => 'width:300px',

				'ajax' => array(

				'type'=>'POST', //request type

				'url'=>CController::createUrl('fields/dependents'), //url to call.

				'update'=>'#setfields', //selector to update

				)));

			

				echo CHtml::ajaxLink(Yii::t('subcategories','Create New Subcategory'),$this->createUrl('subcategories/addnewsubcategory'),array(

       			 	'onclick'=>'$("#subcategoriesDialog").dialog("open"); return false;',

       			 	'data'=>'categoryid='.$categoryid,

       			 	'update'=>'#subcategoriesDialog',

       				 ),array('id'=>'showSubcategoriesDialog'));

echo "</div>";

?>



Alright now here is my problem, if I select a category, the first time you hit the addnewsubcategory link that gets rendered it works perfectly fine and only passes the correct categoryid through in the GET, however if I change the category dropdown, and then click the new addnewsubcategory link that is shown, it calls both the new and old events passing 2 categoryids through. And so on and so forth for every new category clicked without a page refresh.

I tried using the undelegate in the category dropdown, but apparently I am not using it right.

Thanks for any help.

I tried registering a new script that would activate in the onclick of the category dropdown to undelegate the newSubcategory link and it didn’t seem to help.

RRRG. This is really messing with my head.

I seem to have found a way to accomplish what I needed. Just use a static ajax link inside the form view that uses js to grab the datavalue.




 

echo CHtml::ajaxLink(Yii::t('subcategories','Create New Subcategory'),$this->createUrl('subcategories/addnewsubcategory'),array(

'onclick'=>'$("#subcategoriesDialog").dialog("open"); return false;',

'data'=>array('categoryid'=>'js:$("#category_dd").val()'),

'update'=>'#subcategoriesDialog',

),array('id'=>'showSubcategoriesDialog'));