Skip to content

Instantly share code, notes, and snippets.

@joshdmiller
Last active August 29, 2015 14:27
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 joshdmiller/b1dc37a1a2452ce5b68f to your computer and use it in GitHub Desktop.
Save joshdmiller/b1dc37a1a2452ce5b68f to your computer and use it in GitHub Desktop.
Pesudocode Concept: Dependency Injection with Stampit and ES6
import B from './b';
import C from './c';
const A = stampit()
.static({ $inject: [ B, C ] })
.init( ({ instance, args }) => {
let [ instance._b, instance._c ] = args;
})
.methods({
doSomething () {
return this._b.doIt();
}
});
export default A;
import A from './a';
import B from './b';
import mockB from './mockB';
let injector = Injector( {}, mockB );
injector.get( A )
.then( function ( a ) {
a.doSomething();
// -> 'mock!'
});
injector.get( [ A, B] )
.then( function ( a, b ) {
// have both
});
import A from './a';
import {Injector} from './di';
let injector = Injector( {/* config */}, /* ...mocks */ );
let a = injector.get( A );
a.doSomething();
import B from './b';
const mockB = stampit()
.static({ $provides: B })
.methods({
doIt () {
return 'mock!';
}
})
;
export default mockB;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment