Skip to content

Instantly share code, notes, and snippets.

@TechNinjaWeb
Last active August 8, 2017 17:39
Show Gist options
  • Save TechNinjaWeb/0578ad5f4f3e5b51573c3a33ec31086b to your computer and use it in GitHub Desktop.
Save TechNinjaWeb/0578ad5f4f3e5b51573c3a33ec31086b to your computer and use it in GitHub Desktop.
Easily create an encapsulated namespace for your angular app's components, controllers, services, and other properties you want accessible from the console.
/*****************************************************************************************************
* Usage Example
*****************************************************************************************************/
// Get reference and set namespace
const WF = new WindowFactory('myCoolApp')
// Set Root
WF.root({name: 'MainModule'})
// Make additional Components
WF.make({name: 'SecondaryModule'})
// Attach components to any module by name
WF.modules[ 'MainModule' ].run.myCoolProperty = "Hey It's working";
// Attach a component to another module
WF.modules[ 'SecondaryModule' ].run.myCoolProperty = "ANother cool property";
/******************************************************************************************************
******************************************************************************************************/
function WindowFactory( namespace ) {
// Define Important Variables
var self = window[ namespace ] || {};
self.namespace = namespace;
self.modules = {};
self.rootSet = false;
self.installed = [];
// Return Factory Controls
self.root = function( app ) {
// Debugging
// console.dir(self);
// Add To Installed List
self.installed.push( app.name );
// Die if already set
if (self.rootSet) return self.modules.root;
// Set Root True
self.rootSet = true;
// Define The Child Module Namespace
self.modules[ app.name ] = {};
self.modules[ app.name ].app = app;
self.modules[ app.name ].controllers = {};
self.modules[ app.name ].services = {};
self.modules[ app.name ].directives = {};
self.modules[ app.name ].run = {};
// Save Ref To This As Root of Modules
self.modules.root = self.modules[ app.name ];
// Return The Child Module
return window[ self.namespace ] = self;
};
self.make = function( app ) {
self.installed.push( app.name );
// Define The Child Module Namespace
window[ self.namespace ].modules[ app.name ] = {};
window[ self.namespace ].modules[ app.name ].app = app;
window[ self.namespace ].modules[ app.name ].controllers = {};
window[ self.namespace ].modules[ app.name ].services = {};
window[ self.namespace ].modules[ app.name ].directives = {};
window[ self.namespace ].modules[ app.name ].run = {};
// Return The Child Module
return window[ self.namespace ];
};
// Invoke Self On Winodw Now
return (function(n){
return window[ n ] = self;
}(namespace))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment