Chapter 11 - Ajax Integration ============================= Interactions on the client side, complex visual effects, and asynchronous communication are common in Web 2.0 applications. All those require JavaScript, but coding it by hand is often cumbersome and time-consuming to debug. Fortunately, symfony automates many of the common uses of JavaScript in the templates with a complete set of helpers. Many of the client-side behaviors can even be coded without a single line of JavaScript. Developers only need to worry about the effect they want to achieve, and symfony will deal with complex syntax and compatibility issues. This chapter describes the tools provided by symfony to facilitate client-side scripting: * Basic JavaScript helpers output standards-compliant ` But the most common use of JavaScript, more than code blocks, is in a hyperlink that triggers a particular script. The `link_to_function()` helper does exactly that, as shown in Listing 11-2. Listing 11-2 - Triggering JavaScript by a Link with the `link_to_function()` Helper [php] => Click me! As with the `link_to()` helper, you can add options to the `` tag in the third argument. >**NOTE** >Just as the `link_to()` helper has a `button_to()` brother, you can trigger JavaScript from a button (``) by calling the `button_to_function()` helper. And if you prefer a clickable image, just call `link_to_function(image_tag('myimage'), "alert('foobar')")`. ### Updating a DOM Element One common task in dynamic interfaces is the update of an element in the page. This is something that you usually write as shown in Listing 11-3. Listing 11-3 - Updating an Element in JavaScript [php]
Data processing beginning
Data processing complete"; ") ?> Symfony provides a helper that produces JavaScript, not HTML, for this purpose, and it's called `update_element_function()`. Listing 11-4 shows its use. Listing 11-4 - Updating an Element in JavaScript with the `update_element_function()` Helper [php]
Data processing beginning
"Data processing complete", )) ) ?> You might be wondering why this helper is particularly useful, since it's at least as long as the actual JavaScript code. It's really a matter of readability. For instance, you might want to insert content before or after an element, remove an element instead of just updating it, or even do nothing according to a certain condition. In such cases, the JavaScript code becomes somewhat messier, but the `update_element_function()` keeps the template very readable, as you can see in Listing 11-5. Listing 11-5 - Options of the `update_element_function()` Helper [php] // Insert content just after the 'indicator' element update_element_function('indicator', array( 'position' => 'after', 'content' => "Data processing complete", )); // Remove the element before the 'indicator', and only if $condition is true update_element_function('indicator', array( 'action' => $condition ? 'remove' : 'empty', 'position' => 'before', )) The helper makes your templates easier to understand than any JavaScript code, and you have a single syntax for similar behaviors. That's also why the helper name is so long: It makes the code self-sufficient, without the need of extra comments. ### Graceful Degradation The `