R.js is a 30-line JS library that brings life to the third part of minimalistic saga: parameterized client-side routing.
Download the library here or include the following into your HTML:
<script src="//cdn.rawgit.com/plugnburn/67b2736ef7387de151b7/raw/18135db0bd02eb610eb66962b8c8451f7732b5d3/r.min.js"></script>
Just two methods:
R(path, cb)
- setup a route (you can use:param
style - parameters object will be passed into the callback);R.go(path)
- resolve and go to a real existing path (relative to the root, for example,/articles/123/
).
Setting up a route with id
parameter: R('/view/:id/', function(params){console.log('Article ID:', params.id)})
Going to a previously defined route as if the user entered the URL: R.go('/view/5/')
That's it.