Hi dear Yiiers,
In my Page admin view, I can explore the Page model tree and open a form for editing a page while selecting an item in the tree.(I’m using simpleTree extension). This work nicely.
But when I update a page with an ajaxSubmit button, the POST request is executed many times, see logs on the screen capture
3552
The view:
<div class="row">
<div class="span3" id="pageTree">
<?php
$this->renderPartial('_pageTree',array(),false,true);
?>
</div>
<div class="span9" id="pageEdit">
</div>
</div>
the pageTree partial:
<?php
$this->widget('application.extensions.SimpleTreeWidget',array(
'model'=>'Page',
'modelPropertyParentId' => 'parent_id',
'modelPropertyName' => 'title',
'ajaxUrl' => $this->createUrl('/content/page/simpletree'),
'onSelect'=>'
var id = data.inst.get_selected().attr("id").replace("node_","");
$("#pageEdit").load("edit/id/"+id);
'
));
?>
Page Controller actions:
//this is for displaying the form
public function actionEdit($id)
{
$model=$this->loadModel($id);
$this->renderPartial('_update',array('model'=>$model),false,true);
}
//for updating
public function actionUpdate()
{
if(Yii::App()->request->isAjaxRequest){
if(isset($_POST['Page'])){
$model=$this->loadModel($_POST['Page']['id']);
$model->attributes=$_POST['Page'];
if($model->save()){
$this->renderPartial('_pageTree',array(),false,true);
}
}
}
}
and in the form (_update.php):
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'ajaxSubmit',
'type'=>'primary',
'label'=>'Submit',
'url'=>$this->createUrl('/content/page/update'),
'ajaxOptions' => array ('update'=>'#pageTree')
)); ?>
What’s wrong with this approach ?
Also, same js and css files are loaded many times …