You still get to choose the best tool for the job
You buy into a small set of transferable rules
You buy into a philosophical module
define() functionThe specification defines a single function "define" that is available as a free variable or a global variable.
Using an ID is not recommended unless necessary.
var testModule = (function(){
var counter = 0;
return {
incrementCounter: function() {
console.log(counter++);
},
resetCounter: function() {
console.log('counter value prior to reset:' + counter);
counter = 0;
}
};
})();
testModule.incrementCounter();
testModule.incrementCounter();
testModule.resetCounter();
var testModule = (function(){
var counter = 0;
return {
incrementCounter: function() {
console.log(counter++);
},
resetCounter: function() {
console.log('counter value prior to reset:' + counter);
counter = 0;
}
};
})();
testModule.incrementCounter();
testModule.incrementCounter();
testModule.resetCounter();
function(){
var counter = 0;
return {
incrementCounter: function() {
console.log(counter++);
},
resetCounter: function() {
console.log('counter value prior to reset:' + counter);
counter = 0;
}
};
}
function(){
var counter = 0;
return {
incrementCounter: function() {
console.log(counter++);
},
resetCounter: function() {
console.log('counter value prior to reset:' + counter);
counter = 0;
}
};
}
define( function(){
var counter = 0;
return {
incrementCounter: function() {
console.log(counter++);
},
resetCounter: function() {
console.log('counter value prior to reset:' + counter);
counter = 0;
}
};
});
/* et voila */
define(['TestModule'], function(TestModule) {
TestModule.incrementCounter();
return { /* stuff */ }
});
define(function() {
function Shape(x,y) {
this.x = x;
this.y = y;
}
Shape.prototype.move = function(x,y) {
this.x += x;
this.y += y;
}
return Shape
});
define(['Shape'],function(Shape) {
function Circle(x,y,radius) {
Shape.call(this,x,y);
this.radius = radius;
}
Circle.prototype = new Shape();
Circle.prototype.constructor = Circle;
return Circle;
});
define({
'foo' : 42,
'bar' : Math.PI
});
Think JSONP with the callback always being define()
define(['cs!coffeescriptModule.cs'],function(csModule){
csModule.foo(); // ready to use in JavaScript
})
Some common plugins
text! : A text file dependencydomReady! : Cross browser domready solutioncss! : Dynamically require cssorder! : Force ordered evaluationproject-root/
index.html
scripts/
lib/
require.js
dep1.js
dep2.js
main.js
<html>
<head>
<script src="scripts/lib/require.js"></script>
</head>
<body>
</body>
</html>
<html>
<head>
<script data-main="scripts/main.js"
src="scripts/lib/require.js"></script>
</head>
<body>
</body>
</html>
Instruct require.js to run scripts/main.js on load
<html>
<head>
<script data-main="scripts/main.js"
src="scripts/lib/require.js"></script>
</head>
<body>
</body>
</html>
The scripts/ directory will be the baseUrl for our modules
require(['dep1','dep2'],function(dep1,dep2){
// And away we go!
})
$ npm install requirejs
$ r.js -o app.build.js
where app.build.js is your build configuration file
({
baseUrl: ".",
name: "main",
out: "main-built.js",
modules: [
{
name: "main"
}
]
})
r.js can also copy your source tree into a new build dir, and minify and build your CSS.
There is much more! Stay up to date and start coding now!
AMD/RequireJS is not a silver bullet, but it gets us to a better place
@jrburke - require.js
@unscriptable - curl.js
@addyosmani - writer and knowledge machine
The presentation thanks Deck.js, jQuery sparklines, Codemirror plugin for Deck.js,
/
#