Skip to content

Instantly share code, notes, and snippets.

@neonstalwart
Created May 16, 2010 16:04
Show Gist options
  • Save neonstalwart/402960 to your computer and use it in GitHub Desktop.
Save neonstalwart/402960 to your computer and use it in GitHub Desktop.
// this is 'raw' javascript to load dojo, stylesheets, the app, etc.
// this file is all that needs to be included in the host page.
// this technique doesn't dojo.addOnLoad because it assumes that the
// app code will use dojo.addOnLoad as necessary.
// also, this doesn't load dojo.css as a 'reset' since it assumes that
// it is being loaded into an already established environment and it
// doesn't include loading the css for a dijit theme.
(function(){
djConfig = {
afterOnLoad: true,
baseUrl: './',
modulePaths: {
myApp: 'src/myApp',
styles: 'src/styles'
}
};
// setup the environment and inject dojo into the page
var cdn = 'http://ajax.googleapis.com/ajax/libs/dojo/1.4/',
dojoUrl = cdn + 'dojo/dojo.xd.js',
dijitCss = cdn + 'dijit/themes/dijit.css',
readyRE = /^(complete|loaded)$/,
d = document,
head = d.getElementsByTagName("head")[0],
node = d.createElement('script');
// the code to add a style sheet is adapted from dijit._editor.RichText.addStyleSheet
function addStyleSheet(url){
var sheet;
if(d.createStyleSheet){
// IE
d.createStyleSheet(url);
} else {
// other browsers
sheet = d.createElement('link');
sheet.rel = 'stylesheet';
sheet.type = 'text/css';
sheet.href = url;
head.appendChild(sheet);
}
}
// the script loading code is adapted from RequireJS
// - http://github.com/jrburke/requirejs/blob/master/require.js
function scriptLoad(e){
var node = e.target || e.srcElement, url;
if (e.type == 'load' || readyRE.test(node.readyState)) {
url = dojo.moduleUrl('styles', 'appStyles.css').toString();
addStyleSheet(dijitCss);
addStyleSheet(url);
// load the application
dojo.require('myApp.app');
if (node.removeEventListener) {
// good browsers
node.removeEventListener('load', scriptLoad, false);
} else {
// probably IE
node.detachEvent('onreadystatechange', scriptLoad);
}
}
}
node.type = 'text/javascript';
node.charset = 'utf-8';
if(node.addEventListener){
// good browsers
node.addEventListener('load', scriptLoad, false);
}
else{
// probably IE
node.attachEvent('onreadystatechange', scriptLoad);
}
node.src = dojoUrl;
head.appendChild(node);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment