Skip to content

Latest commit

 

History

History
68 lines (54 loc) · 2.42 KB

README.md

File metadata and controls

68 lines (54 loc) · 2.42 KB

moria

A routing system for Mithril.

Mithril has a beautiful lightweight routing paradigm, but as legends recount, sometimes you need to dig deep to get at the good bits: it's difficult to express the routing of a complex application when m.route( rootElement, defaultRoute, routesHash ) can only be invoked once and the routes hash has to be flat.

Moria aims to solve these problems by producing a Mithril-compatible route hash from nested hierarchies.

Features

  • Nested routes
  • Setup functions to run when a route is matched
  • Redirects

Roadmap

  • Nested modules (currently, each route endpointmust specify the complete module structure you want to render)
  • ...

Install

Currently only works as a CommonJS module, and therefore requires Node (and npm if you want command-line installation). I use browserify to compile my CommonJS JavaScript for use on the front-end.

npm install --save moria

Use

var moria = require( 'moria' );

var routeHash = moria( {
  ''          : loginModule,            // Results in '/loginModule',
  'search'    : '../shop/search',       // Redirect '/search' to '/shop/search'
  'shop'      : {
    ''         : browseModule,          // Results in '/shop'
    'search'   : searchModule,
    'checkout' : {
      'payment'  : foo,                 // Results in '/shop/checkout/payment'
      'delivery' : bar,
      'confirm'  : baz
    }
  },
  ':userName' : [                       // When '/:userName' is matched...
    function(){                         // This function will be executed...
      initUserModel();
    },
    profileModule                       // And this module will render
  ],
  'admin'     : [
    initAdminModel,                     // Another setup function
    {
      ''      : 'users',                // Redirect '/admin' to '/admin/users'
      'users' : [                       // When route matches '/admin/users'...
        initUsersModel,                 // Run initAdminModel && initUserModel...
        {
          ''          : usersListModule,// When we render usersListModule...
          ':userName' : editUserModule  // And when we render editUserModule
        }
      ]
    }
  ]
} );

m.route( document.body, '/', routeHash );