Skip to content

Instantly share code, notes, and snippets.

@rjz
Created November 21, 2012 01:32
Show Gist options
  • Select an option

  • Save rjz/4122509 to your computer and use it in GitHub Desktop.

Select an option

Save rjz/4122509 to your computer and use it in GitHub Desktop.
Backbone default/index routes
# Match index page ('/') when using pushState in Backbone
class IndexRouter extends Backbone.Router
initialize: (options) ->
# Supply a matcher for both blank and '/' routes:
@route /^\/?$/, 'index', @.index
index: ->
# Serve up an index view
# Use default routes to handle index pages and routing errors in Backbone
# See: http://blog.rjzaworski.com/2012/09/catch-all-routes-in-backbone/
# Fire up all routers
_.each App.Routers, (router) ->
new router
# Provide a catch-all route.
Backbone.history.handlers.push
route: /(.*)/
callback: (fragment) ->
if fragment == ''
# Serve up index view
else
# Serve up error view
alert "Four-oh-four: '#{fragment}' is unavailable"
# Fire up history
Backbone.history.start pushState: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment