Skip to content

jcubic/route.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

route.js

npm bower

Very small and simple routing library that can be use on the server or in the browser. It use dependency injection to inject arguments to your function.

It contain only a router so it can be use anywhere. It's only the API so you need to write your own code that will execute when ever you want (like on hashchange or using HTML5 History API).

Install

You can grab the file from repo or from npm:

npm install jroute.js

or from bower

bower install route.js

and inlcude the route.js and you're good to go.

Usage

var router = new route();

// from version 0.4.0 you can use arrow functions

router.match('/foo/bar/{{id}}/{{name}}', function(name, id) {
    // name and id will be in different order
    // names in url need to match names in function
    console.log(name + ' ' + id);
});

router.exec(location.hash.replace(/^#/, ''));

if you want to execute on change of the hash so hyperlinks work you can use this code:

window.addEventListener('hashchange', function() {
    router.exec(location.hash.replace(/^#/, ''));
});

the init exec will also be needed to get init route when you refresh the page

API

  • router::route_parser(open tag, close tag): return parser function that need to be called with route the function return object {re: sting: names: array}, if you want regex object you need to use new RegExp('^' + re + '$') to have exact match, name filed contain list of names extracted from route (between open and closing tag).

  • router::extract_names(fn): return parameters names from the function (from version 0.4.0 it work with arrow functions).

  • router::test(route, path): first argument is route that cane be parsed and second is actual path, it return true if path match the route.

  • router::match(path, fn[, data]): create route, first is parsable route path as string, second is a function that will be called when exec is executed and third is optional data that will be stored in route, you will be able to access it in router::exec if you use a function as second argument.

  • router::exec(url[, init]): method execute route added by router::match if found any. The init paramater is a function (optioanl) that allow to execute match function with different context. The function is called with two arguments: (data from match function and callback function that need to be called with different context).

  • router::map(pattern, url): return object with mapping names taken from url, eg. if you call it with:

router.map('/user/{{name}}', '/user/foo');

it will return {name: "foo"} if pattern don't have variables it will return empty object and if it don't match it will return undefined.

  • router::pick(array, url): select one or more routes from array that match url, it return array of objects:
[{
  "pattern": "/user/{{name}}",
  "data": {
    "name": "foo"
  }
}]

for url /user/foo, or empty array if not match found.

License

Licensed under MIT license

Copyright (c) 2014-2017 Jakub T. Jankiewicz

About

Simple isomorphic routing library

Resources

License

Stars

Watchers

Forks

Packages

No packages published