Skip to content

Instantly share code, notes, and snippets.

@marcpalmer
Created February 2, 2012 12:05
Show Gist options
  • Save marcpalmer/1723179 to your computer and use it in GitHub Desktop.
Save marcpalmer/1723179 to your computer and use it in GitHub Desktop.
Idea for a way to easily create dynamic JS data blocks in Grails
<%--
Set up the custom wrapper to automatically lay out before "head"
Resources would have a new "init" disposition added that always renders just before "head" disposition
--%>
<%--
This script would be lazy-evaluated only at time of rendering. "stash" would be the
accumulated data for this stash "name"
--%>
<r:script stash="globalJSData" disposition="init">
var jsData = {
${stash}
}
</r:script>
<%-- Later in the page we repeatedly call tags to add to this data --%>
<r:append stash="globalJSData">
updateURL: '${g.createLink(action:"update").encodeAsJavaScript()}'
</r:append>
...
<r:append stash="globalJSData">
'msg.welcome': '${g.message(code:"x.y.welcome").encodeAsJavaScript()}',
'msg.error': '${g.message(code:"x.y.error").encodeAsJavaScript()}'
</r:append>
The above tags would normally be wrapped by other tags, but these are the basic building blocks.
r:append/stash mechanism could be adapted to support lazy evaluation of closures, such that r:append
could be used in other tagLib code to generate the body content on the fly at time of rendering
i.e. collate all the required messages in the request and then dump them out in one go / support
caching etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment