Created
May 1, 2013 01:20
-
-
Save a-musing-moose/5493166 to your computer and use it in GitHub Desktop.
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
var site = (function ($) { | |
"use strict"; | |
if (typeof console === 'undefined') { | |
var console = { | |
log: function (message) { | |
// noop | |
} | |
}; | |
} | |
function Site () { | |
var namespaces = {}; | |
/** | |
* Adds a new namespace | |
* | |
* @var string name | |
* @var object definition | |
* | |
**/ | |
this.ns = function (name, definition) { | |
if (typeof namespaces[name] !== 'undefined') { | |
throw Error(name + " is already registered"); | |
} | |
namespaces[name] = definition; | |
if (typeof Object.defineProperty == 'function') { | |
try { | |
Object.defineProperty(this, name, { | |
get: function () { | |
return namespaces[name]; | |
} | |
}); | |
} catch (e) { | |
//Bloody IE! | |
} | |
} | |
if (!this[name]) { | |
//fallback for those where defineProperty doesn't work | |
this[name] = namespaces[name]; | |
} | |
console.log("Registered ns: " + name); | |
} | |
/** | |
* Initialises all namespaces | |
**/ | |
this.load = function () { | |
console.log("Initialising..."); | |
for (name in namespaces) { | |
if (namespaces.hasOwnProperty(name)) { | |
var ns = namespaces[name]; | |
if (typeof ns.load === 'function') { | |
console.log("Calling load on ns: " + name); | |
ns.load(); | |
} | |
} | |
} | |
} | |
}; | |
var site = new Site(); | |
$(document).ready(function () { | |
site.load(); | |
}); | |
return site; | |
})(jQuery); | |
// Register settings | |
site.ns('settings', { | |
//add settings here | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment