Backbone.Highway - Routing Backbone with style \o/
Backbone.Highway
wraps the Backbone.Router
to simplify its use and bring new functionalities.
This is a simplified version of the previous Backbone.Highway
library completely re-written in ES2015.
Added functionalities compared to the Backbone.Router
are:
- Named routes
- Special error routes 404
- Event triggers distributed using an event aggregator
Installation
npm i --save backbone-highway2
or
bower i --save backbone-highway2
Getting started
Simply declare some routes using the highway.route()
method
and then start the router with the highway.start()
method.
;; // Declare a home routehighway; // Declare a user profile routehighway; // Wait for document ready event;
Navigating
Use the go
method to navigate to a declared route
// Navigate to simple route using its namehighway; // Navigate to route with parametershighway;// `params` can be called `args` aswellhighway; // Navigate using urlhighway;
Catching client-side 404
You can declare a special route named 404
to catch inexisting routes
highway;
Events
Each route can trigger events using an event aggregator like Backbone.Events
or Backbone.Radio
;;; // Listen to 'core:render' eventEvents; // Declare a profile routehighway; // Wait for document ready;
In this example, the route parameter :name
will be passed to the event,
but it can be overridden by declaring specific params
for the event.
Dependencies
- Backbone >= 1.1.4
- Underscore >= 1.4.4
Demo / Example
Use npm to install dependencies and launch the demo server.
npm install && npm start
License
The MIT License (MIT)
Copyright (c) 2015 d4f
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.