Skip to content

marcusphillips/module.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

MODULE helps me write robust, configrable JS libraries declaratively.  I incorporate any best practices for library and module creation that I find here, rather than leaving delicate, magical code in every library I write.

Here's an example of how you can can use MODULE:


-------------------------------
### awesome_tools.js ###
-------------------------------

/*
* My Awesome Tools
* by Fabulous McGee
*/
MODULE('awesomeTools', {

  // Define your module's default configuration here.
  // you can specify any other settings you like, and the user may override them elsewhere
  option1: 'blue',
  option2: 100,
  exposeLocals: false,

  // the attachTo property is handled specially by MODULE
  // if set to object, your module will be attached to that object
  // if set to a string, the string will be interpreted as a path
  // from global scope where the parent object can be found
  // By default, it is attached to the global object
  // In this example, awesome_tools will act as a jQuery plugin
  attachTo: 'jQuery'

}, function(moduleConfig){

  // This anonymous function scope provides you a local namespace that will not pollute the global one
  var a=1, b=2, c=3;

  // However, to help manage private local variables, MODULE also provides a _VARS function
  // Putting things here rather than declaring them with as var statments allows turnkey
  // debugging access to sensitive data as well as important properties that you would
  // like to keep hidden because they are simply unsupported by your API
  var _ = _VARS({
    secretSauce: 'peanutButter',
  });

  // You can now define your module's public interface
  // The methods of this object will have privilaged scope access to this anonymous function
  var myModule = {
    handyTool: function(){},
    usefulWidget: function(){
      // You can easily access your private variables using the "_." notation:
      return someRPC(_.secretSauce);
    }
  }

  // if you would like to provide access to the local variables, you can do so by augmenting the module with your _ object
  // in this example, such behavior has been parameterized, to allow the original importer of the module exclusive control of this feature
  if(moduleConfig.exposeLocals){
    myModule._ = _;
  }

  // the result returned from this function will be added the value at moduleConfig.attachTo[ moduleconfig.key || 'awesomeTools' ]
  return result;
});



-----------------------------------------
### elsewhere/awesome_tools.config.js ###
-----------------------------------------

// Your module can be configured by the user without editing your source code, simply by calling MODULE.configure()
// any settings provided here will mask the ones defined in the call to MODULE() at the top of awesome_tools.js
MODULE.configure('awesomeTools', {
  option1: 'red',
  option2: 26
});



-------------------------------

TODO
- add tests
- add support for dependencies

About

Helper tools for making configurable JavaScript libraries

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published