Skip to content

Instantly share code, notes, and snippets.

Created September 6, 2013 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6470443 to your computer and use it in GitHub Desktop.
Save anonymous/6470443 to your computer and use it in GitHub Desktop.
Working example of RequireJS modules.. I think I'm doing it right but I'm not sure. Goes with http://stackoverflow.com/questions/18619245
<html>
<head>
...
<script data-main="" src="libraries/require.js"></script>
<script type="text/javascript">
require(['simulatorConfiguration.js',
'modelConfiguration.js',
'libraries/jquery-1.10.2.min',
'libraries/jquery.lightbox_me.js',
'libraries/jquery-migrate-1.2.1.js',
'libraries/raphael-min.js',
'loadPage'], function(
simulatorConfiguration,
modelConfiguration,
$,
lightbox,
migrate,
raphael,
loadPage) {
loadPage();
});
//hide all divs
//function hideAllDivs() {...}
define('hideAllDivs', [], function()
{
return function ()
{
//jquery stuff
};
});
//function that get's called on body onload
//function loadPage() {...}
define('loadPage', ['hideAllDivs', 'createModelMenu', 'setUpLightBoxes'], function(hideAllDivs, createModelMenu, setUpLightBoxes)
{
return function()
{
hideAllDivs();
//jquery stuff
createModelMenu();
//jquery stuff
setUpLightBoxes();
};
});
//function setUpLightBoxes() {...}
define('setUpLightBoxes', ['takeControl', 'libraries/jquery.lightbox_me.js', 'giveUpControl'], function(takeControl, lightbox, giveUpControl)
{
return function()
{
//set up "take control" lightbox's on page load:
$('#loginLink').mousedown(function() {
$("#takeControl").lightbox_me({centered: true, onLoad: function() {
$("#takeControl").find("input:first").focus();
}});
//e.preventDefault();
});
$('#releaseLink').click(function() {
$("#giveUpControl").lightbox_me({centered: true, onLoad: function() {
$("#giveUpControl").find("input:first").focus();
}});
//e.preventDefault();
});
$('table tr:nth-child(even)').addClass('stripe');
//set up mouse events for the lightbox's
$('#takeControlSubmit').mousedown(function() { takeControl(); });
$('#releaseControlSubmit').mousedown(function() { giveUpControl(); });
};
});
//For every model in "modelConfiguration.js", create a navigation menu item for it.
//function createModelMenu() {...}
define('createModelMenu', ['createTestMenu'], function (createTestMenu)
{
return function()
{
for (var i=0; i<Object.keys(models).length; i++)
{
//jquery stuff
$("#"+model_key).mousedown(function() { createTestMenu(model_key); });
}
};
});
//When a model is selected in the navagation menu, create a test dropdown for each test given in "modelConfiguration.js"
//function createTestMenu(model_key) {...}
define('createTestMenu', ['showModelInformation', 'showTest'], function (showModelInformation, showTest)
{
return function(model_key)
{
//jquery stuff
//import the test configuration js file for this model:
var javascriptLoc = "models/" + models[model_key].modelDir + "/testConfiguration.js";
require([javascriptLoc], function(util) {
showModelInformation(model_key);
//jquery stuff
for (var i=0; i<Object.keys(tests).length; i++)
{
//jquery stuff
$("#"+test_key).mousedown(function() { showTest(test_key); });
}
});
};
});
//Shows model intakeControlation (when clicking a model from the navigation menu, but before clicking on a test)
//function showModelInformation(model_key) {...}
define('showModelInformation', ['hideAllDivs', 'showTest'], function (hideAllDivs, showTest)
{
return function(model_key)
{
hideAllDivs();
//jquery stuff
for (var i=0; i<Object.keys(tests).length; i++)
{
//jquery stuff
$("#"+test_key+"testLink").mousedown(function() { showTest(test_key); });
}
};
});
//function showTest(test_key) {...}
define('showTest', ['hideAllDivs', 'showLegend'], function (hideAllDivs, showLegend)
{
return function(test_key)
{
hideAllDivs();
//jquery stuff
showLegend();
};
});
define('showLegend', ['libraries/raphael-min.js'], function (raphael)
{
return function()
{
//RaphaelJS stuff
};
});
//TODO: IMPLEMENT SESSIONS / COOKIES TO KEEP TRACK OF WHO HAS CONTROL
//function takeControl() {...}
define('takeControl', [], function()
{
return function()
{
//jquery stuff
};
});
//function giveUpControl() {...}
define('giveUpControl', [], function()
{
return function()
{
//jquery stuff
};
});
</script>
</head>
<body>
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment