A short introduction to Require.JS and JavaScript loaders with a healthy amount of LOLCats sprinkled in. Used as an internal presentation to help teams modularize javascript applications. Try opening the presenter view & looking at notes (I leave the presentations pretty bare)
13. //A few convenient extras:
require(["text!template.html"], function(template){
//The text is passed in as a string into the function
console.log(template);
});
//-----------------------------
require.ready(function(){
//fires once the dom is loaded - often in other libraries,
but very convenient nonetheless
});
//-----------------------------
require(['some/module'], function(){
//can nest requires
require(['a.js','b.js']);
});
//-----------------------------
//Conveniently, this also works:
var util = require(['util']);
1. DEPENDENCIES - No way to cross-reference javascript (dependencies)\nEvery page needs to repeat the list of scripts, maintain it’s own set of dependencies\n
2. SPEED - Each new script that is included blocks the browser until it is done downloading & runs\n\n
3. EXECUTION ORDER - Managing order of downloading & running scripts- what comes before what & what needs to be on the page afterwards\n
\n
The easiest way to use it is to just load require.js on the page and point it to a single file that contains the scripts- it will download all scripts in parallel & execute in order\n
A single main file that can load any dependencies\n
Modules are where require.js really shines.\nJavaScript’s biggest problem is that it was designed to be written and run directly in the browser- they didn’t have to worry about how to manage dependencies in between scripts- they didn’t expect it to grow like it did. Require.js solves this problem for us.\n\n
Once you define a module, you can then use that module in any require script and directly call that script as a function\n
\n
Modules can be named, can require other modules (or files), and pass those modules into the module function\n
\n
\n
Assuming a structure like above, you can build a build script that can trace down through all of your files and build a minified version of each, as well as an overall minified project file\n