something that’s been bothering me for quite a while is that Yii in general retransmits the whole page upon an AJAX request. This makes sense if the ID hasn’t been explicitly specified and thus needs to be calculated I guess, but when you actually specify the ID of the div that should be updated, I cannot understand the reasoning behind it.
For my own widgets I have come up with a solution I’m quite happy with. For a given widget, say Commenting, I have a class CommentingAction extending Action. I then register one common controller WidgetController, which in turn refers to CommentingAction as the appropriate action. CommentingAction can then only re-run the widget Commenting if the request is an AJAX request, otherwise redirect the user to the correct page.
However, with CListView I find it conceivably hard to implement anything like this. Of course I’d like the buttons to work as they do now if the request is not AJAX. Otherwise, I’d like it to trigger some action that only returns the widget again, not the whole page. I cannot see any good implementation for this without either rewriting almost everything or possibly hacking something together.
Do you have any recommendations? What would be wrong with my (possibly quite heavy) feature request that AJAX requests should only redraw the one div tag if an ID has been explicitly specified?
Never done like you asked but I have the same problem before so I create my own style of list view. My ajax did alter the HTML code in the page in order to redraw something. Not a very good solution since I didn’t utilize Yii’s CListView but worked for me. Also look forward to any answer to your question.
You can try something like this to render only the specific part you need:
public function beforeAction($action){
if(Yii::app()->request->isAjaxRequest){
$this->layout = false;
}
}
That will make CController::render behave as if were renderPartial by not rendering the layout, that way only the result in your view will be returned in the ajax call, but normal calls will be sent with the template as normal.
The problem is that this cannot work. CListView does not give links to some specific action but merely appends some GET parameters. As a result there is no way of telling which part of the view needs to be updated. So one must change the behavior of the buttons.
Well with this, you’ll mainly update everything that is not part of the layout, its not exactly what you want, but it still saves some bandwidth and therefore load time.