Created
December 31, 2012 20:58
-
-
Save philmander/4422682 to your computer and use it in GitHub Desktop.
Using factory methods to generate values or dependencies to inject
This file contains 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
//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; | |
}); |
This file contains 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
//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" | |
}, | |
} | |
}; | |
}); |
This file contains 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
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