Router

The Router is implemented to work very similarly to Backbone.Router. The difference is that Rendr helps deconstruct the functionality of a Backbone.Router by adding Controllers and a routes.js file.

addBackboneRoute Router.addBackboneRoute(routeObj)


Client-side only Adds a route to the Backbone.Router by calling Backbone.Router.route method. This function is normally invoked by the addRouteDefinition method.

addExpressRoute Router.addExpressRoute(routeObject)


Server-side only Adds a route to an Express.js router.

addRouteDefinition Router.addRouteDefinition(route)


The route is formatted as an array: ['url', 'controller#action', { options }]. This function creates a route and a callback for it that invokes the associated controller action.

Once the route has been created, the route:add event is triggered with an array of route data.

buildRoutes Router.buildRoutes()


Requires the routes.js file and creates an array of routes to be added to the client or server. Invokes addRouteDefinition for each of the routes in the routes.js file.

currentRoute Router.currentRoute


Returns the routing object for the route currently being displayed.

An example result for currentRoute when on the home page of a site might be the following

{
  controller: "home",
  action: "index"
}

routes.js FILE app/routes.js


This file stores the mapping between a URL route and a controller action.

Example routes.js file:

module.exports = function(addRoute) {
  addRoute('users/login', 'users#login', { role: admin });
  addRoute('users/:id', 'users#show');
  addRoute('test', {controller: 'test', action: 'index'});
  addRoute('testRedirect', 'testRedirect#index', { redirect: '/foo' });
  addRoute('testRoles', 'testRole#index', { role: 'admin' }});
  addRoute(/^\/regexp\/(foo|bar)/, 'test#regexp');
};

addRoute is a callback function defined in Rendr to add the route to a list of available routes.

The callback function takes 3 parameters:

  1. The URL for the route, which can also be a regular expression
  2. The The controller name and action can be specified either by a shorthand string notation, or by an object:

    • Shorthand: 'controllerName#action'
    • Object: {controller: 'controllerName', action: 'action'}
  3. optional An object of options to pass to the router. If the object contains a redirect, no controller information is required. The options object can also add headers to the content returned from the Express server by adding them to a headers element, e.g.:

var headers = {
  'Vary': 'User-Agent',
  'Cache-Control': 'no-transform,public,max-age=300,s-maxage=900',
  'ETag': '12345678'
};
addRoute('testRoles', 'testRole#index', {role: 'admin', headers: headers}});