Skip to content

Instantly share code, notes, and snippets.

@larscwallin
Created February 7, 2012 11:25
Show Gist options
  • Save larscwallin/1759223 to your computer and use it in GitHub Desktop.
Save larscwallin/1759223 to your computer and use it in GitHub Desktop.
simplx.js
simplx = {
_debugMode:false,
_namespaceSeparator:'.',
_extend:function(ns,component){
var nsArray;
var targetLocation = this; // First point at the base namespace/Object
var currentNs = '';
if(ns !== '' && component instanceof Object){
if(ns.indexOf(this._namespaceSeparator)>0){
if(this._debugMode){console.log('Got namespaces');}
nsArray = ns.split(this._namespaceSeparator);
for(part in nsArray){
currentNs = nsArray[part];
if(this._debugMode){console.log('Current namespace = '+currentNs);}
// Does this namespace exists in the current context?
if(targetLocation.hasOwnProperty(currentNs)){
if(this._debugMode){console.log(currentNs+' is an existing namespace.');}
// As the namespace exists we re-point the targetLocation.
targetLocation = targetLocation[currentNs];
}else{
if(part < nsArray.length-1){
console.log(currentNs+' is not an existing namespace.');
return false;
}else{
console.log(currentNs+' is added.');
targetLocation[currentNs] = component;
return targetLocation[currentNs];
}
}
}
}else{
// Just one single namespace which means its going in the root of the simplx object
return this[ns] = component;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment