- SUMMARY
- DESCRIPTION
- HOW-TO
A quite basic implementation of Core-Sandbox-Module pattern. API is based on talks and slides by N. Zakas[1] and Eric Shepherd[2]. This implementation heavily relies upon a correct use of the revealing module pattern [3], and upon the publish/subscribe pattern. Base library (jQuery is used here) is lightly abstracted from sandbox and modules; some main functions are exposed like sandbox.DOM.find(selector) which is equivalent to $().
Aim of the pattern is providing a loosely coupled architecture for JS applications.
Architecture components are:
-
Base library Provides cross-browser functionalities. JQuery is used here.
-
Core object Is responsible for:
- Registering modules
- Starting modules
- Stopping modules
- Indirectly handling (via sandbox) pub/sub of messages
- Exposing some basic functions of base lib to sandbox
-
Sandbox Sandbox is responsible for:
- Providing access to common features to modules
- Provide a pub/sub API to modules
- Exposing some basic functions of core's base lib to sandbox
-
Module(s) Each module is defined by a Module Creator function, knows only about its Sandbox and is subject to these rules:
- Hands to yourself
– Only call your own methods or those on the sandbox
– Don't access DOM elements outside of your box
- Don't access non-native global objects
- Ask, don't take
- Anything else you need, ask the sandbox
- Don't leave your toys around – Don't create global objects
- Don't talk to strangers
– Don't directly reference other modules
- Don't mess with other module's markup
- Hands to yourself
– Only call your own methods or those on the sandbox
– Don't access DOM elements outside of your box
Basically is all about:
- Defining modules
- Registering modules
- Starting/stopping modules
var MyModule = function (sandbox){
return {
init: function(){
//Module initialization
},
destroy: function(){
//Module destruction
}
// put your code here, and use your sandbox as needed...
};
}
You can only have one unique name per module.
Core.register("myModule", MyModule);
Core.startAll();
[1] Nicholas Zakas' Core-Sandbox-Module
- http://msdn.microsoft.com/en-us/scriptjunkie/gg314983
- http://developer.yahoo.com/yui/theater/video.php?v=zakas-architecture
[2] Building a JavaScript Module Framework at Gilt
[3] Christian Heilmann’s Revealing-module pattern