Last active
December 10, 2015 10:39
-
-
Save philmander/4422560 to your computer and use it in GitHub Desktop.
Demonstrates using singleton and static scopes to create movie utils
This file contains hidden or 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
| //movie/movie-util-instance.js | |
| define(function() { | |
| var MovieUtil = function() { | |
| }; | |
| MovieUtil.prototype.doSomething = function() { | |
| //dostuff | |
| }; | |
| return MovieUtil; | |
| }); | |
| //movie/movie-util-static.js | |
| define(function() { | |
| var movieUtil = {}; | |
| movieUtil.doStuff = function() { | |
| //do stuff | |
| }; | |
| return movieUtil; | |
| }); |
This file contains hidden or 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: { | |
| movieUtilInstance: { | |
| module: "movies/movie-util-instance", | |
| scope: "singleton" | |
| }, | |
| movieUtilStatic: { | |
| module: "movies/movie-util-static", | |
| scope: "static" | |
| } | |
| } | |
| }; | |
| }); |
This file contains hidden or 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(["movieUtilInstance" "movieUtilInstance"], | |
| function(movieUtilOne, movieUtilTwo) { | |
| //(movieUtilOne === movieUtilTwo) == true | |
| movieUtilOne.doStuff(); | |
| }); | |
| //get as static (no instance created) | |
| appContext.getProto("movieUtilStatic", function(movieUtil) { | |
| movieUtil.doStuff(); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment