Skip to content

Instantly share code, notes, and snippets.

@matthijsgroen
Created February 14, 2012 21:35
Show Gist options
  • Save matthijsgroen/1830654 to your computer and use it in GitHub Desktop.
Save matthijsgroen/1830654 to your computer and use it in GitHub Desktop.
Backbone.contextmanager
window.App =
Models: {}
Collections: {}
Routers: {}
Views: {}
DebugInstances: {}
context_manager: null
init: ->
@context_manager = new App.Views.ContextManager
default: "home"
contexts:
"contact":
".left-side-panel": "active"
".main-content": "program"
".right-side-panel": "active"
"home":
".main-content": "home"
new @Routers.HomeRouter()
new @Routers.ContactRouter()
Backbone.history.start()
$ ->
App.init()
class App.Views.ContextManager extends Backbone.View
initialize: ({ @default, @contexts, @options }) ->
@current_context = null
@prepare() unless @options?.skip_prepare
prepare: ->
if @options?.element
@setElement @options?.element
else
@setElement $("body")[0]
@switch_to_default_context()
switch_to_default_context: -> @switch_context @default
switch_context: (context) ->
reverts = if @current_context then _.clone(@contexts[@current_context]) else {}
for selector, class_name of @contexts[context]
delete reverts[selector] if reverts[selector] is class_name
@$(selector).addClass(class_name)
@$(selector).removeClass(class_name) for selector, class_name of reverts
@trigger "change:context", @current_context, context
@current_context = context
.some-top-bar
.page
.left-side-panel
.main-content
.home
.contact
.right-side-panel
class App.Routers.HomeRouter extends Backbone.Router
routes:
"home": "welcome"
set_context: ->
App.context_manager.switch_context "home"
welcome: ->
@set_context()
# other stuff related to this route
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment