Giter Club home page Giter Club logo

router's Introduction

The Router

Promise-powered routing independent of view framework.

Not ready for production! This library is only a concept at this point and is not ready for serious use.

Usage

Illustrative example:

const router = new Router();

router.setRoutes({
    '/': HomeView,
    '/person/:id': {
        '/': PersonView,
        '/edit': EditPersonView
    },
    render: renderApp
});

router.listen();

function renderApp(view) {
    // view is now HomeView, PersonView or EditPersonView
    viewEngine.render(view);
}

Attach hooks to run before and after routes. Promises are supported.

const ensurePersonIsLoaded = params => getPerson(params.id)
    .then(person => state.dispatch('person_loaded', person));

router.setRoutes({
    '/': HomeView,
    '/person/:id': {
        before: ensurePersonIsLoaded,
        '/': PersonView,
        '/edit': EditPersonView
    }
});

Nested views:

router.setRoutes({
    '/': HomeView,
    '/person/:id': {
        '/': PersonView,
        render: renderPerson
    },
    render: renderApp
});

function PersonView(params) {
    return 'Text about person';
}

function renderPerson(view) {
    return m('.person-container', view);
}

function renderApp(view) {
    return m('.app-container', view);
}

// Visiting /person/123 would yield something like this:
// m('.app-container', m('.person-container', 'Text about person'))

See examples/mithril.html for a working, more elaborate example.

Credits

Powered by route-parser and history.

License

MIT

router's People

Contributors

codeclown avatar

Watchers

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