Updated for grails 1.0
I’ve been playing a bit with grails, but I really miss rails-style dynamic javascript that can be returned for ajax calls, like:
render :update do |page|
page.replace_html 'user_list', :partial => 'user', :collection => @users
page.visual_effect :highlight, 'user_list'
end
using the JavaScriptGenerator
So I decided to see how hard it would be to get something like that in grails. It turns out to be not too difficult. What I have now looks like:
renderJavascript {
replace 'user_list'
effect 'highlight', "user_list"
}
It’s pretty basic, but I’ve wrapped it up as a plugin:
grails-dynamic-javascript-0.2.zip
It adds two methods to all controllers: boolean isAJAX() and renderJavascript().
The basic methods supported are:
appendJavascriptappendJavascript "alert('hi')"
callFunctioncallFunction "foo", 1, true, [a,b]
will generate:
foo(1, true, [a,b]);
JSON encoding is used for the arguments.
effecteffect 'appear', 'element_id', [delay:3]
will generate:
new Effect.Appear('element_id', {'delay':3});
update and replacerender method, supplying it with an out option. If you don’t supply an template option, the element id will be used by default. The rendered template is javascript encoded and used in a prototype update or replace call.
update 'document_1', [text:'Hello']
replace 'document_list'
will generate:
Element.update("document_1","Hello");
Element.replace('document_list', <render _document_list.gsp> );
queueQueue up some actions at the end of the scriptaculous effects queue.
queue {
callFunction "c"
}
will generate:
new Effect.Event({ afterFinish:function() {
c();
}, queue: "end" });
For an example of this, see the demo app – when deleting, it fades out the deleted row, and then queues up a call to remove the tr dom element and recolor alternate table rows.
I’ve made a simple demo application based on a scaffolded ‘Book’ controller. I’ve changed the grails-generated controllers and views to use AJAX calls for creating and deleting books using the plugin. dynamic_javascript_demo_0.1.zip
javascript = "/*-secure-\n"+javascript+"*/"
page.select('#items li').each do |value|
value.hide
end
# => $$('#items li').each(function(value) { value.hide(); });
Update: Fixed link to the demo zip
Update: Fixed plugin to work with Grails 1.0. grails-dynamic-javascript-0.2.zip
the links don't work
Ok - the link the demo zip should be ok now. thanks!
Eoin, very nice works ! thanks.
ibmsoft 25 Jun 12:47
dynamic_javascript_demo_0.1.zip can not download