Book Image

Backbone.js Essentials

By : Jeremy Walker
Book Image

Backbone.js Essentials

By: Jeremy Walker

Overview of this book

<p>This book offers insight into creating and maintaining dynamic Backbone.js web applications. It delves into the the fundamentals of Backbone.js and helps you achieve mastery of the Backbone library.</p> <p>Starting with Models and Collections, you'll learn how to simplify client-side data management and easily transmit data to and from your server. Next, you'll learn to use Views and Routers to facilitate DOM manipulation and URL control so that your visitors can navigate your entire site without ever leaving the first HTML page. Finally, you'll learn how to combine those building blocks with other tools to achieve high-performance, testable, and maintainable web applications.</p>
Table of Contents (20 chapters)
Backbone.js Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Multiple routers


In general, you only need a single Router to power your entire site. However, if you have reason to do so, you can easily include multiple Routers, and Backbone will happily allow this. If two or more Routers on the same page match a particular route, Backbone will trigger the route from the first Router with a matching route defined.

There are two main reasons why you should use multiple Routers. The first reason is to separate your routing into logical groups. For instance, in an earlier example, we used a conditional to add certain admin-only routes to the Router class when the current user was an administrator. If our site has enough of these routes, it might make sense to create a separate Router for the admin-only routes, as follows:

var NormalRouter = Backbone.Router({
     routes: {
        // routes for all users would go here
    }
};
var AdminRouter = Backbone.Router({
     routes: {
        // routes for admin users only would go here
    }
};
new NormalRouter...