Working with Cakephp Ajax (Prototype)
Posted on | January 30, 2009 | 1 Comment
I have been working on a project that is quite heavy in ajax, using the AjaxHelper in Cakephp.
To note down a couple of things:
- Set up content type for Ajax response
- Pitfall when commenting out codes
- Callback types
1) Set the content type of Ajax response in app_controller beforeFilter()
if($this->RequestHandler->isAjax()) {
Configure::write('debug', 0);
$this->RequestHandler->setContent('javascript', 'text/javascript');
$this->RequestHandler->respondAs('javascript');
$this->layout = 'ajax';
}
Javascript codes returned in the response will be eval as javascript automatically without setting the evalscript:true in the Ajax options.
2) Always use <?php /* ?><?php */ ?> to comment out. I can’t remember how many times this has got me. I always comment out my html instead of deleting them in case they are needed again. Using html <!– //–> to comment out the html code which is mixed with php codes to generate the ajax sorting headers for table or ajax links, will cause javascript errors.
The error message is usually ‘element is null’, triggered somewhere in prototype.js.
Note to myself: Always use php comments to comment unused codes out!
3) The return callbacks to take note of
- loading (onLoading) : this is where you will want to show your loading message, disable the form to prevent user from submitting again by showing a modal message, etcUse $(’msg’).show() to display the loading spinnerResources for spinners
Generator – http://www.ajaxload.info/
More spinner images – http://www.loadinfo.net/ - complete (onComplete) : This is where you will deal with the response from the server. When using Ajax.Updater, the element will be updated automatically with the response. I am also enabling the form or removing the loading message in this callback.Use $(’msg’).hide() to hid the loading spinner
Comments
One Response to “Working with Cakephp Ajax (Prototype)”
Leave a Reply





















June 2nd, 2009 @ 8:55 pm
Great tip – that is exactly what I was looking for. Thanks for posting it