As mentioned in previous posts, Ajax in Symfony is a very easy experience. However sometimes the pre-made functions don’t provide all the functionality you need.
Unfortunately when you want to update a dynamically assigned part of the page you apparently need to write your javascript manually. In my case I had a list of comments where I wanted to be able to vote them up or down. So i needed the remote_function to update whichever comment the javascript code was called from. The remote_function however always adds nice quotes around what you type. So:
remote_function(array( 'update' => 'obj', 'url' => 'comment/giverating', ))
results in this javascript:
new Ajax.Updater('obj', '/frontend_dev.php/comment/giverating', {asynchronous:true, evalScripts:false})
This makes it impossible to refer to an object for the first argument of the Updater method.
How to update a dynamic area:
In javascript you can refer the location from which a function is called by passing the this variable in the function call. So in your function call you write:
link_to_function(__('+'), 'giverating('this)' )
Now in your actual javascript function you simply receive the this object and update it, as such:
function giverating(obj) {
new Ajax.Updater(obj, '".url_for('comment/giverating')."', {asynchronous:true, evalScripts:false})
}
");?>
Note that using the url_for php function to write the javascript ensures that a routing change will not upset the functioning of the javascript.