Grails 0.6: Modifying JavaScript Libraries

September 19, 2007 by Michael

Grails has a good mechanism for including JavaScript Libraries to do all that fance Ajax stuff. It all starts with:

<g:javascript library="scripaculous" />

Library can be one of yahoo, prototype, dojo, scriptaculous or rico. It’s a nice fact, that Grails doesn’t force you to use any special of these.

I wanted to change the included javascript source files (I use yahoo and wanted to get the latest release from their servers). In Grails 0.5.6 the core plugins were copied to $APP_HOME/plugins and i could modifiy them inplace. Grails 0.6 cleaned up some stuff so that the core plugins do not flood into the application tree.

I could hack the Grails source files or i can just modify the static member LIBRARY_MAPPINGS in JavascriptTagLib while starting up the application like so:

BootStrap.groovy in $APP_HOME/grails-app/conf

class BootStrap {
     def init = { servletContext ->        
          org.codehaus.groovy.grails.plugins.web.taglib.JavascriptTagLib.LIBRARY_MAPPINGS.yahoo.clear();
          org.codehaus.groovy.grails.plugins.web.taglib.JavascriptTagLib.LIBRARY_MAPPINGS.yahoo + ['anylibrary_file_you_want.js', 'anylibrary_file_you_want2.js', 'etc.js']
     }
     def destroy = {
     }
}

Furthermore, Grails 0.6 introduced the “base” attribute to g:javascript. Base denotes the full path that is prepend to the library file in question. So its possible to use any js file from wherever you want.

No comments yet

Post a Comment

Your email is never published nor shared. Required fields are marked *