Git Product home page Git Product logo

poptart's Introduction

Some JavaScript client-side router.

Install

With npm do:

npm install poptart-router history --save

You also need the history module.

Usage

var createRouter = require('poptart-router')
var history = require('history')()

var router = createRouter(history)

function callback (location, next) {
  // That is history's location object
  // https://github.com/ReactJSTraining/history/blob/master/docs/Location.md

  var name = location.params.name; // url params
  var foo = location.state.foo; // your state object
  
  console.log(name)
  console.log(foo)
  
  next() // allow the callback chain go forward
}

router.add('/hello/:name', callback)

router.start()

router.push({
  pathname: router.generate('/hello/:name', { name: 'world' })
  state: { foo: 'bar' }
})

// => "world"
// => "bar"

API

createRouter

createRouter(history[, base, callback])

  • history A history object.
  • base (String) should be set in case you're not operating at the root path / of the domain.
  • callback is fired after all callbacks of every matched route have been called. It should follow this signature function (err, location) {}.

#add

router.add(path[, callback...])

You can add as many callbacks as you need. This is internally handled by the ware module, so the callback signature should be the following:

function (location, next) {}

Remember to call next when you're done so the next callback in line can be fired.

Routes are matched in the order they were added, and they are matched using the famous path-to-regexp module, used by Express among many others, so regular expressions are supported and all that.

Please check out the path-to-regexp documentation to know more about route options. Also checkout the live demo!

#route

add alias.

#start

router.start([immediately])

Router starts listening for route changes.

For immediately pass false if you want to skip parsing the current location at start.

#push

router.push(location)

A wrapper around history.push, where location is that one from the history module.

This will update the browser's URL and fire any callbacks.

#replace

router.replace(location)

Same as push but using replaceState...

#stop

router.stop()

Stop listening for changes.

License

MIT

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.