Last active
March 1, 2016 23:06
-
-
Save hwclass/335610035971df6ba1c8 to your computer and use it in GitHub Desktop.
Design Patterns : Module Pattern
This file contains 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
/** | |
* @fileoverview The setter for the modules into the main context | |
* @author hwclass | |
*/ | |
'use strict'; | |
//----------------------------------------------------------- | |
// Public | |
//----------------------------------------------------------- | |
/** | |
* register method | |
* @param {Object} Module | |
* @param {Function} | |
*/ | |
register.module(new Module('testModule', function () { | |
console.log('testModule initialized.'); | |
})); | |
/** | |
* Initialization invoked | |
* @returns {void} | |
*/ | |
init.module(‘testModule’); |
This file contains 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
/** | |
* @fileoverview The global file to attach modules into an array called MODULES | |
* @author hwclass | |
*/ | |
/** | |
* global.js | |
* This part of code is the main layer that the other | |
* modules are attached into an array to be fetched | |
* during an immediate usage in this tiny structure | |
*/ | |
'use strict'; | |
//----------------------------------------------------------- | |
// Public | |
//----------------------------------------------------------- | |
/** @type {Array} */ | |
var MODULES = MODULES || []; |
This file contains 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
/** | |
* @fileoverview The main initialization method to start modules working | |
* @author hwclass | |
*/ | |
/** | |
* init.js | |
* This method realize the binded modules in our application | |
* with their context to be invoked | |
*/ | |
'use strict'; | |
//----------------------------------------------------------- | |
// Public | |
//----------------------------------------------------------- | |
/** | |
* register method | |
* @returns {Function} module | |
*/ | |
var init = (function () { | |
return { | |
/** | |
* module method | |
* @param {String} moduleName | |
*/ | |
module : function (moduleName) { | |
for (var counter = 0, len = MODULES.length; counter < len; counter++) { | |
if (MODULES[counter]['name'] === moduleName) { | |
MODULES[counter]['context'](); | |
} | |
} | |
} | |
} | |
})(); |
This file contains 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
/** | |
* @fileoverview The base object to generate modules | |
* @author hwclass | |
*/ | |
/** | |
* models/Module.js | |
* This object is used to generate some instances | |
* as modules to be attached into the MODULES array. | |
*/ | |
'use strict'; | |
//----------------------------------------------------------- | |
// Public | |
//----------------------------------------------------------- | |
/** | |
* Module object | |
* @param {String} name | |
* @param {Function} context | |
*/ | |
var Module = function (name, context) { | |
this.name = name; | |
this.context = context; | |
} |
This file contains 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
/** | |
* @fileoverview The module registration file to attach modules into the context | |
* @author hwclass | |
*/ | |
/** | |
* register.js | |
* This method sets the module with two parameter | |
* called name as string and context as a pure function | |
* into MODULES array as a Module reference | |
*/ | |
'use strict'; | |
//----------------------------------------------------------- | |
// Public | |
//----------------------------------------------------------- | |
/** | |
* register method | |
* @noparam | |
*/ | |
var register = (function () { | |
return { | |
module : function (module) { | |
MODULES.push(new Module(module.name, module.context)); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment