Created
February 14, 2012 21:35
-
-
Save matthijsgroen/1830654 to your computer and use it in GitHub Desktop.
Backbone.contextmanager
This file contains 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
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() | |
This file contains 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 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 | |
This file contains 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
.some-top-bar | |
.page | |
.left-side-panel | |
.main-content | |
.home | |
.contact | |
.right-side-panel |
This file contains 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 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