Router Code Walkthrough

In notebook:
FrontEndMasters BackboneJS
Created at:
2016-06-10
Updated:
2016-06-10
Tags:
libraries JavaScript

Routes should be repeatable

How an app, centred around a route would look like:
  module.exports = Backbone.Router.extend({
    initialize: function(options){
        if(!options.app){
            throw new Error('You must provide an app instance');
        }
        this.app = options.app;
    }
    
    routes: {
        '': 'showLouggedout',
        'logout': 'logoutUser',
        'login': 'showLogin',
        'auth/:service': 'authenticate',
        'dashboard': 'showDashboard'
    },
    
    showLogin: function() {
        this.app.views.header.signIn.render();
    },
    
    authenticate: function (service) {
        if(service === 'indentity') {
            this.app.views.header.createAccount.render();
            return;
        }
        
        this.app.user.authenticate(service, this.app);
    },
    
    logoutUser: function() {
        this.app.user.logout();
    },
    
    showDashboard: function () {
        this.app.views.dashboard.render();
    }
})
Argues for keeping the router very lightweight. This also helps with testing.