Skip to content

Instantly share code, notes, and snippets.

@a-musing-moose
Created May 1, 2013 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-musing-moose/5493166 to your computer and use it in GitHub Desktop.
Save a-musing-moose/5493166 to your computer and use it in GitHub Desktop.
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