Code Monkey home page Code Monkey logo

mod's Introduction

mod

mod is a function for defining and loading JS modules from a browser. It serves as a method to break your project into a tree of maintainable pieces that depend on each other, much like an include or import statement. Once your project is loaded you may use mod to compile your code into one monolithic closure.

Use

mod uses initialization objects (called packages, internally) to define modules. An initialization object takes a name, an array of dependencies and an init function, which by my convention always returns a constructor.

Include mod.js in your <head>:

<script src="mod.js" type="text/javascript" charset="utf-8"></script>

and then set up your a module:

<script type="text/javascript" charset="utf-8">
	// create a main module
		mod({
			name : 'SomeModule',
			dependencies : [
				'path/to/AnotherModule.js',
				'path/to/YetAnotherModule.js'
			],
			init : function initMain(AnotherModule, YetAnotherModule) {
				// We can access AnotherModule and YetAnotherModule because mod.js
				// initializes dependencies in order and provides them to your init
				// in the order that you declare them in the dependencies array above...
				function SomeConstructor() {
					this.foo = "bar";
				}
				SomeConstructor.prototype = {
					anotherInstance : new AnotherModule(),
					yetAnotherInstance : new YetAnotherInstance()
				};
				return SomeConstructor;
			}
		});
</script>

In this first call mod packages your module and starts loading its dependencies (either through XMLHttpRequest or script tag injection). Once the dependencies are loaded, which probably define more modules and load more scripts, the result of the init function is stored in mod.modules, in this case as mod.modules.SomeModule, so they don't clutter global space. The initialized modules are provided to other init functions in the order they are asked for.

As an added benefit, you can share data between modules using mod.modules.

Compiling

If you use mod to write lots of modules (when you're making a big project) mod can 'compile' your project for you, removing all instances of mod. It essentially takes all your init functions and prints them to one monolithic file, which you can then compress with YUI or Google Closure. To do this, load your project in your browser, open the js console and type mod.printCompilation(). Alternatively, to store in a string, type var compilation = mod.compile();.

Examples

For more examples check out my other project bang at https://github.com/schell/bang Notes

In development situations the scripts mod loads can change often. In order to avoid the browser cacheing these files (and returning an old version of your scripts) set mod.nocache = true, which will enable the "force re-download" feature. Unfortunately with mod.nocache set to true, many javascript debuggers can't set breakpoints on the loaded scripts. Keep this in mind.

mod's People

Contributors

schell avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mod's Issues

Ability to define modules within a module init() or callback()

Right now there is no way to define a module inside the init or callback function of another module.
Doing so causes two runs of initPackages - the first calls the initial init() (in which the second module is defined) and the second after that module is done loading.

Needs dependency resolution using trees.

mod should build a dependency tree (instead of list), resolving circular dependencies as best it can, loading the leaves and reducing the tree on up until the root is loaded.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.