Skip to content

Instantly share code, notes, and snippets.

@robwormald
Forked from anonymous/decorators.js
Created April 21, 2015 20:32
Show Gist options
  • Save robwormald/432d7a04ad4302255066 to your computer and use it in GitHub Desktop.
Save robwormald/432d7a04ad4302255066 to your computer and use it in GitHub Desktop.
import * as di from './annotations';
const Inject = (...dependencies) => {
return (classDef) => {
di.annotate.apply(di, [classDef].concat(dependencies.map((dep) => new di.Inject(dep))));
}
}
const Provide = (targetClassDef) => {
return (classDef) => {
di.annotate(classDef, new di.Provide(targetClassDef));
}
}
export {Provide, Inject}
//angular2 (and di.js) use Annotations - *not* decorators.
//source of the DI.js *annotation* looks like:
class Inject {
constructor(...tokens) {
this.tokens = tokens;
this.isPromise = false;
this.isLazy = false;
}
}
//used like
@Inject(SomeDep)
class SomeServiceThing {
constructor(someDep){
}
}
//which in *traceur* compiles down to
function SomeServiceThing(someDep){}
SomeServiceThing.annotations = [new Inject(someDep)];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment