The smallest (1100 bytes gzipped!) JavaScript Router with Named Parameters & Event Listening. Lots of other Features too!
var router = new Grapnel({ pushState: false });
router.get('products/:category/:id?', function(req){
var id = req.params.id,
category = req.params.category;
// GET http://mysite.com/#products/widgets/134
console.log(category, id);
// => widgets 134
});
Grapnel.js supports regex style routes similar to Sinatra or Express. The properties are mapped to the parameters in the request.
router.get('products/:id?', function(req){
// GET /file.html#products/134
req.params.id
// => 134
});
router.get('products/*', function(req){
// The wildcard/asterisk will match anything after that point in the URL
// Parameters are provided req.params using req.params[n], where n is the nth capture
});
var routes = {
'products' : function(req){
// GET /file.html#products
},
'products/:category/:id?' : function(req){
// GET /file.html#products/widgets/35
req.params.category
// => widgets
}
}
Grapnel.listen(routes);
var router = new Grapnel();
router.on('hashchange', function(event){
// GET /file.html#products
console.log('Anchor changed to %s', this.anchor.get());
// => Anchor changed to products
});
Grapnel.js allows RegEx when defining a route:
var expression = /^food\/tacos\/(.*)$/i;
var router = new Grapnel();
router.get(expression, function(req, event){
// GET http://mysite.com/page#food/tacos/good
console.log('I think tacos are %s.', req.params[0]);
// => "He thinks tacos are good."
});
You can even add context to a route:
var router = new Grapnel();
var foodRoute = router.context('food');
foodRoute(':foodname', function(req, event){
// GET /file.html#food/tacos
req.params.foodname
// => This taco thing is getting out of hand.
});
require(['lib/grapnel'], function(Grapnel){
var router = new Grapnel();
router.bind('hashchange', function(){
console.log('It works!');
});
});
bower install grapnel
var router = new Grapnel.Router();
Or you can declare your routes with a literal object:
Grapnel.listen({
'products/:id' : function(req){
// Handler
}
});
/**
* @param {String|RegExp} path
* @param {Function} callback
*/
router.get('store/:category/:id?', function(req, event){
var category = req.params.category,
id = req.params.id;
console.log('Product #%s in %s', id, category);
});
/**
* @param {String|Array} event
* @param {Function} callback
*/
router.bind('hashchange', function(event){
console.log('Grapnel.js works!');
});
defaultHash
Static anchor during initializationset
Sets a new absolute anchorget
Get absolute anchorclear
Clears the anchorreset
Resets the anchor to its original state when it was loaded
router.on('match', function(event){
event.preventDefault(); // Stops propagation of the event
});