Created
February 7, 2011 08:13
-
-
Save jrburke/814133 to your computer and use it in GitHub Desktop.
How one piece of functionality could be created out of many files.
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
/* | |
Define a library called foo that has a parse and render | |
functionality, and a utility function called escape. | |
*/ | |
/** foo.js **/ | |
//If wanting a global, declare it here | |
var foo; | |
//Assemble foo from parts | |
define([ | |
//exports is special, it creates an empty object | |
//for foo immediately. | |
'exports', | |
//loads parse.js as a sibling to foo.js | |
'./parse', | |
'./compile', | |
'./util/escape' | |
], function (exports) { | |
//Optionally create a global object | |
//for apps that just want to use | |
//<script src="foo.js"> and use | |
//a global "foo" object to interact. | |
foo = exports; | |
}); | |
/** foo/parse.js **/ | |
//Asking for './foo' will get the exports object | |
//created in foo.js. | |
define(['./foo'], function(foo) { | |
foo.parse = function () {}; | |
}); | |
/** foo/render.js **/ | |
define(['./foo'], function(foo) { | |
foo.render = function () {}; | |
}); | |
/** foo/util/escape.js **/ | |
define(['../foo'], function(foo) { | |
foo.escape = function () {}; | |
}); | |
/** RequireJS optimizer can turn the above into one file. **/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment