Created
April 27, 2012 19:25
-
-
Save craigspaeth/2512046 to your computer and use it in GitHub Desktop.
Mixin Art.sy uses to manage "frames" in our CMS app
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
# | |
# Mixin to a router to wrap `route` so that it clears ajax requests and transitions frames. | |
# | |
Backbone.FrameManager = | |
# | |
# Declare regex: frame pairs. If the route matches the regex it will create an instance | |
# of the frame once, and activate/deactivate that view from there on. | |
# | |
# A "frame" is a high-level view associated with an element that is a child of the #frames div. | |
# A frame is often instantiated once and hangs around the duration of the session, which may | |
# require some setup/teardown of events. In this case it may be helpful to listen for a change in | |
# state (the "activate/deactivate" event triggered on the frame's element) or check the current | |
# state (the frame element's "active" class). | |
# | |
frames: {} | |
route: (route, name, callback) -> | |
_.bind(Backbone.Router::route, @) route, name, => | |
@before route | |
callback? arguments... | |
before: (route) -> | |
Artsy.clearXHRs() | |
@deactivateCurrentFrame() | |
@setCurrentFrame route | |
@activateCurrentFrame() | |
@trigger 'highlightNav', route | |
activateCurrentFrame: -> | |
$(@currentFrame.el).trigger('activate').addClass('active') if @currentFrame? | |
deactivateCurrentFrame: -> | |
$(@currentFrame.el).trigger('deactivate').removeClass('active') if @currentFrame? | |
setCurrentFrame: (route) -> | |
@_cachedFrames ?= {} | |
@currentFrame = null | |
for regex, klass of @frames | |
if route.match new RegExp regex | |
@currentFrame = @_cachedFrames[regex] ?= new klass() | |
break | |
# This can then be used in a router like so. | |
# | |
# class App.Routers.Index extends Backbone.Router | |
# frames: | |
# 'artworks' : App.Views.Artworks | |
# | |
# artworks: => | |
# @currentFrame.collection.reset [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment