Skip to content

Instantly share code, notes, and snippets.

@snelson
Created December 20, 2015 03:26
Show Gist options
  • Save snelson/5813dedac997e06ebbda to your computer and use it in GitHub Desktop.
Save snelson/5813dedac997e06ebbda to your computer and use it in GitHub Desktop.
class App.Router extends Backbone.Router
initialize: (options) ->
# keep track of the first route event so that
# loadNormally doesn't get stuck in an infinite
# redirect loop
@isFirstRoute = true
@once 'route', =>
@isFirstRoute = false
routes:
# replace main view
'' : 'replaceMainView'
# load any url to the admin normally
'admin*path' : 'loadNormally'
# load all urls in an overlay by default
'*default' : 'presentOverlay'
loadNormally: ->
# Don't redirect if this is the first route event
# (the page has just loaded)
window.location = @getUrl() unless @isFirstRoute
replaceMainView: ->
@loadUrl @getUrl(), (data) ->
$('#main-view').html(data)
$('#overlay').hide()
presentOverlay: ->
@loadUrl @getUrl(), (data) ->
$('#overlay').html(data).show()
getUrl: ->
"/#{Backbone.history.fragment}"
loadUrl: (url, callback) ->
$.get url, { partial: true }, (data) =>
callback(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment