The Backbone router

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 ...

Get Building Scalable Apps with Redis and Node.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.