Skip to content

Instantly share code, notes, and snippets.

@Dirrk
Created March 8, 2017 01:23
Show Gist options
  • Save Dirrk/19cfa0ff53ece4d139b4248036a022c0 to your computer and use it in GitHub Desktop.
Save Dirrk/19cfa0ff53ece4d139b4248036a022c0 to your computer and use it in GitHub Desktop.
const Inversify = require('inversify');
const helpers = require("inversify-vanillajs-helpers").helpers;
require('reflect-metadata');
class A {
constructor() {
this.name = 'I am A!';
}
}
class B {
static inversify() { return ['A']; }
constructor(a) {
this.a = a;
this.name = 'I am B!';
}
}
class C {
static inversify() { return ['A', 'B']; }
constructor(a, b) {
this.a = a;
this.b = b;
this.name = 'I am C!';
}
}
const container = new Inversify.Container();
const register = helpers.register(container);
const decorateForMe = function(c, type) {
let decorators = [];
if (c && c.inversify && typeof c.inversify === 'function') {
decorators = c.inversify();
}
register(type, decorators)(c);
};
decorateForMe(A, 'A');
decorateForMe(B, 'B');
decorateForMe(C, 'C');
let a = container.get('A');
let b = container.get('B');
let c = container.get('C');
console.log(a.name);
// I am A!
console.log(b.name);
// I am B!
console.log(c.name);
// I am C!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment