This article covers how to use the jQuery library in to power ajax applications developed in the PHP5 framework, Symfony-project. You need to have basic understanding of AJAX, JavaScript and the MVC structure of Symfony-project to get the fullest out of this article.
The example shown below uses only a few lines of JavaScript to globally power the typical ajax calls used on the site Kevo.com. It is also totally degradable and easy to remove and test your application without AJAX enabled.
');
$('#' + params.target).load(action[0] + '/ajax', function(){
$("#" + params.target).highlightFade('#ADDCF0', 1000, null, 'exponential');
});
return false;
});
as you can see the target of this function is defined in the HTML, but only accessed by javascript. it tells the ajax action where to go.
the line $('#' + params.target).load(action[0] + '/ajax', function(){ tells the ajax location WHERE to go, and WHICH action to use in the controller.
and the ACTION code
now you can remove prototype and scriptalicious from symfony-project, saving you quite a bit of load space. You will also have removed inline javascript and made a global ajax action that can work on any link using the same code.
but this isn't complete yet, I'll show you how we need to do the routing and partials
the _addFavorite.php partial says "if this profile is in your favorites, link to your favorites, otherwise show the link to add them"
And be sure to create the template "profileAddAjaxSuccess.php" which includes only the partial. This is what the ajax action calls, which then calls the partial executes it and inserts the result into the target location
test it out. You'll notice that the entire site is now imput into that div! , so turn off the layout on your action
Cool. This works perfectly fine right now, but what if I told you we could turn up the heat a bit more and really make this entire ajax thing shine. Lets add in the ability to pass multiple peices of information back to the browser at one time using JSON, which also parses faster than its XML equivalent.
JSON is just a fancy name for a javascript object literal, like this obj = {}. or like this obj = {one: this, two: that}. Now instead of the variable names one, two, lets pass html & message , Something like this
So instead of parsing the partial and echoing out the result which is transported w/ XML and inserted into the location, we parse the Partial with the get_parial(); function (escape line breaks to make it a single liner with our KEVO utilities) and return , literally { html : '
Now lets parse this with a modified ajax function and stick things where they need to go
I've also added a div that is absolutely positioned with the ID "ajaxmessage" to be the container for the message results , since we're passwing the message along w/ the HTML contents.
Often in symfony you'll end up using some routing and referrer tricks and components. These provide challenges to AJAX components because we want to create easy ways to store referrers and posted values so those can be forwarded or redirected wherever necessary.
An example would be a global "rating" system. Since this is abstracted and not attached to anything else within your project, say an item to be rated, then the routing must go from the initial rating component -> click on rate -> rating module action (executes rate) -> back to location (executes referrer).
Doing this with AJAX , by just tacking on "/ajax" to the routing requires that the referrer also know that it needs to be referred to its ajax route, this can be solved w/ forwarding function....
etc./