npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

quilk-frontend-router

v1.1.1

Published

A lightweight router for es6. No dependencies. Works perfectly with jQuery and Vanilla JS

Downloads

25

Readme

quilk-frontend-router

A simple es6 router for JS code to run on a predominately server side driven application.

For example, if you were developing a PHP CMS (eg eZ Platform) you could use this router to trigger only the js code needed based on either what the URL is or what HTML elements are on screen.

This prevents excessive build up of events being initialised, using this router means only the controllers needed or the page will be initialised.

Example how to use it

(NB a class will never be called more than once on a page. Once the runner has initialised a class it keeps an internal record to ensure it doesn't duplicated class calls.)

Example app.js

This is your front door into you whole app, app.js: Include the routing module and pass in your routes/attributes.

import $ from 'jquery';
import QuilkFrontendRouter from 'quilk-frontend-router';
import routes from './routes';
$(document).ready( () => { new QuilkFrontendRouter( routes ) } );

You can also turn on verbose(ish) logging with

$(document).ready( () => { new QuilkFrontendRouter( routes, true ) } );
Example routes.js file

In this example your routes file could look like:

// Import your controllers, this will be initiated when a match is hit, either via route of html attr:
import indexController from './controllers/indexController';
import contactUsController from './controllers/contactUsController';
import sliderController from './controllers/sliderController';
import blogController from './controllers/blogController';
import GlobalController from './controllers/GlobalController'
import MainNaviController from './controllers/MainNaviController'
import Select2Controller from './controllers/Select2Controller'

// Now map each controller to 1 or more url paths or html attributes (see below for a fuller explanation of what it is possible.
export default {
    'path': {
        '*': [ GlobalController ]
        '/': [ indexController ],
        '/contactUsController/(thanks)':  [ contactUsController ],
        '/blog-posts/*':  [ blogController, sliderController ]
    },
    'attributes': {
        'content-type': {
          'artical-container': [articleContainerController],
          'article': [articlePageController]
        },
        'select2': {
           '*': [Select2Controller]
        }
        'class': {
          'mega-navi': [MainNaviController],
        }
    }
};

URL PATH MATCHING

Example straight match:

/this-route-only/

The above will only match if the pathname is the same as the key, ie: /this-route-only /this-route-only/

Example key with params:

/some-route/(controller-a|controller-b|controller-b/param_a)

The above will call the array of controller classes provided if the incoming url pathname is any of the following list, but nothing more: /some-route/ /some-route/controller-a /some-route/controller-b /some-route/controller-b/param_a

Exmaple wildcard key:

/somepath/*

The above will call the array of controller classes provided if the incoming url pathname is any of the following list, but nothing more: /some-route/ /some-route/* basically it matches everything after the key /somepath

HTML ATTRIBUTE MATCHING

The router, as can be seen from the example above, be used to initialise es6 classes based on what is on screen in the form of html attributes.

From the example, html attr class will match when a class is found of mega-navi... eg <div class="col-md-4 mega-navi"></div>

There is also wildcard attribute matching, ie trigger simply when the attribute is found regardless of the value.. see the wildcard in the main example above for attr select2.

Example class to be called

The controllers just require a construct and you're good to go. Here is what the indexController could look like. This would console log 'This is the index controller' when the url pathname was '/':

export default class indexController {

    constructor () {
        this.logIt();
    }
    
    logIt () {
        console.log( `This is the index controller` );
    }
}

Example directory structure

controlllers/ <your controller classes here>
lib/ <your general lib classes here>
services/ <your service files here (ie class to call ajax requests etc)>
app.js
routes.js