Sunday 27 January 2008

Grails: Easy tags (remoteDiv)

TagLibs in Grails are extremely easy, you have some tags for Ajax like remoteField, remoteFuntion, remoteLink. But this time I need to get div asynchronous when page loads, this is very useful for example to show the users connected.

There isn't remoteDiv tag in Grails but in five minutes I've got my tag:


def remoteDiv = { attrs,body ->
def complete = ""
if(attrs.onComplete){
complete = ",onComplete: ${attrs.onComplete}"
}

out << """
<script type="text/javascript">
new Ajax.Updater(
{ success: '${attrs.id}' },
'${attrs.url}',
{
method: 'get',
evalScripts: true
${complete}
});
</script>
"""
}


An example of use:

<div id="testAjax"></div>
<g:remoteDiv
id="testAjax"
url="${createLink(action: 'show', id:'1')}"
onComplete="new Effect.Highlight('testAjax')"
/>


I've use prototype AjaxUpdater to do asynchronous request, it's not library agnostic like others remote tags in Grails but it's simple :D

No comments: