Created
March 4, 2014 01:09
-
-
Save michaelgruber/9338251 to your computer and use it in GitHub Desktop.
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
| class SingletonConfig | |
| # Private scope | |
| # Ensure correct scope by setting instance to null. | |
| instance = null | |
| # This private class gets initialized by the singleton. | |
| class Config | |
| # The actual configuration attributes | |
| defaults: | |
| foo: "bar" | |
| development: | |
| foo: "baz" | |
| # Set up our environment specific config, | |
| # overriding defaults. | |
| constructor: (env) -> | |
| @config = _.extend @defaults, @[env] | |
| # Provide a Backbone-style shortcut to retrieve our | |
| # config values. | |
| get: (key) -> @config[key] | |
| # The static method to retrieve/create instance | |
| @get: () -> | |
| env = switch window.location.hostname | |
| when "localhost", "127.0.0.1" then "development" | |
| else "production" | |
| instance ?= new Config(env) | |
| MyApp.Config = SingletonConfig.get() | |
| # http://localhost:3000/ | |
| MyApp.Config.get('foo') # => baz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment