Created
November 21, 2012 01:32
-
-
Save rjz/4122509 to your computer and use it in GitHub Desktop.
Backbone default/index routes
This file contains hidden or 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
| # 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 |
This file contains hidden or 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
| # 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