Giter VIP home page Giter VIP logo

router's Introduction

router

A nice client-side router.

Installation

$ component install ianstormtaylor/router

Example

Simple example:

var db = require('db');
var Router = require('router');

var router = new Router()
  .on('/:user', user, renderUser)
  .on('/:user/:repo', user, repo, renderRepo)
  .start();

function user (context, next) {
  var id = context.params.user;
  var user = db.users.get(id);
  context.user = user;
  next();
}

function repo (context, next) {
  var id = context.params.repo;
  var repo = db.users.get(id);
  context.repo = repo;
  next();
}

function renderUser (context, next) {
  var user = context.user;
  render(user);
}

function renderRepo (context, next) {
  var user = context.user;
  var repo = context.repo;
  render(user, repo);
}

Check the examples folder for the some more complicated examples.

API

#on(path, middleware...)

Bind middleware functions to a path. Middleware take next callbacks to move to the next middleware on the queue.

#in(middleware...)

Add "in" transition middleware that will be executed when a route is matched. This is equivalent to passing middleware to #on. You have to call #on first before calling #in.

#out(middleware...)

Add "out" transition middleware that will be executed when the route is being "torn down" and another route is matched. You have to call #on first before calling #out.

#start()

Start the router, dispatching the current URL. (Convenience so you don't have to call #dispatch(window.location... yourself.)

#listen(path)

Start and listen for link clicks that the router should handle, optionally namespaced by a path. This means that you don't need to .preventDefault clicks by hand on a[href]'s since the router will do it for you for internal links.

#go(path, [state])

Dispatch to a path and push it onto the history with an optional state object.

#dispatch(path)

Trigger middleware for a path.

#push(path, [state])

Push a path onto the history, with an optional state object.

#replace(path, [state])

Replace the current URL in the history with a new path, with an optional state object.

#unbind()

Unbind the router

.go(path, [state])

Trigger path on all routes that listen for it.

.use(plugin)

Use the given plugin.

License

MIT

router's People

Contributors

anthonyshort avatar ianstormtaylor avatar jkroso avatar lvivier avatar matthewmueller avatar queckezz avatar stagas avatar timaschew avatar trevorgerhardt avatar

Watchers

 avatar  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.