Last active
December 19, 2015 15:09
-
-
Save latentflip/5974376 to your computer and use it in GitHub Desktop.
Scoutfile used at Float developed around the concepts presented in @SlexAxton's post: http://alexsexton.com/blog/2013/03/deploying-javascript-applications/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ScoutFile - MIT (c) @philip_roberts | |
// This file should be as close to the top of you html as possible, and be cached for | |
// a short time. | |
// | |
// The configurable section should define a `loadScripts` function | |
// which uses addCssTag and addScriptTag to load in the required | |
// scripts. loadScripts should also call onScriptsLoaded when it's | |
// imported all the scripts it needs (probably in the addScriptTag onDone callback). | |
// | |
// addCssTag(url) - injects the css file at url into the page | |
// addScriptTag(url, onDone) - injects the javascript file at url into the page, | |
// and calls onDone when it's been imported | |
// | |
// The scoutfile also exposes a $R function on window. You can use this to define | |
// callbacks that will not be called until `onScriptsLoaded` is called, ie: | |
// | |
// $R(function() { | |
// $(function() { | |
// alert('hi'); | |
// }) | |
// }) | |
// | |
// will not be run until the script tags have all loaded (which will presumably | |
// include jQuery). This prevents the script which depends on jQuery running before | |
// $ has been loaded | |
// NB: this example will be compiled by grunt, where the deployEnv and buildNumber values are interpolated at grunt-time. | |
(function() { | |
//Configurable | |
var base, loadScripts; | |
base = 'https://<your subdomain>.cloudfront.net/envs/<%= deployEnv %>/b<%= buildNumber %>/'; | |
loadScripts = function() { | |
addScriptTag(base+'app-min.js', onScriptsLoaded); | |
addCssTag(base+'app-min.css'); | |
}; | |
//Private | |
var firstTag = document.getElementsByTagName('script')[0], | |
callbacks = [], | |
scriptsLoaded = false, | |
$R, | |
onScriptsLoaded; | |
window.$R = $R = function(cb) { | |
if (scriptsLoaded) cb(); | |
else callbacks.push(cb); | |
}; | |
onScriptsLoaded = function(cb) { | |
for (var i=0,ii=callbacks.length; i<ii; i++) { | |
callbacks[i](); | |
} | |
scriptsLoaded = true; | |
}; | |
function addScriptTag(f, cb) { | |
var tag = mkTag('script'); | |
tag.src = f; | |
tag.type = 'text/javascript'; | |
addOnloadHandler(tag, cb); | |
putTag(tag); | |
} | |
function addCssTag(f, cb) { | |
var tag = mkTag('link'); | |
tag.type = "text/css"; | |
tag.rel = "stylesheet"; | |
tag.href = f; | |
putTag(tag); | |
} | |
function mkTag(t) { | |
return document.createElement(t); | |
} | |
function putTag(tag) { | |
firstTag.parentNode.insertBefore(tag, firstTag); | |
} | |
function addOnloadHandler(tag, cb) { | |
if (!cb) return; | |
if (typeof tag.addEventListener !== "undefined") { | |
tag.addEventListener("load", cb, false); | |
} else { | |
tag.onreadystatechange = function() { | |
script.onreadystatechange = null; | |
ieLoadBugFix(script, cb); | |
}; | |
} | |
} | |
function ieLoadBugFix(scriptEl, cb) { | |
if (scriptEl.readyState=='loaded' || scriptEl.readyState == 'completed') { | |
cb(); | |
} else { | |
setTimeout(function() { ieLoadBugFix(scriptEl, cb) }, 100); | |
} | |
} | |
loadScripts(); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment