Skip to content

Instantly share code, notes, and snippets.

@philmander
Created December 31, 2012 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philmander/4422682 to your computer and use it in GitHub Desktop.
Save philmander/4422682 to your computer and use it in GitHub Desktop.
Using factory methods to generate values or dependencies to inject
//movies/move-finder.js
define(function() {
var MovieFinder = function(username, accessCode) {
this.username = username;
this.accessCode = accessCode;
};
return MovieFinder;
});
//movies/movie-util.js
define(function() {
var MovieUtil = function() {
};
MovieUtil.prototype.createAccessCode = function() {
return 12456;
};
return MovieUtil;
});
//app-config.js
define(function() {
return {
protos: {
movieFinder : {
module: "movies/movie-finder",
args : [
"movieUser", {
factoryRef : "movieUtil",
factoryMethod : "createAccessCode"
}]
},
movieUtil: {
module: "movies/movie-util",
scope: "singleton"
},
}
};
});
require(["inverted", "app-config"], function(inverted, conf) {
var appContext = inverted.create(conf);
//get two singletons (same instance)
appContext.getProto("movieFinder", function(movieFinder) {
//(movieFinder.username == "movieUser") == true
//(movieFinder.accessCode == 123456) == true
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment