Single-page web applications usually need to provide linkable and user-friendly links. Until recently, the only way around this was using hash fragments (/#/posts
), but with the HTML5 history API, the use of standard URLs (/posts
) is available. The Backbone.Router
class provides useful methods for client-side routing in a single-page web application.
To create a new router, use the backbone:router
subgenerator as follows:
$ yo backbone:router posts
This command creates a new Backbone router located at app/scripts/routes/posts.coffee
.
To use the Backbone.Router
class, simply extend the class and provide the routes
property with an object hash set on the name/callback of the route, as follows:
define [ 'backbone' 'config' 'views/app' 'views/main' 'views/about' 'views/posts' 'models/post' ], (Backbone, Config, AppView, MainView, AboutView, PostsView, PostModel) -> class AppRouter extends Backbone.Router currentView: null ...