Implemented di container component

This commit is contained in:
Borys Levytskyi
2015-04-03 03:32:14 +03:00
parent 72f9d0bb4e
commit 569c335368
4 changed files with 137 additions and 119 deletions

40
components/container.js Normal file
View 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;
})();