Now that xaJax is aware of our method, we need to implement it withint the controller.
Create a new method and give it the same name as we used when registering it in the previous step. Now add the following 2 lines inside this method:
$oResponse = new xajaxResponse(); return $oResponse;
This provides the basics for any xajax method. It will always return an xajax response object and this should be the first thing instantiated.
At this point it is worth noting that the distributor does not attempt to authenticate ajax requests: they are simply dispatched to the controller. This can of course be changed quite easily, but an alternative (as in the case of the admin system) is to add an authentication wrapper to the ajax calls.
With the basics in place, it is time to add our response. In the same manner as tutorial 2, we will add another method to our guestbookView class to return a formatted random comment.
First though, we need to add the xajax script to the view. This should be added to the construct method so that it is always available to the template files. If we miss this out, then xajax will not be enabled as the javascript links will not be generated.
/**
* @see mvcViewBase::__construct()
*/
function __construct($inController) {
parent::__construct($inController) ;
$this->getEngine()->assign("xajaxScript", $this->getController()->getXajax()->getJavascript('/libraries/'));
}
Now we can create our compile method to fetch the actual template, add our data and return the compiled template. The view code looks like the following:
/**
* Compiles a random comment
*
* @return string
*/
function getRandomComment() {
if ( !$this->isCached($this->getTpl('randomComment', 'guestbook')) ) {
$this->getEngine()->assign('oModel', utilityOutputWrapper::wrap($this->getController()->getModel()));
$this->getEngine()->assign('oComment', utilityOutputWrapper::wrap($this->getModel()->getRandomComment()));
}
return $this->compile($this->getTpl('randomComment', 'guestbook'));
}
The view simply fetches the data from the model using a custom method and defers rendering to the template: 'randomComment'. The model method is implemented as the following within guestbookModel:
/**
* Returns a random comment
*
* @return guestbook
*/
function getRandomComment() {
$oStmt = dbManager::getInstance()->prepare("SELECT * FROM ".system::getConfig()->getDatabase('tutorials').'.guestbook ORDER BY RAND() LIMIT 1');
if ( $oStmt->execute() ) {
$res = $oStmt->fetchAll();
if ( is_array($res) && count($res) == 1 && isset($res[0]) ) {
$oObject = new guestbook();
$oObject->loadFromArray($res[0]);
return $oObject;
}
}
return new guestbook();
}
Finally we can now add the response from our view and assign it back to xajax for display in the web browser. This is handled by the method assign() on the xajaxResponse object. This requires the elementID of an ALREADY existing element on the page, the property to modify which in this case is 'innerHTML' (the case IS important!) and finally our data.
The full request code for the controller is then:
/**
* Returns a random comment block
*
* @return xajaxResponse
*/
function randomComment() {
$oResponse = new xajaxResponse();
$oView = new guestbookView($this);
$oResponse->assign('randComment', 'innerHTML', $oView->getRandomComment());
return $oResponse;
}
The last step is to set-up our view templates.
Posted by: Dave Redfern (Writer), in Tutorials on 11 Nov 2008 @ 20:46
Tags: actions, ajax, controller, form, functions, tutorial,
Contents:
Related Articles
This
work is licenced under a
Creative Commons Licence.