I’d like to discuss a common problem that many time I found in topics.
The problem happens everytime that we have to refresh a content with clientscript in an ajax request.
The two instriment that the framework actually provide are:
$this->renderPartial('view', $params, false, true)
The first don’t works, because all clientScript inside the refreshed content will not work, and the latter don’t work, because all client script outside the refreshed div will not work.
By the way, calling the latter make so that ajax request start a strange recursion, the first time 2 request will be send instead of 1, the second 4, then 8 - 16 and so on.
The common workaround (and what I have always done too) was to avoid to use clientscript for this widget, and that means avoiding even to use CJuiWidget, because the register script file, css file and the script itself in the clientscript.
The workaround concept itself is not bad: the main point is that if we have to render a subview, this subview should be complete and contain all the necessary script for work on his own.
Starting from this idea I implemented this patch for the bug: 768
This file extends CController (so you can put in components and edit /components/Contoller.php for extend it).
This extended class provide a method "renderPartialWithHisOwnClientScript", that will render the clientScript of the subView (and ONLY the clientScript of the subView) at the bottom of the subview itself.
It allows you to work normally, without workaround and without hand-creating CJuiWidget, just remember to call this new renderparital and in the normal renderPartial and in the ajaxRequest function.
NOTE: there is still a problem with id, the components with clientScript inside the content to ajax-refresh should still have a unique id in order to not confilct with external components with the same id.
This cannot be fixed with an extension because CHtml::ID_PREFIX is a const and cannot (for now) be edited.
For YiiDevelopers: this file is a small edit in CControler and CClientScript, please check if it break something and include it in the official release. Let’s have a talk about how to fix even the id-conflict problem.
I feel that this detail is the last one Yii’s week point in client side managment, let’s fix it!