Created
February 21, 2010 01:49
-
-
Save peta/310050 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 NamespaceManager = new function() { | |
// Used to store dependencies; Two Arrays because there are | |
// probably multiple dependencies on the same classes | |
var memberIds = [], | |
callbacks = []; | |
// Used to store ALL loaded classes | |
var loaded = []; | |
/** | |
* Called everytime a new namespace member is | |
* loaded. Checks for unsolved dependencies of | |
* the newly registered ns member; when found | |
* resolves these. | |
* | |
* @private | |
* @param {String} id Fully qualifing namespace ID | |
*/ | |
function registerMember(id, mem) { | |
if (loaded.indexOf(id) == -1) { | |
loaded.push(id); | |
var idx; | |
while ((idx = memberIds.indexOf(id)) > -1) { | |
callbacks[idx](id, mem); | |
memberIds[idx] = callbacks[idx] = null; | |
} | |
} | |
} | |
/** | |
* Set up "listeners" for registration of namespace | |
* members. When all specified namespace IDs were | |
* registered, the supplied callback function is called. | |
* | |
* @param {String, Array} ids Fully qualifing namespace ID(s) | |
* @param {Function} callback | |
*/ | |
this.addDependency = function(ids, callback) { | |
if (typeof ids == "string") { | |
// When already loaded -> fire immediately | |
if (loaded.indexOf(ids) > -1) { | |
return callback(); | |
} | |
// Else queue up | |
memberIds.push(ids); | |
callbacks.push(callback); | |
} | |
else if (ids.constructor === Array) { | |
// Senseless -> immediate execution | |
if (!ids.length) return callback(); | |
var cb = (function(len, cb) { | |
var i = len, | |
args = ids; | |
return function(id, mem) { | |
args[args.indexOf(id)] = mem; | |
if (!--i) cb.call(null, args); | |
i = args = null; | |
}; | |
})(ids.length, callback); | |
for (var i=0, j=ids.length; i < j; i++) { | |
if (loaded.indexOf(ids[i]) > -1) { | |
cb(); | |
} | |
else { | |
callbacks[memberIds.push(ids[i]) - 1] = cb; | |
} | |
} | |
} | |
}; | |
/** | |
* Registers the supplied member object under the | |
* specified namespace ID. Probably the member | |
* object will mostly be a Class; but other | |
* value types will work too. | |
* | |
* @param {String} id Fully qualifing namespace ID | |
* @param {Object} member | |
*/ | |
this.defineMember = function(id, member) { | |
var path = id.split("."), | |
j = (path.length > 1) ? path.length-1 : 0, | |
i = 0, | |
g = window; | |
while (i < j) { | |
g = g[path[i++]] || {}; | |
} | |
g[path[j]] = member; | |
registerMember(id, member); | |
}; | |
}; | |
// Handy aliases | |
$addDependency = NamespaceManager.addDependency; | |
$class = NamespaceManager.defineMember; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment