Alex Tech Adventures The webs best tutorials!

AJAX requests with Zend Framework

(0 votes, average 0 out of 5)
AJAX requests in Zend Framework are no different than regular requests. You can call any controller/action from the background and it will give you an HTML response. However, you probably do not want the unnecessery code that layout and view scripts provide. You could, of course, create separate controllers and viewers specially for AJAX but that's repetitive code we obviously do not want. Manually switching off layout and view scripts on xmlHttp header detection is not pretty too. To the rescue comes action context switcher that detects the type of request sent and applies appropriate changes to te controller. Watch the video to see how to set it up.

But that is not enough. Once you got ZF to output the desired response, be it XML or JSON, you need to update your front end to accept that response and make sense out of it. In the video I show how you can grap JSON output with a response JavaScript function and put the variables in their right places on the page.


Key notes in the presentation:
  • Setting up action context switcher.
  • Creating links using ajaxLink view helper from ZendX_Jquery ...
  • ... that invoke the context switcher.
  • Review view script to make sure that only final variables are in the view.
  • Review view script's design for more comfortable jQuery manipulation.
  • Create a callback function in JavaScript that accepts JSON generated by context switcher and ...
  • ... applies variables from JSON to the appropriate sections of the page.

Mistake in frontend.js was noted by Yorian

In the video I have <div id="books"> in the loop JSON loop so each book entry becomes an accordion container on its own.  To fix that, need to take <div id="books"> outside of the loop.  I am also putting all HTML in a string and then appending it at the end becuase if you append <div id="books"> on its own, jQuery parses it as a complete HTML code and puts a self closing /> at the end so the rest of the code breaks.  I could, of course, append children but that too complex for this scenario:
1
2
3
4
5
6
7
8
9
10
11
function displayBooks(response){
	response = JSON.parse(response);
	$("#booksResponse").html('');
	var booksHtml = '<div id="books">';
	for(var i in response.books){
		booksHtml += '<h3><a href="#">'+response.books[i].title+'</a></h3><div>written by'+response.books[i].author+'</div>';
	}
	booksHtml += '</div>'
	$("#booksResponse").append(booksHtml);
	$("#booksResponse #books").accordion({"active":"none", "collapsible" : "true"});
}

 

Download soarce

Context Switcher
JSON
ZendX_JQuery ajaxLink

Last Updated ( Sunday, 25 October 2009 13:23 )  
Discuss (1 posts)
AJAX requests with Zend Framework
Aug 30 2010 15:20:08
thx for the tut. good work!
one tiny thing - the injection of js in the layout view specific js doesn't look good. How about $this->headScript()->appendFile( blahblah ) in the view or even to keep it together
$this->headScript()->captureStart() ?>
function blahblah ...
headScript()->captureEnd() ?>

and in the layout template prior jQuery stuff: echo $this->headScript();

Cheers!
#740

You need to login or register to post comments.
Discuss...
You are here: Home Development Zend Framework AJAX requests with Zend Framework

Statistics

Members : 657
Content : 42
Web Links : 1
Content View Hits : 88232

Poll

Interested in TinyBrowser and TinyMce plugin for ZF?
 

Who's Online

We have 65 guests and 1 member online