Revision: 12102
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 2, 2009 15:13 by johnloy
Initial Code
/* http://www.wait-till-i.com/2008/05/23/script-configuration/ */ var module = function(){ // configuration, change things here var config = { CSS:{ classes:{ hover:'hover', active:'current', jsEnabled:'js' }, ids:{ container:'maincontainer' } }, timeout:2000, userID:'chrisheilmann' }; // start of main code function init(){ changeConfig.set(arguments[0]); console.log(config); }; // … more methods and other code … // Configuration changes var changeConfig = function(){ function set(o){ var reg = /./g; if(isObj(o)){ for(var i in o){ if(i.indexOf(’.')!== -1){ var str = ‘["' + i.replace(reg,'"]["') + '"]‘; var val = getValue(o[i]); eval(’config’ + str + ‘=’ + val); } else { findProperty(config,i,o[i]); } } } }; function findProperty(o,p,v){ for(var i in o){ if(isObj(o[i])){ findProperty(o[i],p,v); } else { if(i === p){o[p] = v;}; } } }; function isObj(o){ return (typeof o === ‘object’ && typeof o.splice !== ‘function’); }; function getValue(v){ switch(typeof v){ case ’string’: return "’"+v+"’"; break; case ‘number’: return v; break; case ‘object’: if(typeof v.splice === ‘function’){ return ‘['+v+']‘; } else { return ‘{’+v+’}'; } break; case NaN: break; }; }; return{set:set}; }(); // make init a public method return { init:init }; }(); module.init({ ‘container’:'header’, ‘CSS.classes.active’:'now’, ‘timeout’:1000 });
Initial URL
Initial Description
Initial Title
Way to programmatically override a config object for a module pattern
Initial Tags
javascript, textmate
Initial Language
Other