[TYPO3-mvc] [INFO] How-to do AJAX with Extbase

Robert Lemke robert at typo3.org
Mon Aug 3 11:37:21 CEST 2009


Hi Xavier,

Am 01.08.2009 um 10:41 schrieb Xavier Perseguers:

> We now have
>
> public function genresAction() {
> 	$genreRepository =
> t3lib_div
> ::makeInstance('Tx_MvcExtjsSamples_Domain_Model_GenreRepository');
> 	/* @var $genreRepository  
> Tx_MvcExtjsSamples_Domain_Model_GenreRepository */
>
> 		// Retrieve all genres from repository
> 	$genres = $genreRepository->findAll();
>
> 	$this->view->assign('genres', $genres);
> }
>
> with genres.html:
>
> {namespace mvcextjs=Tx_MvcExtjsSamples_ViewHelpers}
> <mvcextjs:json>{genres}</mvcextjs:json>


Hmm, looks a bit dirty having a genres.html file producing JSON.

In FLOW3 I'm using the concept of formats: the request object contains  
the currently
requested format (html, json, xml, ...) and the action controller will  
automatically
select the right Fluid template or view. The big advantage is that you  
don't need to modify
the controller at all for providing a JSON view.

Some untested sample code (note the small name changes):

class Tx_MvcExtjsSamples_Controller_GenreController {

    public function indexAction() {
       $genreRepository =  
t3lib_div 
::makeInstance('Tx_MvcExtjsSamples_Domain_Repository_GenreRepository');
       $genres = $genreRepository->findAll();
       $this->view->assign('genres', $genres);
    }

}

index.html:
    <ul>
    <f:for each="{genres}" as "genre">
       <li>{genre.title}</li>
    </f:for>
    <ul>

index.json:
{namespace mvcextjs=Tx_MvcExtjsSamples_ViewHelpers}
<mvcextjs:json>{genres}</mvcextjs:json>

Cheers,
robert



More information about the TYPO3-project-typo3v4mvc mailing list