I’m trying to write a Grails custom tag that (among other things) triggers inclusion of a resource, so something like <myTags:view name="foo"/> would load, say, js/views/foo.js. And I want it loaded with disposition: 'head'.
I could use <r:external/>, but that wouldn’t put it in the <head>, it would just produce an inline <script/> tag. And I could use <r.script/>, but that doesn’t let me reference a path; I’d have to have my custom tag read the file and dump it to out.
Now, if foo.js was its own module, I could do something like: r.require([module: 'foo']), but it’s not; part of the point of this is that I don’t want to have to declare all of these files in ApplicationResources.groovy. But maybe I could have ApplicationResources.groovy create the modules programmatically, by reading through the available files — is that possible? Or is there a better way?
I ended up going in the direction of having
ApplicationResources.groovycreate modules programmatically, so the custom tag can use<r:require/>.The idea is, for each Backbone view, under
web-app/myApp/views, there’s a Backbone view in a.jsfile, and a Handlebars template in a.handlebarsfile (with the same name, by convention). The.handlebarsfile gets declared as an ordinary module, but gets precompiled by the Handlebars-Resources plugin.Some code in
ApplicationResources.groovyfinds all the views and creates corresponding resource modules:Then the taglib:
Invoking it in a GSP:
Produces:
It’s not pretty but the mess is mostly hidden, and it should cut down considerably on boilerplate and on opportunities to forget to add magic strings to multiple files.