Coder Social home page Coder Social logo

reneviering / vanilla-ui-router Goto Github PK

View Code? Open in Web Editor NEW
48.0 10.0 5.0 201 KB

Simple vanilla JavaScript router

License: MIT License

JavaScript 100.00%
router javascript vanilla-javascript-router vanilla-ui-router vanilla-js vanillajs vanilla-javascript vanilla spa single-page-applications

vanilla-ui-router's Introduction

npm version Build Status Coverage Status Code Climate devDependency Status Unicorn

Vanilla UI Router

Greenkeeper badge

Simple vanilla JavaScript router to be used inside a single page app to add routing capabilities.

The router comes with zero dependencies and can be used with any other libraries. It's based on the hashchange-Event.

Installation

$ npm install --save vanilla-ui-router

As UMD module this runs everywhere (ES6 modules, CommonJS, AMD and with good ol’ globals).

Usage

Let's assume your initial markup has the following structure:

<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="styles.min.css" />
</head>
<body>

	<!-- Entry point, dynamic content is rendered into this DOM element -->
	<main id="app"></main>

	<!-- Bundle where your JavaScript logic lives, even the router configuration -->
	<script src="bundle.js"></script>
</body>
</html>

Then you could configure the router with the following JavaScript:

import {createRouter} from 'vanilla-ui-router';

// Initialize the router with the dynamic DOM entry point
const router = createRouter(document.getElementById('app'));

router

	// Start route: The server side URL without a hash
	.addRoute('', () => {
		/*
			Use navigateTo(…) to make dynamic route changes, i.e. to redirect to another route
		*/
		router.navigateTo('home');
	})

	.addRoute('home', (domEntryPoint) => {
		domEntryPoint.textContent = 'I am the home route.';
	})

	.addRoute('about/:aboutId/:editable', (domEntryPoint, routeParams) => {
		console.log('I am the about route.');

		/*
			routeParams are extracted from the URL and are casted to the correct type
			(Number/Boolean/String)
		*/
		console.log(routeParams); // => { aboutId: 42, editable:false }
	})

	/*
		If routes get more complex, e.g. you need to render a template URL,
		pass a configuration object as second parameter (instead of the function)
	*/
	.addRoute('route-with-template-url', {
		templateUrl: 'path/to/template.html' // is loaded and gets rendered
	})

	.addRoute('route-with-template-string/:id', {
		templateString: '<p>Lorem ipsum dolor.</p>',
		routeHandler: (domEntryPoint, routeParams) => {
			/*
			It's called just after rendering the template, so you can add route-specific logic.
			But only if needed!
			*/
		}
	})

	/*
		You can also define a templateId, i.e. if you have a template-script inside
		your markup like:

		<script type="text/template" id="template42">
			<p>
				Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor, tenetur?
			</p>
		</script>
	*/
	.addRoute('route-with-template-id/:id', {
		templateId: 'template42'
	})

	.addRoute('route-with-dispose', {
		routeHandler: () => {},
		dispose: () => {
			// Is called before navigating to another route to do some cleanup if needed.
		}
	})

	.addRoute('inject-custom-data', {
		routeHandler: (domEntryPoint, routeParams, {customData}) => {
			// It's passed as the last parameter of the route, for instance to pass a redux store.
		},
	}, { customData: 'moep'}) // if you need to pass custom data to your routes

	.otherwise(() => {
		// If no route configuration matches, the otherwise route is invoked.
		console.log('I am the otherwise route');
		router.navigateTo('404');
	});

License

Please be aware of the licenses of the components we use in this project. Everything else that has been developed by the contributions to this project is under MIT License.

vanilla-ui-router's People

Contributors

chrkhl avatar greenkeeper[bot] avatar haroenv avatar mischah avatar nicolaisueper avatar reneviering avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vanilla-ui-router's Issues

No Type Definitions

I've searched for type definitions both on npm and tsd but can't find. Are there any type definitions for vanilla-ui-router?

New feature 'middleware'

There are situations where you need a global handling for some routes before the route-handlers are invoked.

We need a middleware to handle exactly this.

Change API for disposing a route

At the moment you have to add a configuration property dispose which is called just before navigating to another route.

A common pattern for deregistering an event or for disposing something is invoking the return-value of the routehandler function, e.g.:

const routeHandler = () => {

   return () => {
       console.log('dispose.....');
   }
}

Lazy loading assets

Hi!

Very nice project... I was looking for something just like this.
But, it would be very nice if it could also load assets like embedded js and css in the template files.

One of the most concerns regarding SPA's is about performance. So, is better to load js and css bundles progressively. Having all css and js already in the first page load can be a huge performance issue. It's always nice to get just the assets for that page you're visiting...

It's really difficult to find some router that does that, I can achieve that using a combination with some router library and a module that loads html,css,js that I had to write on my own, but it has jQuery's dependency.

If your router could do such thing it would be perfect...

Great project! Keep it up!

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected 🤖


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

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.