spa-hash-router

2.1.5 • Public • Published

#Router

#Router is a simple hash router for a Single Page Application (SPA). The implementation follows the Mobile Web Application Best Practices from W3C.

A route can be defined by using named parts like :name, or by using a RegExp with captured groups like (\\w+). Remember to allways escape a \ character and use the round braces to capture variable parts of a route. Please note, that appropriate methods will be executed only on the first matched route. So plan the order of similar routes by importance in descending order.

#Router also processes queries. It converts them into an object and pass it into each method as the last argument. Even if there is no queries an empty object will be passed as the last argument, so you don't need every time to check if it is defined or undefined.

Requirements

If this library is planned to be used in some legacy environment then please add required plyfills. Here is a good resource: https://github.com/es-shims/es5-shim

Installation

Via NPM (Node Package Manager)

$ npm install spa-hash-router --save

Library files:

  • dest/HashRouter.js - not minified library
  • dest/HashRouter.min.js - minified library

The library can be used:

  • as an AMD module
  • or it can be get from the global variable under the name HashRouter

Simple usage example

new HashRouter({
    prefix: '!',
    fallbackRoute: 'home',
    routes: {
        home: function() {
            console.log('I am home');
        },
        about: [
            function(){
                console.log('do something before');
            },
            function(){
                console.log('show about');
            },
            function(){
                console.log('do something after');
            }
        ]
    }
});

Useful links

Readme

Keywords

Package Sidebar

Install

npm i spa-hash-router

Weekly Downloads

1

Version

2.1.5

License

MIT

Unpacked Size

66.5 kB

Total Files

9

Last publish

Collaborators

  • valerii-zinchenko