Fork me on GitHub

21 May 2011

Resources plugin and modular web components for Grails apps

I recently gave a talk on ‘Building progressive UIs with Grails’ at gr8conf in Copenhagen and was really pleased with the feedback & comments I received afterwards. There was a question asked at the end that I felt in retrospect I could have answered better, though. I had mentioned that the Grails AJAX tags were something that should be avoided as they write script blocks directly into the page and event handlers directly onto HTML elements. I was pitching an approach based on a clean separation of clean semantic markup and script enhancements and inline script violates that separation.

I was asked if there might be any kind of tags that could be developed that provided a more appropriate replacement and answered that since modern JavaScript frameworks such as jQuery make decorating elements with AJAX functionality so easy that I didn’t think there was much point.

I wouldn’t want anyone to understand me to mean that creating taglibs and GSP templates for modular components of your pages is a bad thing. I’d just advocate keeping the script out of them. Custom tags that write out markup or delegate to templates can be really useful for building complex reusable modules. Pairing those with external JavaScript files that enhance the generated markup would be very effective.

If you’re using the resources plugin (and really… you should be) then there’s a really neat way to tie the taglib or template to the JavaScript file as well. I did briefly mention this in my talk but it’s worth expanding on here as it’s a technique with a lot of potential.

The resources plugin’s r:use tag doesn’t write out anything directly to the page but rather adds a resource module to a list that will get written out at the appropriate place in the page when the r:layoutResources tag is used. This means modular components throughout the page can declare a dependency on JavaScript and CSS resources by simply calling r:use. In a complex app this can be a real boon as it might not be obvious from the top level GSP exactly which modules are going to get rendered. Also if a module is later added to or removed from a page you don’t need to worry about fiddling around with resource declarations in the top level GSP. Taglibs or templates become real drop in components whilst maintaining a nice clean separation of markup and script. Even better, if you use the same resource dependency multiple times the plugin ensures the resources are actually only linked once.

Since r:layoutResources is typically used in a SiteMesh layout the resources your module depends on can even be ones that need to appear in the head of the document. The SiteMesh is rendered after the GSP it decorates so any r:use calls will already have been made.

8 comments:

Willie Kaminski said...

You blog post is just entirely quality and useful, many new facts and information which I have not heard about before.
essay writing service reviews

Online Friv said...

You need to kill time, you need entertainment. Refer to our website. hope you get the most comfort.
Thanks you for sharing!
Friv 4

plussize73 said...

Can I simply say what a comfort to discover someone who actually understands what they're discussing on the web. You certainly realize how to bring an issue to light and make it important. More and more people need to check this out and understand this side of your story. It's surprising you aren't more popular given that you surely have the gift. www.menstopspot.com | www.pureitfresh.com | www.warmmensfashion.com | www.urbanplussizeclothing.org | www.burgundybodycondress.com

ngocanh said...

Your blog posts are more interesting and impressive. I think there are many people like and visit it regularly, including me.I actually appreciate your own position and I will be sure to come back here.
hotmail login l login hotmai l hotmail log in l hotmail sign in l hotmail sign up

Emily Smith said...

Looking for some addicting Korean TV shows to watch? ... For example, variety shows tend to use simple, conversational language, while dramas can cover a ...Feb 27, 2017 - Several hit South Korean variety shows have been removed from major streaming platforms in China over the past two days.Best Korean Variety Shows interactive top ten list at TheTopTens ®. Vote, add to, or comment on the Best Korean Variety Shows.Read FACT #9 || What Korean Variety shows & K- Dramas I've watched || from the story ~ All about Meh~ by PinkPrincessJinOppa (ʕ•ᴥ•ʔ Cutie ʕ•ᴥ•ʔ) with 43 ...Dailymotion United States - Playlist Korean Variety Shows.Find and follow posts tagged korean variety show on Tumblr.Translate Korean variety shows. See Spanish-English translations with audio pronunciations, examples, and word-by-word explanations.

我愛你 said...


What you do is beautiful and very sexy. Please take care and do not shorten what you do
العاب سباق 2018
العاب طبخ
العاب تلبيس

ali gamal said...


شركة كشف تسربات المياة بالدمام

شركة مكافحة بق الفراش بالدمام
هل تود الاستعانة بأفضل شركات عزل الاسطح بالدمام مع الحصول علي اكبر فترة ضمان عليك بشركة النسور تقدم الشركة عروض وخصومات رائعة فقط عليك بزيارة موقعنا

شركة عزل اسطح بالدمام



شركة مكافحة حشرات بالخبر

Games 2 Girls said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write
run2online.net