Book Image

Building Scalable Apps with Redis and Node.js

By : Joshua Johanan
Book Image

Building Scalable Apps with Redis and Node.js

By: Joshua Johanan

Overview of this book

Table of Contents (17 chapters)
Building Scalable Apps with Redis and Node.js
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 page we are building. For example, we could send out a link that was /chat#room/test and the application would start at the JoinRoom function instead of RoomSelection.

In this router, we only need two routes along with a catch-all. The...