cherrytreex

3.5.0 • Public • Published

Cherrytree is a flexible hierarchical router that translates every URL change into a transition descriptor object and calls your middleware functions that put the application into a desired state.

This is a fork of master / v2 branch with the following changes

  • API changes:
    • Published package with cherrytreex name
    • Export router constructor instead of factory function
    • Add possibility to register middleware with a object containing next/done/error hooks
    • Drop ability to customize Promise implementation. To run in browsers without native Promise is necessary a polyfill
    • Do not use active state to generate links
  • Infrastructure changes
    • Update build system simplifying it and producing smaller bundle
    • Incorporated location-bar dependency removing shared code
    • Upgraded dev dependencies

Installation

The size excluding path-to-regexp dependency is ~43kB (without minification and compression)

$ npm install --save cherrytreex

In a CJS/AMD environment

require('cherrytreex')

Docs

Demo

See it in action in this demo.

Plugins

To use cherrytree with React, check out cherrytree-for-react.

Usage

var Cherrytree = require('cherrytreex')
 
// create the router
var router = new Cherrytree()
var handlers = require('./handlers')
 
// provide your route map
router.map(function (route) {
  route('application', {path: '/', abstract: true}, function () {
    route('feed', {path: ''})
    route('messages')
    route('status', {path: ':user/status/:id'})
    route('profile', {path: ':user'}, function () {
      route('profile.lists')
      route('profile.edit')
    })
  })
})
 
router.use(function render (transition) {
  transition.routes.forEach(function (route, i) {
    route.view = handlers[route.name]({
      params: transition.params,
      query: transition.query
    })
    var parent = transition.routes[i-1]
    var containerEl = parent ? parent.view.el.querySelector('.outlet') : document.body
    containerEl.appendChild(view.render().el)
  })
})
 
// for error logging attach a catch handler to transition promise...
router.use(function errorHandler (transition) {
  transition.catch(function (err) {
    if (err.type !== 'TransitionCancelled' && err.type !== 'TransitionRedirected') {
      console.error(err.stack)
    }
  })
})
 
// ...or use the error hook
router.use({
  error: function (transition, err) {
    if (err.type !== 'TransitionCancelled' && err.type !== 'TransitionRedirected') {
      console.error(err.stack)
    }    
  }
})
 
// start listening to URL changes
router.listen()

Examples

You can clone this repo if you want to run the examples locally:

A more complex example in it's own repo:

Features

  • can be used with any view and data framework
  • nested routes are great for nested UIs
  • generate links in a systematic way, e.g. router.generate('commit', {sha: '1e2760'})
  • use pushState with automatic hashchange fallback
  • all urls are generated with or without # as appropriate
  • link clicks on the page are intercepted automatically when using pushState
  • dynamically load parts of your app during transitions
  • dynamic segments, optional params and query params
  • support for custom query string parser
  • transition is a first class citizen - abort, pause, resume, retry. E.g. pause the transition to display "There are unsaved changes" message if the user clicked some link on the page or used browser's back/forward buttons
  • navigate around the app programatically, e.g. router.transitionTo('commits')
  • easily rename URL segments in a single place (e.g. /account -> /profile)

How does it compare to other routers?

  • Backbone router is nice and simple and can often be enough. In fact cherrytree uses some bits from Backbone router under the hood. Cherrytree adds nested routing, support for asynchronous transitions, more flexible dynamic params, url generation, automatic click handling for pushState.
  • Ember router / router.js is the inspiration for cherrytree. It's where cherrytree inherits the idea of declaring hierarchical nested route maps. The scope of cherrytree is slightly different than that of router.js, for example cherrytree doesn't have the concept of handler objects or model hooks. On the other hand, unlike router.js - cherrytree handles browser url changes and intercepts link clicks with pushState out of the box. The handler concept and model hooks can be implemented based on the specific application needs using the middleware mechanism. Overall, cherrytree is less prescriptive, more flexible and easier to use out of the box.
  • react-router is also inspired by router.js. React-router is trying to solve a lot of routing related aspects out of the box in the most React idiomatic way whereas with cherrytree you'll have to write the glue code for integrating into React yourself (see cherrytree-for-react plugin). However, what you get instead is a smaller, simpler and hopefully more flexible library which should be more adaptable to your specific needs. This also means that you can use a react-router like approach with other React inspired libraries such as mercury, riot, om, cycle, deku and so on.

CI

Build Status build status

Browser Support

Sauce Test Status

Cherrytree works in all modern browsers. It requires es5 environment and es6 promises. Use polyfills for those if you have to support older browsers, e.g.:

Acknowledgement

Thanks to Marko Stupić for giving Cherrytree a logo from his http://icon-a-day.com/ project!

FAQ

  • Why is cherrytree written as one word? You got me, I'd say that represents the wabisabi nature of the library.

Want to work on this for your day job?

This project was created by the Engineering team at Qubit. As we use open source libraries, we make our projects public where possible.

We’re currently looking to grow our team, so if you’re a JavaScript engineer and keen on ES2016 React+Redux applications and Node micro services, why not get in touch? Work with like minded engineers in an environment that has fantastic perks, including an annual ski trip, yoga, a competitive foosball league, and copious amounts of yogurt.

Find more details on our Engineering site. Don’t have an up to date CV? Just link us your Github profile! Better yet, send us a pull request that improves this project.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.5.019latest

Version History

VersionDownloads (Last 7 Days)Published
3.5.019
3.4.00
3.3.00
3.2.00
3.1.00
3.0.41
3.0.30
3.0.20
3.0.10
3.0.00

Package Sidebar

Install

npm i cherrytreex

Weekly Downloads

20

Version

3.5.0

License

MIT

Unpacked Size

136 kB

Total Files

6

Last publish

Collaborators

  • blikblum