Skip to content

Instantly share code, notes, and snippets.

@jurberg
jurberg / requirejs.groovy
Last active December 12, 2015 02:29
Run the Require.JS optimizer when creating a war file.
eventCreateWarStart = { warName, stagingDir ->
def curDir = new File('Scripts');
ant.exec(outputproperty: "cmdOut", errorproperty: "cmdErr", resultproperty: "cmdExit", failonerror: "false", executable: "java") {
arg(line: "-cp ${new File(curDir, 'js.jar').absolutePath}")
arg(line: "org.mozilla.javascript.tools.shell.Main")
arg(line: "${new File(curDir, 'r.js').absolutePath}")
arg(line: "-o")
arg(line: "name=bootstrap")
arg(line: "baseUrl=${stagingDir}/js")
arg(line: "out=${stagingDir}/js/bootstrap-build.js")
@jurberg
jurberg / requirejs.gsp
Last active December 12, 2015 02:29
Use different Require.JS main when running in a war
<script src="js/lib/require-2.1.2.js"
data-main="js/bootstrap${grailsApplication.isWarDeployed() ? '-build' : ''}">
</script>
define('view/person', ['domain/person', 'jQuery', 'window'], function(Person, $, Win) {
var person = Person.createPerson();
return {
showPerson: function() {
$('#person-div').html(person.name);
Win.alert('Person updated!');
}
};
<a onclick="view.person.showPerson(); return false;">Show person</a>
<div id="person-div"></div>
app.all('/:controller/:action/:id?', function(req, res) {
require(['app/controller/' + req.params.controller], function(Controller) {
Controller[req.params.action](req, res);
});
});
app.all('/:controller', function(req, res) {
require(['app/controller/' + req.params.controller], function(Controller) {
Controller.index(req, res);
});
UrlMappings.config.forEach(function(mapping) {
app[mapping.verb](mapping.route, function(req, res) {
require(['app/controller/' + mapping.controller], function(Controller) {
Controller[mapping.action](req, res);
});
});
});
<r:require module="test_dialog"/>
<div id="test-dialog" title="Test Dialog">
<div class="test-dialog-content">
This is a test.
</div>
</div>
modules = {
application {
resource url:'js/application.js'
}
test_dialog {
dependsOn 'jquery, jquery-ui'
resource url:'css/testDialog.css'
resource url:'js/testDialog.js'
}
}
.test-dialog-content {
font-weight: normal;
color: red;
}
var TestDialog = (function($) {
var $dialog;
$(function() {
$dialog = $('#test-dialog');
$dialog.dialog({
autoOpen: false,
width: 300,
height: 200