Skip to content

Instantly share code, notes, and snippets.

@Leikam
Last active August 29, 2015 14:17
Show Gist options
  • Save Leikam/5b3b3836cf2fcbe9b22e to your computer and use it in GitHub Desktop.
Save Leikam/5b3b3836cf2fcbe9b22e to your computer and use it in GitHub Desktop.
(function () {
'use strict';
/**
* @author <a href="mailto:[email protected]">Konstantin Kitmanov</a>
* May be freely distributed under the MIT license.
*/
function ResourceRegistry () {
/** @private */
this._storage = {};
/** @private */
this._fabric = {};
}
ResourceRegistry.prototype = {
register: function (key, value, options) {
if (arguments.length == 3) {
return this._fabric[key] = [value, options]
} else {
return this._storage[key] = value;
}
},
unregister: function (key, isFabric) {
if (isFabric) {
delete this._fabric[key]
}
else {
delete this._storage[key];
}
},
acquire: function (key, options) {
if (arguments.length == 2) {
if (this._fabric[key]) {
var fabric = this._fabric[key],
create = fabric[0],
params = _.extend({}, fabric[1], options);
return create(params)
} else {
return this._storage[key] || options;
}
} else {
return this._storage[key]
}
}
};
// conflict management — save link to previous content of ResourceRegistry, whatever it was.
var root = this,
prevName = root.ResourceRegistry;
/**
* Cleans global namespace, restoring previous value of window.ResourceRegistry, and returns ResourceRegistry itself.
* @return {ResourceRegistry}
*/
ResourceRegistry.noConflict = function () {
root.ResourceRegistry = prevName;
return this;
};
// Expose our precious function to outer world.
if (typeof define === 'function' && define.amd) { // requirejs/amd env
define(
[],
function () {
return ResourceRegistry;
}
);
} else { // plain browser environment
root.ResourceRegistry = ResourceRegistry;
}
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment