Sort of Dependency Injection

function Core() {
    this.id = "core 0.1";
    var self = this;
    var args = Array.prototype.slice.call(arguments);
    var callback = args.pop();
    var mods = args[0];
    this.modules = [];
    var private_modules = [];
    private_modules.push(self);
    if (!(this instanceof Core)) {
        return new Core(mods, callback);
    }
    if (mods) {
        for (var i in mods) {
            private_modules.push(new Core.modules[mods[i]]());
        }
    }
 
    callback.apply(this, private_modules);
}
 
Core.modules = {};
Core.modules.http = function() {
    this.get = function() {
        return "get";
    };
    this.post = function() {
    };
}
Core.modules.cookie = function() {
    this.get = function() {
    };
    this.set = function() {
    };
}
 
 
Core(['http'], function(core, http) {
    window.console.log(core.id + ":" + http.get());
})
 
/disk_1/www/cz/ladariha/wiki/data/pages/programovani/javascript/di.txt · Poslední úprava: 2013/07/07 22:28 autor: ladariha
 
Kromě míst, kde je explicitně uvedeno jinak, je obsah této wiki licencován pod následující licencí:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki