-
-
Save joshdmiller/b1dc37a1a2452ce5b68f to your computer and use it in GitHub Desktop.
Pesudocode Concept: Dependency Injection with Stampit and ES6
This file contains 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
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; |
This file contains 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
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 | |
}); |
This file contains 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
import A from './a'; | |
import {Injector} from './di'; | |
let injector = Injector( {/* config */}, /* ...mocks */ ); | |
let a = injector.get( A ); | |
a.doSomething(); |
This file contains 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
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