Giter Site home page Giter Site logo

fdt2k / hash-router Goto Github PK

View Code? Open in Web Editor NEW

This project forked from michaelsogos/hash-router

0.0 3.0 0.0 65.37 MB

Tiny and lightweight browser router library, developed with SPA in mind :)

License: MIT License

JavaScript 97.00% HTML 3.00%

hash-router's Introduction

GitHub version Bower version Build Status Circle CI Join the chat at https://gitter.im/michaelsogos/Hash-Router

Hash-Router

Tiny and lightweight browser router library, developed with SPA in mind :)

Documentation & Examples

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.

hash-router's People

Contributors

michaelsogos avatar fdt2k avatar

Watchers

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