mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-23 21:22:48 +01:00
Implemented di container component
This commit is contained in:
40
components/container.js
Normal file
40
components/container.js
Normal file
@@ -0,0 +1,40 @@
|
||||
(function(should){
|
||||
function Container(store) {
|
||||
this.store = {};
|
||||
this.resolved = {};
|
||||
}
|
||||
|
||||
Container.prototype.register = function(name, inst) {
|
||||
var reg = this.store[name];
|
||||
if(reg == null) {
|
||||
reg = this.store[name] = { instance: inst };
|
||||
}
|
||||
return reg;
|
||||
};
|
||||
|
||||
Container.prototype.resolve = function(name) {
|
||||
var reg = this.store[name];
|
||||
if(reg == null) {
|
||||
throw new Error(''); // TODO: wrote
|
||||
}
|
||||
|
||||
if(reg.resolved == null) {
|
||||
var inst = reg.instance;
|
||||
this.resolveProperties(inst);
|
||||
reg.resolved = inst;
|
||||
}
|
||||
|
||||
return reg.resolved;
|
||||
};
|
||||
|
||||
Container.prototype.resolveProperties = function (instance) {
|
||||
for(var property in instance) {
|
||||
if(property[0] == '$') {
|
||||
var name = property.substr(1, property.length - 1);
|
||||
instance[property] = this.resolve(name);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
window.Container = Container;
|
||||
})();
|
||||
Reference in New Issue
Block a user