pathrouter

0.1.3 • Public • Published

Pathrouter JS

Simple routing library using HTML5 history.pushState or hashbang for web browsers.

Installation

$ npm install pathrouter

Standalone

Also, you can use the standalone version without components.

<script src="../standalone/path.js"></script>

How-to

First, initialize the router:

var path = require('pathrouter');

Now, define some listener for any paths:

function user(id) {
    console.log(id);
}
 
function userAction(id, action) {
    console.log(id);
    console.log(action);
}

Then, add some path to the router:

path('/user/:id', user);
path('/user/:id/:action', userAction);

You can also add to an object of path-listener:

path({
    '/user/:id': user,
    '/user/:id/:action': userAction
});
path('*', userAction);
path(userAction);

Somewhere in your HTML code, you should have anchor tags with #hash or #!/hash hyperlinks related to router js structure.

<a href="/user/pazguille" data-path>User</a>

API

path(path, listener)

Creates a new path and stores its listener into the collection.

  • path - The path you want to create.
  • listener - Listener you want to add to given path.
path('/user/:id', user);

path#remove(path, listener)

Removes a path or its litener from the collection with the given path. The listener is optional.

  • path - The path you want to remove.
  • listener [optional] - Listener you want to remove from given path.
path.remove('/user/:id', user);

path#getListeners(path)

Returns a collections of listeners with the given path. The path is optional. If not have a path as parameter it returns an entire collection of path-listeners.

  • path [optional]
path.getListeners('/user/:id'); // returns [user]

Contact

License

Copyright (c) 2013 @pazguille Licensed under the MIT license.

Readme

Keywords

none

Package Sidebar

Install

npm i pathrouter

Weekly Downloads

1

Version

0.1.3

License

ISC

Last publish

Collaborators

  • oleksandrknyga