[Best Practice] Yii CGridView update/create in popup with iframe

In Yii wiki, there are some articles about this topic.

http://www.yiiframework.com/wiki/263/cgridview-update-create-records-in-a-cjuidialog/

http://www.yiiframework.com/wiki/216/update-delete-model-with-cjuidialog-works-in-cgridview/

But there are so much modification in controllers, and the the UI logic is coded in controller. I think the expansibility is not good enough. Now based on the wiki/263, I create my own.

Step 1 – generate an iframe-layout (layouts/iframe.php)

Same as http://www.yiiframework.com/wiki/263/cgridview-update-create-records-in-a-cjuidialog/

Step 2 – render CGridView
modify

//--------------------- begin new code --------------------------
 'buttons'=>array(
    'update'=>array(
        'url'=>'$this->grid->controller->createUrl("update", array("id"=>$data->primaryKey,"asDialog"=>1,"gridId"=>$this->grid->id))',
        'click'=>'function(){$("#cru-frame").attr("src",$(this).attr("href")); $("#cru-dialog").dialog("open");  return false;}',
     ),
 ),
//--------------------- end new code --------------------------

to

//--------------------- begin new code --------------------------
 'buttons'=>array(
    'update'=>array(
    'url'=>'$this->grid->controller->createUrl("update", array("id"=>$data->primaryKey,"asDialog"=>1,"gridId"=>$this->grid->id))',
    'click'=>'function(){'
              . '$("#cru-frame").attr("src",$(this).attr("href")); '
              . '$("#cru-dialog").unbind("close"); '
              . '$("#cru-dialog").bind("close", function(){$.fn.yiiGridView.update("'. $this->grid->id . '"); $("#cru-frame").attr("src","");});'
              . '$("#cru-dialog").dialog("open");'
              . 'return false;'
              . '}',
),
 ),
//--------------------- end new code --------------------------

Step 3 – change Controller
Change

if (!empty($_GET['asDialog']))
            {
                //Close the dialog, reset the iframe and update the grid
                echo CHtml::script("window.parent.$('#cru-dialog').dialog('close');window.parent.$('#cru-frame').attr('src','');window.parent.$.fn.yiiGridView.update('{$_GET['gridId']}');");
                Yii::app()->end();
            }

to

if (!empty($_GET['asDialog']))
{
      //Close the dialog, reset the iframe and update the grid
      echo CHtml::script("window.parent.$('#cru-dialog').dialog('close');");
      Yii::app()->end();
}

That is the key point, the controller only take responsibility for closing the dialog. What should be done after dialog closing is defined in the view. And then the controller can be reused in more ajax context without changing controller. Hope it can help.