Skip to content

Instantly share code, notes, and snippets.

@basecode
Last active December 31, 2015 20:09
Show Gist options
  • Save basecode/8038663 to your computer and use it in GitHub Desktop.
Save basecode/8038663 to your computer and use it in GitHub Desktop.
Dependency injection using RequireJS. I don't like it!
// 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