Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/michaelsogos/Hash-Router
Tiny and lightweight browser router library, developed with SPA in mind :)
https://github.com/michaelsogos/Hash-Router
Last synced: 3 months ago
JSON representation
Tiny and lightweight browser router library, developed with SPA in mind :)
- Host: GitHub
- URL: https://github.com/michaelsogos/Hash-Router
- Owner: michaelsogos
- License: mit
- Created: 2014-10-19T00:53:30.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2018-01-22T09:19:45.000Z (almost 7 years ago)
- Last Synced: 2024-08-09T08:07:45.670Z (3 months ago)
- Language: JavaScript
- Size: 63.8 MB
- Stars: 40
- Watchers: 7
- Forks: 13
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![GitHub version](https://badge.fury.io/gh/michaelsogos%2Fhash-router.svg)](https://badge.fury.io/gh/michaelsogos%2Fhash-router)
[![Bower version](https://badge.fury.io/bo/hash-router.svg)](https://badge.fury.io/bo/hash-router)
[![Build Status](https://travis-ci.org/michaelsogos/Hash-Router.svg?branch=master)](https://travis-ci.org/michaelsogos/Hash-Router)
[![Circle CI](https://circleci.com/gh/michaelsogos/Hash-Router/tree/master.svg?style=shield)](https://circleci.com/gh/michaelsogos/Hash-Router/tree/master)
[![Join the chat at https://gitter.im/michaelsogos/Hash-Router](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/michaelsogos/Hash-Router?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)# Hash-Router #
Tiny and lightweight browser router library, developed with SPA in mind :)
[Documentation & Examples](http://michaelsogos.github.io/Hash-Router)
# Why #
I tried for two weeks many useful libraries (sammy.js, director.js, path.js, crossroads.js, etc.)
that do the same thing that i do with my library, but every time a piece of
code/functionality/behaviour lacks to reach the "perfection".I'm working a lot developing SPA (single page application) web application, and in my opinion,
an SPA web application have to be based on lazy loading concept, that's why the other libraries fail.
In an lazy loading scenario the router rules could be lazy loaded.So if i want to load a js file (controller?) that isn't loaded yet?
And if inside this file i will found new set of router rules?
And if one of these rules is the same that is executing now?# How to manage lazy loading #
To better understanding imagine that your main application starts with some needed libarary
(jquery, bootstrap, etc.) and the main js controller.
Of course a small set of router rules already existing in the main js controller,
but not every rules needed to the entire web app.These initial rules are a starting point to load the right controller when a route is requested.
When the controller js files is loaded (as you want, like RequiredJS or any library suitable for you)
you have to:
- Register any route rules found into the controller
- Run route for the actual path running, because the main route was only a starting point
# How to manage route function priority #
For any route is possible to specify a function to execute when the hash change, and to better organize
the code you can specify an before and after function too.
When a function make an ajax async request (or any async call) the issue could be that next function will be
executed before the actual function complete its execution.
So every function calls are syncronized thanks to a task manager (see the example).
The difference with other libraries is that you have to specify when your function/task has been done,
else the assumption is to not execute next function.
Actually the priority is BEFORE -> ON -> AFTER,
but there isn't a real difference using BEFORE or ON.
You can insert the custom code all in one method (not important which) without problem.
The only advantage is that any value returned from one function will be sent to the next function (see example),
this should really improve your code organization.
Typically i evaluate some condition on BEFORE and then execute specific code to the ON.
# Open Scenario #
The library can be used in normal scenario like you can do with path.js (or sammy.js or ...),
even if you don't need lazy loading capabilities.
The library have no opinion about how you lazy load files, and most important
the context passed to route functions is completely customizable as you want.
# Copyright and Licensing #
I hope you can appreciate my work and help me to improve this library with your suggestions.
Use it freely as you want!
Copyright (c) 2014 - 2016 Michael Sogos, released under the MIT license.