Skip to content

Instantly share code, notes, and snippets.

@domenic
Created October 10, 2011 04:05
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save domenic/1274607 to your computer and use it in GitHub Desktop.
Save domenic/1274607 to your computer and use it in GitHub Desktop.
Dependency injection sample with RequireJS
// EntryPoint.js
define(function () {
return function EntryPoint(model1, model2) {
// stuff
};
});
// Model1.js
define(function () {
return function Model1() {
// stuff
};
});
// Model2.js
define(function () {
return function Model2(helper) {
// stuff
};
});
// Helper.js
define(function () {
return function Helper() {
// stuff
};
});
// composition root, probably your main module
define(function (require) {
var EntryPoint = require("./EntryPoint");
var Model1 = require("./Model1");
var Model2 = require("./Model2");
var Helper = require("./Helper");
var entryPoint = new EntryPoint(new Model1(), new Model2(new Helper()));
entryPoint.start();
});
@domenic
Copy link
Author

domenic commented Oct 10, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment