Skip to content

Instantly share code, notes, and snippets.

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
foo: "bar"
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", "" 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