Routers and History
In the Views and Events chapter, we built a rudimentary quiz application that displayed a sequence of exercises, with a Next button for navigation. When we left off, the quiz was usable, but lacked a number of features that would be appreciated by the user.
For example, imagine that you wanted to bookmark an exercise in order to return directly to it later.In a native application, this would require a custom control. With Backbone, however, we can leverage capabilities already present in the browser. As I'm sure you know, browsers navigate to stateful resources using URLs, and offer sophisticated navigation controls, including bookmarking.
Wouldn't it be great if we could use the browser to bookmark and then return to specific Exercises within our quiz application? Well with just a little work, we can. The creators of Backbone already thought of this, and included the Router and History components to help us do it.
Thus, in this chapter we shall continue our work on our small quiz application, with a special focus on coordinating application state with browser's URL.
Once again, our first example will be simple, so that you understand the fundamentals. I also hope that these initial examples, which always end with should assertions, provide an inkling of how to unit test your own Backbone code. I will write about unit testing with CoffeeScript in an upcoming book, but please don't wait for it. If you have never tried unit testing with CoffeeScript and would like to, visit the home pages for Mocha and Chai, download or clone those projects from GitHub, and create a basic test suite using some of the simple examples that begin each chapter in this book.