Git Product home page Git Product logo

leafeon's Introduction

build status code coverage release npm release minified size

As light as a leaf, leafeon is a Javascript routing library that fits perfectly with client-side templating.

Unmaintained notice

This is project is unmaintained and dependencies might be deprecated. Feel free to fork and update it.

Table of Content

Features

  • Dynamic routing & URL generator
  • Error handling
  • Before and after router middleware
  • Route mapping
  • Browser & npm usage

Overview

Edit leafeon

A simple route

leafeon.add('default', '/', function () {
    /* do something */
});

A simple route using parameter

leafeon.add('single_category', '/category/:id', function (id) {
  console.log('You requested the category #' + id);
});

Register a callback when route is not found. It returns router object.

leafeon.setErrorCallback(function () {
    throw new TypeError('I think there\'s a problem.');
});

Mapping routes using a route prefix

// This will create two routes under /docs prefix
leafeon.map('docs_', '/docs', [
    {
        name: 'intro', // will be registered as docs_intro
        path: '/',
        callback: () => { document.write('Hey! Welcome.') }
    },
    {
        name: 'get_started',
        path: '/get-started', // will be registered as /#/docs/get-started
        callback: getStartedAction()
    }
]);

Usage

npm

Install the package :

npm i leafeon --save
const leafeon = require('leafeon').Router();
// or using ES6
// import * as leafeon from 'leafeon';
// const router = leafeon.Router();

leafeon.add('home', '/', () => {
    document.write('hello world');
})

leafeon.run();

Browser

  1. Include leafeon.js in or at the end of the
<script src="leafeon.min.js"></script>

<!-- or via jsdelivr CDN -->
<script src="https://cdn.jsdelivr.net/gh/sundowndev/leafeon@latest/dist/leafeon.min.js"></script>
  1. Init the router
const leafeon = new leafeon.Router();
  1. Create some routes and run the router
leafeon
    .add('home', '/', () => { /* ... */ })
    .add('contact', '/contact', () => { /* ... */ })
    .setErrorCallback(() => { /* ... */ })
    .run();

Browser support

Supports IE 11+, Chrome 43+, Opera 29+, and Firefox 41+

API

.add(name: string, path: string, callback: function)

Register a route. Use : in path to create a parameter. It returns the router object.

.map(prefixName: string, prefixPath: string, routes: Array)

Register several routes using a prefix name and path. Routes must be an array of object that follows this schema :

{
  name: string,
  path: string,
  callback: function
}

.fetchRoute(name: string[, parameters: object])

Fetch a registered route by name or path. For dynamic routes, It'll generate the path using given parameters.

leafeon.fetchRoute('home'); // or .fetchRoute('/');

// with parameters
leafeon.fetchRoute('/hello/:name', {name: 'Sundown'});

.route: object

Get the current route :

{
    name: string,
    path: string,
    callback: function,
    paramsEnabled: boolean,
    params: array
}

.setErrorCallback(callback: function)

Set the not found exception. It returns the router object.

Example :

// overwrite the default not found exception
leafeon.setErrorCallback(function () {
    document.write('Oh no! Page not found.');
});

.notFoundException()

Call the not found exception callback.

.before(path: string, callback: function)

Register a middleware that will be executed before given path. Type * to target every routes. It returns the router object.

.run([callback: function])

Run the router with registered routes. Optionally, register a middleware that will be executed after every routes callback.

License

This repository is MIT licensed.

Icon made by Good Ware from Flaticon under CC 3.0 BY license.

leafeon's People

Contributors

dependabot[bot] avatar sundowndev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

5l1v3r1

leafeon's Issues

Add support for npm

When using the router with npm, the router variable isn't exported :

module.exports = Router;

Unit tests

Especially for param handling and route generator.

Name parameter on map function

// payload
router.map('hello_', '/prefix/:id/test', [
    {
        name: 'index',
        route: "/",
        callback: function (id) {
            content.innerHTML = '' +
                '<h1>' + id + '</h1>'
            ;
        }
    }
]);

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.