Wednesday 30 January 2008

Grails: Tag for Prototype observe

Continuing with Grails taglib, now I'm going to do a taglib to easy capture and group javascript events. I'll use prototype element observe method. Here the code:


def observe = {attrs ->
if(!attrs.noScript){
out << '<script type="text/javascript">'
}
if(attrs.element && attrs.element instanceof String){
printObserve("\$('${attrs.element}')", attrs.event, attrs.function, out)
}
if(attrs.element && attrs.element instanceof List){
attrs.element.each{it -> printObserve("\$('${it}')", attrs.event, attrs.function, out)}
}
if(attrs.classes && attrs.classes instanceof String){
printObserveClass(attrs.classes, attrs.event, attrs.function, out)
}
if(attrs.classes && attrs.classes instanceof List){
attrs.classes.each{ it -> printObserveClass(it, attrs.event, attrs.function, out)}
}
if(!attrs.noScript){
out << '</script>'
}
}

def printObserveClass(className, event, function, out){
out << "var classes = \$\$('.' + '${className}');"
out << "for(i = 0; i < classes.length; i++) {"
printObserve("classes[i]", event, function, out)
out << "}"
}

def printObserve(element, event, function, out){
if(event && event instanceof String){
out << "${element}.observe('${event}', ${function});"
}
if(event && event instanceof List){
attrs.event.each{ it -> out << "${element}.observe('${it}', ${function});"}
}
}




Now, some examples.

Simple use:

<g:javascript>
function testing(event){event.stop();alert('hello');}
</g:javascript>

<a id="idTest" href="http://grails.org">Grails Rulez</a>

<g:observe element="idTest" event="click" function="testing"/>


With a list of elements:

<g:observe element="${['id1','id2','id3']}" event="click" function="testing"/>


With a list of events:

<g:observe element="${['id1','id2','id3']}" event="${['click','load']}" function="testing"/>


With a list of classes:

<g:observe element="${['id1','id2']}" classes="${['class1','class2']}" event="change" function="testing"/>

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

Saturday 19 January 2008

Grails, be agile my friend

This month I've been testing Grails, (it was in my Todo list some time ago).
First I've read some about Groovy, and it's really easy the syntax and I like it.

Then, I've read about Grails architecture and the User Guide.

Impressions.


My impresion it's Grails is not much diferent than Appfuse, step by step:
  • GORM and Domain Model, is based in JPA and Hibernate. (domain centric approach explained by icoloma)
  • Controllers are based in Spring MVC.
  • IoC with Spring (of course).
  • Layouts based in Sitemesh.
  • Also you have Web Flow with Spring Web Flow.
  • Both are a simple WAR file.
This similars are very good for me, because I'm an old user of Appfuse and maybe can adapt Appfuse applications to Grails, I and can achive more agility.

Enterprise Ready?

Graeme Rocher gave a kick ass to someone that say no :D. I think it's ready, you have to take care about your code like in any other language.

The only thing I miss, is better support for Groovy and GSP in Eclipse (autocompletion, refactor) and manage the dependencies.

update: Guillaume Laforge thoughts about Enterprise Ready