Skip to content

Instantly share code, notes, and snippets.

@Xiphe
Created July 9, 2014 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xiphe/5d27bb8ef14053e27895 to your computer and use it in GitHub Desktop.
Save Xiphe/5d27bb8ef14053e27895 to your computer and use it in GitHub Desktop.
Thoughs about angular/di.js@2.0 and DRY
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();
/**
* 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
module.exports = function MyOtherClass() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment