Skip to content

Instantly share code, notes, and snippets.

@dhigginbotham
Created February 19, 2013 01:33
Show Gist options
  • Save dhigginbotham/4982344 to your computer and use it in GitHub Desktop.
Save dhigginbotham/4982344 to your computer and use it in GitHub Desktop.
//scripts.schema.js
//this schema will allow us to conditionally load stuff, without a huge mess.
var scriptLoader = {};
scriptLoader.files = [
//header scripts
{path:'//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js', name:'jquery', where:'head', url:'/', type: 'js'},
{path:'//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/js/bootstrap.min.js', name:'bootstrap', where:'head', url: '/', type: 'js'},
{path:'/socket.io/socket.io.js', name:'socket-io', where:'head', url: '/', type: 'js'},
{path:'//code.shutterstock.com/rickshaw/vendor/d3.v2.js', name:'d3', where:'head', url: '/dashboard', type: 'js'},
{path:'//code.shutterstock.com/rickshaw/rickshaw.min.js', name:'rickshaw', where:'head', url: '/dashboard', type: 'js'},
{path:'/css/style.css', name: 'stylesheet', where: 'head', url: '/', type: 'css'},
{path:'/css/flexslider.vendor.css', name: 'flexslider', where: 'head', url: '/p/view/{ln}', type: 'css'},
//footer scripts
{path:'/js/vendor/jquery.flexslider-min.js', name: 'flexslider-js', where: 'foot', url: '/p/view/{ln}', type: 'js'},
{path:'/js/view.client.js', name: 'view-client', where: 'foot', url: '/p/view/{ln}', type: 'js'},
{path:'/js/top5.client.js', name:'top5', where:'foot', url: '/dashboard', type: 'js'},
{path:'/js/rickshaw.client.js', name:'rickshaw-client', where:'foot', url: '/dashboard', type: 'js'},
{path:'/js/socket.client.js', name:'socket-io-client', where:'foot', url: '/', type: 'js'},
];
exports.ManageScriptLoader = function(request, type, callback) {
var internals = scriptLoader.files;
var loaded = {
head: [],
foot: []
};
for(var i=0;i<internals.length;++i) {
var f = internals[i];
if (f.url == '/p/view/{ln}') {
f.url = '/p/view/' + request.params.ln;
}
if ( (f.where === 'head' && f.url === request.url.pathname && f.type === type ) || (f.where === 'head' && f.url === '/' && f.type === type ) ) {
loaded.head.push(f);
}
if ( (f.where === 'foot' && f.url === request.url.pathname && f.type === type ) || (f.where === 'foot' && f.url === '/' && f.type === type ) ) {
loaded.foot.push(f);
}
}
return (callback) ? callback(loaded) : loaded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment