Backbone named routes
Backbone router extension with named routes for easy client side links generation. Keep your url logic in one place and never type urls by hand again. Weighs less than 2KB.
Tired of hand coding those pesky href attributes on anchors and having to go through all templates when urls are changed? This named router extension allows you to generate urls from named routes or route aliases. Your url structure is already written and defined in your standard Backbone router - it is a good place to generate urls from.
Examples and api
Extend named router and define your routes as you usually do:
var Router = BackboneNamedRouter
Create router instance:
var router = baseUrl: 'http://mysite.loc' usesPushState: true root: '';
Get urls where and when you need them.
// outputs: 'http://mysite.loc'router; // outputs: 'http://mysite.loc/help'router; // outputs: 'http://mysite.loc/search/my-query'router; // outputs: 'http://mysite.loc/search/my-query/p2'router; // outputs: 'http://mysite.loc/file/myFile.pdf?campaign=email'router; // outputs: 'http://mysite.loc/docs/backbone/router'router;
Start Backbone history
Backbonehistorystartrouter;
Convenient helpers
Navigating to named routes is easy as:
// http://mysite.loc/helprouter // http://mysite.loc/search/backbonerouter;
Navigating to urls:
// help route handler is dispatchedrouter; // you can pass jquery anchor objects;
Installation
Backbone named routes is packaged as UMD library so you can use it in CommonJS and AMD environment or with browser globals.
npm install backbone-named-routes --save
// with bundlersvar BackboneNamedRouter = ; // with browser globalsvar BackboneNamedRouter = windowBackboneNamedRouter;