Skip to content

Instantly share code, notes, and snippets.

@philmander
Last active December 10, 2015 10:38
Show Gist options
  • Select an option

  • Save philmander/4422282 to your computer and use it in GitHub Desktop.

Select an option

Save philmander/4422282 to your computer and use it in GitHub Desktop.
Injecting a dependency as a constructor argument
//movies/movie-lister.js
define(function() {
var MovieLister = function(movieFinder) {
this.movieFinder = movieFinder;
};
return MovieLister;
});
//movies/movie-finder.js
define(function() {
var MovieFinder = function() {
//constructor stuff
};
return MovieFinder;
});
//app-config.js
define(function() {
return {
protos: {
movieLister: {
module: "movies/movie-lister",
args: [
"*movieFinder"
]
},
movieFinder: {
module: "movies/movie-finder"
}
}
};
});
require(["inverted", "app-config"], function(inverted, conf) {
var appContext = inverted.create(conf);
appContext.getProto("movieLister", function(movieLister) {
//movieLister instanceof MovieLister == true
//movieLister.movieFinder instanceof MovieFinder == true
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment