Last active
December 31, 2015 20:09
-
-
Save basecode/8038663 to your computer and use it in GitHub Desktop.
Dependency injection using RequireJS. I don't like it!
This file contains hidden or 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
// EntryPoint.js | |
define(function () { | |
return function EntryPoint(require) { | |
this.modul1 = new require("./Model1"); | |
this.model2 = new require("./Model2"); | |
}; | |
}); | |
// Model1.js | |
define(function () { | |
return function Model1() { | |
// stuff | |
}; | |
}); | |
// Model2.js | |
define(function () { | |
return function Model2(require) { | |
this.helper = new require("./Helper"); | |
}; | |
}); | |
// Helper.js | |
define(function () { | |
return function Helper() { | |
// stuff | |
}; | |
}); | |
// composition root, probably your main module | |
define(function (require) { | |
var entryPoint = new EntryPoint(require); | |
entryPoint.start(); | |
}); | |
// test would look like this | |
define(function () { | |
// pseudo spy API | |
spyOn(require, "./Model1").andReturn(new MyMock()); | |
spyOn(require, "./Model2").andReturn(new MyMock()); | |
var entryPoint = new EntryPoint(require); | |
entryPoint.start(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment