Skip to content

gitter-badger/moria

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A routing system for Mithril

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published