Last active
August 29, 2015 14:04
-
-
Save SSilence/3c84b5ef5fd3162ab1fe to your computer and use it in GitHub Desktop.
dependency injection
This file contains hidden or 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
/** | |
* simple dependency injection | |
* | |
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de) | |
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) | |
*/ | |
var injectCallable = {}; | |
var injectObjects = {}; | |
var define = function(name, obj) { | |
injectCallable[name] = obj; | |
}; | |
var injectPlaceholder = function(name) { | |
this.name = name; | |
this.injectPlaceholder = true; | |
}; | |
var injected = function(name) { | |
return new injectPlaceholder(name); | |
}; | |
var inject = function(name) { | |
if (typeof injectCallable[name] == 'undefined') | |
throw name + " was not found"; | |
if (typeof injectObjects[name] == 'undefined') { | |
injectObjects[name] = new injectCallable[name](); | |
for (property in injectObjects[name]) { | |
if (typeof injectObjects[name][property].injectPlaceholder != 'undefined') | |
injectObjects[name][property] = inject(injectObjects[name][property].name); | |
} | |
} | |
return injectObjects[name]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple dependency injection. Example: