Book Image

Learning Yeoman

By : Jonathan Spratley
Book Image

Learning Yeoman

By: Jonathan Spratley

Overview of this book

Table of Contents (17 chapters)
Learning Yeoman
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Modern Workflows for Modern Webapps
Index

Backbone.Router


Single-page web applications usually need to provide linkable and user-friendly links. Until recently, the only way around this was using hash fragments (/#/posts), but with the HTML5 history API, the use of standard URLs (/posts) is available. The Backbone.Router class provides useful methods for client-side routing in a single-page web application.

Creating routers

To create a new router, use the backbone:router subgenerator as follows:

$ yo backbone:router posts

This command creates a new Backbone router located at app/scripts/routes/posts.coffee.

Using routers

To use the Backbone.Router class, simply extend the class and provide the routes property with an object hash set on the name/callback of the route, as follows:

define [
  'backbone'
  'config'
  'views/app'
  'views/main'
  'views/about'
  'views/posts'
  'models/post'
  ], (Backbone, Config, AppView, MainView, AboutView, PostsView, PostModel) ->

  class AppRouter extends Backbone.Router
    currentView: null
 ...