Skip to content

Commit

Permalink
Modified require tag to be AMD compliant
Browse files Browse the repository at this point in the history
Converted the legacy <require> tag to produce a <script> tag filled-in
with AMD style requires, along with a callback closure containing
parameters named after the required modules.
e.g. <require modules="['dijit/form/Form',
'dijit/form/ValidationTextBox']">
console.debug("I'm in the callback");
</require> will produce :
require(['dijit/form/Form', 'dijit/form/ValidationTextBox'],
function(Form, ValidationTextBox) {
console.debug("I'm in the callback");
});

The tag now accepts contents in its body, which will be the body of the
callback.

The callback parameter names can be overridden by the tag parameter
"callbackParamNames".

e.g. <require modules="['dijit/form/Form',
'dijit/form/ValidationTextBox']"
callbackParamNames="['dijitForm','vtb']">
console.debug("I'm in the callback");
</require> will produce :
require(['dijit/form/Form', 'dijit/form/ValidationTextBox'],
function(dijitForm,vtb) {
console.debug("I'm in the callback");
});

Note : It is deprecated to use global namespaces like dojo or digit in
dojo 1.7+ applications.
  • Loading branch information
Philippe Soares committed May 19, 2012
1 parent 315129f commit b6ae36d
Showing 1 changed file with 27 additions and 42 deletions.
69 changes: 27 additions & 42 deletions grails-app/taglib/org/dojotoolkit/DojoTagLib.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.dojotoolkit

import grails.converters.deep.JSON
import grails.converters.JSON


class DojoTagLib {
Expand Down Expand Up @@ -211,52 +211,37 @@ class DojoTagLib {


/**
* Will include dojo modules via the dojo loader.
* Will include dojo modules via the dojo loader and make them available in the body of the tag script
* Each require will provide a callback parameter named after the last part of the module name
* e.g.: "dijit/form/Form" will provide a parameter called "Form" in the callback
* @param attrs.modules = This is a map of components to include
* @param attrs.async = If true will use the Dojo AMD compatible way to bring in modules.
* @param attrs.callbackParamNames = This overrides the default callback parameter names, in case you want to use different ones than the defaults
*/
def require = {attrs ->
attrs.async = attrs?.async ?: "false"
if (attrs.modules) {
if(attrs?.async == "true"){
out << amdRequire(attrs)
}
else{
def modules = attrs.modules.collect{"dojo.require('${it}')"}
out << "<script type='text/javascript'>${modules.join(';')}</script>"
}
}
def require = {attrs, body ->
def modules = attrs.modules.collect{ "'${it}'" }
def callbacks = attrs.modules.collect{ it.split("/").last() }
if (attrs.callbackParamNames) {
callbacks = attrs.callbackParamNames
}
assert attrs.callbackParamNames?.size() <= modules.size()

out << """
<script type='text/javascript'>
require(${modules},
function(${callbacks.join(',')}){
"""
attrs?.modulePaths?.each{k,v->
out << " dojo.registerModulePath('${k}','${v}'); "
}

out << body()
out << """
});
</script>
"""
}



/**
* Will bring in modules using the new amd async approach.
* @param attrs.modules - The modules to include
* @param attrs.modulePaths - Defines custom paths to look for those modules.
*/
def amdRequire = {attrs->
def modules = attrs.modules.collect{"dojo.require('${it}')"}

out << """
<script type='text/javascript'>
require(['dojo/_base/kernel', 'dojo/_base/loader'],
function(dojo){
"""
attrs?.modulePaths?.each{k,v->
out << " dojo.registerModulePath('${k}','${v}'); "
}

out << """
${modules.join(';')}
});
</script>
"""
}




/**
* This will wrap a response in a text area element if a flash var is set.
* This is set by a dojo.io.iframe call and intercepted by the BaseController.
Expand Down

0 comments on commit b6ae36d

Please sign in to comment.