Book Image

Mastering KnockoutJS

By : Timothy Moran
Book Image

Mastering KnockoutJS

By: Timothy Moran

Overview of this book

Table of Contents (16 chapters)
Mastering KnockoutJS
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using the router


While technically an optional plugin, I don't imagine any real-world SPAs will do without the use of the router. While SammyJS ties a URL fragment to a function, Durandal's router ties the URL directly to a module ID. The module can return either a singleton or a constructor, and will be used to bind the view using the standard composition system.

Configuring the router

Let's start configuring the router:

  1. Route configuration is pretty straightforward. Here is the shell module with router configuration for the Contact application:

    define(['plugins/router', 'knockout', 'durandal/app'], 
    function (router, ko, app) {
      return {
        title: app.title,
        router: router,
        activate: function() {
    
          router.map([
            { route: '', moduleId: 'contacts/list', title: 'Contacts', nav: true },
            { route: 'contacts/new', moduleId: 'contacts/edit', title: 'New Contact', nav: true },
            { route: 'contacts/:id', moduleId: 'contacts/edit', title: 'Contact Details', nav: false...