September 2014
Intermediate to advanced
316 pages
7h 6m
English
Backbone has one more feature we will use. This is routing. A Backbone router will listen for hash changes in the URL and this will trigger events. The router can be configured to match certain patterns and even pull out parameters. Let's create the router, as shown in the following code:
var Router = Backbone.Router.extend({
routes: {
'': 'RoomSelection',
'room/:room' : 'JoinRoom',
'*default' : 'Default'
}
});Routers are built the same way as other Backbone objects; we extend the base router. The Router object really only needs a routes object that has patterns as the properties and the event name as the value. Routers allow the back button and deep linking to work in a single-page JavaScript application, just like the chat ...