Coder Social home page Coder Social logo

arianra / grapnel.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from baseprime/grapnel

0.0 1.0 0.0 474 KB

The smallest (1100 bytes gzipped!) JavaScript Router with Named Parameters

Home Page: http://engineeringmode.github.io/Grapnel.js

License: MIT License

grapnel.js's Introduction

Grapnel.js

The smallest (1100 bytes gzipped!) JavaScript Router with Named Parameters & Event Listening. Lots of other Features too!

Features & Basic Usage

Router

var router = new Grapnel();

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
});

Named Parameters

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
});

Declaring Multiple Routes

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);

Event Handling

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
});

RegExp Support

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."
});

Route Context

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.
});

RequireJS/AMD and CommonJS Compatibility

require(['lib/grapnel'], function(Grapnel){

    var router = new Grapnel();

    router.bind('hashchange', function(){
        console.log('It works!');
    });

});

ย 


Documentation

Installation

bower install grapnel

Basic Configuration

var router = new Grapnel.Router();

Or you can declare your routes with a literal object:

Grapnel.listen({
    'products/:id' : function(req){
        // Handler
    }
});

Methods

get Adds a new route listener
/**
 * @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);
});
bind Adds a new event listener
/**
 * @param {String|Array} event
 * @param {Function} callback
*/
router.bind('hashchange', function(event){
    console.log('Grapnel.js works!');
});
on An alias of bind
add An alias of get
anchor
  • defaultHash Static anchor during initialization
  • set Sets a new absolute anchor
  • get Get absolute anchor
  • clear Clears the anchor
  • reset Resets the anchor to its original state when it was loaded

Events

match Fires when a new match is found, but before the handler is called
hashchange Fires when hashtag is changed
initialized Fires when object is initialized (this will likely be useless)

Stopping a Route Event

router.on('match', function(event){
    event.preventDefault(); // Stops propagation of the event
});

License

grapnel.js's People

Contributors

baseprime avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.