Friday, May 8, 2009

Ajax Link Using symfony

Ajax Link

Ajax links form a large share of the Ajax interactions available in Web 2.0 applications. The link_to_remote() helper outputs a link that calls, not surprisingly, a remote function. The syntax is very similar to that of link_to() (except that the second parameter is the associative array of Ajax options), as shown in Listing 11-9.

Listing 11-9 - Ajax Link with the link_to_remote() Helper



echo link_to_remote('Delete this post', array(
'update' => 'feedback',
'url' => 'post/delete?id='.$post->getId(),
)) ?>

In this example, clicking the 'Delete this post' link will issue a call to the post/delete action in the background. The response returned by the server will appear in the element of id feedback. This process is illustrated in Figure 11-1.

Figure 11-1 - Triggering a remote update with a hyperlink

Triggering a remote update with a hyperlink

You can use an image instead of a string to bear the link, use a rule name instead of an internal module/action URL, and add options to the tag in a third argument, as shown in Listing 11-10.

Listing 11-10 - Options of the link_to_remote() Helper

"emails">
echo link_to_remote(image_tag('refresh'), array(
'update' => 'emails',
'url' => '@list_emails',
), array(
'class' => 'ajax_link',
)) ?>

1 comments:

  1. I just noticed that if I use an image for the link, instead of text, it seems to cause the page to jump to the top in IE... not in FF or Chrome. Have you seen this?

    ReplyDelete