Skip to content

Instantly share code, notes, and snippets.

@jrburke
Created February 7, 2011 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jrburke/814133 to your computer and use it in GitHub Desktop.
Save jrburke/814133 to your computer and use it in GitHub Desktop.
How one piece of functionality could be created out of many files.
/*
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