Created
July 9, 2014 22:38
-
-
Save Xiphe/5d27bb8ef14053e27895 to your computer and use it in GitHub Desktop.
Thoughs about angular/[email protected] and DRY
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
var di = require('di'); | |
/* Here comes the interesting part: | |
We pass some kind of module loader into di (require for node or RequireJS apps) | |
We'd have to add some configuration for async/promise support | |
The string-tokens we use in our app will than be loaded/required and di will then use the Object/Class/whatever | |
as internal Token. | |
*/ | |
di.moduleLoader(require); | |
var injector = new di.Injector([]); | |
/* The injector requires MyClass.js, sees that it needs MyOtherClass.js and fs.readFile from node, requires ans resolves them | |
and injects them into the MyClass constructor */ | |
myClass = injector.get('./MyClass').doThings(); |
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
/** | |
* Just a simple example Class | |
* Main point here is: This is completely independent from di implementations | |
* We could as well require this file and instantiate the Class on our own | |
*/ | |
function MyClass(myOtherClass, readFile) { | |
this.myOtherClass = myOtherClass; | |
this.readFile = readFile; | |
this.doThings = function() {}; | |
} | |
/* yes, we have again strings as tokens, but in most cases (exept, we define all modules in the same scope) | |
we would have had to write these strings somewhere else */ | |
MyClass.inject = ['./MyOtherClass', 'fs.readFile'] | |
module.exports = MyClass |
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
module.exports = function MyOtherClass() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment