Instantly share code, notes, and snippets.

Embed
What would you like to do?
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