[go: up one dir, main page]

FasterJS

A lightweight vanilla JavaScript front-end framework that lets you build a simple website with predefined routes.


Want to go? Type [next] or [prev] in each virtual terminal then press Enter

Benefits of using FasterJS


  • Lightweight size ~ 10kb for unminified version.
  • Both hash-based and history-mode-based routing system are available.
  • Designed to work with simple kinds of basic, static and small webpages that does not really need an enormous frameworks to do just some staff.
  • A framework launched today and is being developed everyday, so a lot of new features are on the way. It's now just a fresh start.
  • This website is already built with FasterJS. Imagine!
  • Easy to use (Try it and you'll see!).

Upcoming Features

This section will be updated continuously.

Currently, our plan is to deliver these features:

  • Dynamic routes and parameters in router config.

Requirements


FasterJS is developed within vanilla JavaScript, so there is no dependency required. All what you need is a good knowledge in HTML5 and JavaScript. For CSS, it depends on your website design.

Installation


First of all, you need to download the framework. If you are planning to use FasterJS as is (you don't care about its codes under the hood), then I suggest to use the minified version faster.min.js then include it before closing the body tag.

Then, you need to set <base /> tag in <head>. This is important for website routeing system stability.

I suggest to create a separate js file to setup FasterJS rather than coding it in <script> tag below the script file.:

Basically, FasterJS works with a one-only whole root container element having this attribute [data-faster-app]. It's a very good idea to use the HTML5 semantic element <main> as a direct child of <body>, but using <div> still a good choice anyway.

It'll be like this way:

Basic Setup


FasterJS divides your pages into components, each one should have the attribute [data-faster-component] and [data-faster-component-id].

It's a good idea to create them using <section> HTML5 semantic tag. The root container [data-faster-app] MUST have the [data-faster-component] elements as a direct children of the root. Nesting components is NOT ALLOWED.

You can surround your [data-faster-app] with elements that you want to keep them available whatever you browse. FasterJS will handle [data-faster-app] ignoring anything inside <body> else.

In fact, you can still use FasterJS without creating [data-faster-component] elements. FasterJS only handles those elements for pagination-routing system. As long the root [data-faster-app] element is found, FasterJS will function as well.

It'll be like this way:

Getting Started


When you include FasterJS framework, the variable FasterJs will be ready for you. After preparing the basics, you could run your website using init() method. It'll be like this way:

Although it's good to go, but we missed some basic setup to move on.

Firstly, the routing system is, by default, hash mode. If you want to change it to history mode, it'll be like this way:

Good to know: ordering codes is not an important matter, but it's desirable to do for a clean code.
Although it, FasterJs.init(); should be in last!

Then, we have to set basePathName, it refers to an absolute path source of your website. If it's in root, set it to / (this is default value, anyway). It'll be like this way:

Good to know: ordering codes is not an important matter, but it's desirable to do for a clean code.
Although it, FasterJs.init(); should be in last after all.
Slashes around the path above are IMPORTANT TO ASSIGN!

Then, we have to register two things: routeMap and routeMethod. To understand its concept, let's have a look at this scenario:

Assuming we have this URL requested: https://www.example.com/subfolder/

/subfolder/ is came from the FasterJs.config.basePathName that we'd set above. Remember that point.
In hash routing mode, FasterJS will inject #! delimiter before routes. This is related with baseRoute option. We're going to talk about it in a while.

Nothing will be injected in history mode.
In FasterJS, the / route is predefined with index() blank method. You can override it.

Routinely, FasterJS will check up if the / route is registered. If it is, it'll fire the index() method. It'll be like this way:

Code 4 image 1

Here, we have to override the index method using registerMethods method in router config. It'll be like this way:

The formula of registerMethods() is an object list with key-value pairs. The key is the path route preceded with /, and value is the function to trigger, having FasterCore parameter.
The parameter FasterCore represents an object contains:
{
    view: FasterJs.view(selector),
    goTo: FasterJs.goTo(route),
    route: { ... },
    currentRoute: '...',
}

We're going to talk about this parameter in a while. Don't worry about that.

Now, the result would be like this way:

Code 4 image 2

Let's have a look at registerMaps() method in router config with this example like this way:

The formula of registerMaps() is an object list with key-value pairs. The key is the route path preceded with /, and value is the name of the function to trigger.
All registered routes MUST be preceded by / slash.

With this new routes that are defined above, let's register their methods. It'll be like this way:

And so on ...


Good to know: If you have two or more routes and you want to trigger one method, you can handle this issue using pipeline character ( | ) like this way:

In hash routing mode only, you can define baseRoute option in router config, the default value is /. To understand its concept, let's have a look at this scenario:

When you navigate to the website and the URL does not have #!, delimiter, FasterJS will redirect to baseRoute route. If the requested URL has #!, then baseRoute will not effect. It'll be like this way:

This idea helps us to redirect the user to a different route either than the index route, like: special offer route page, limited time offer route page, ...


Programmatically, if you need to know the current route, you can use currentRoute property in FasterCore parameter. It'll be like this way:

Also, if you want to navigate to another route, you can use goTo() method in FasterCore parameter. It'll be like this way:

This will update the URL and navigate to the desired route path, exactly like when the user manually he does when normal surfing your website.


In another hand, if the user reaches a URL with a route that is not registered yet, FasterJS will trigger a fallback error.

FasterJS has 4 fallbacks:

  1. noRoutesMap
  2. noRoutesMethods
  3. routeMapNotFound
  4. routeMethodNotFound

Fallback system in FasterJS will not suspend your website or stop running it, but it'll print an error in Console Panel in the browser. It'll be like this way:

Code 6 image 1

Guess what? Instead of waiting for the fallback error to be printed in the Console Panel, you can show up a specific page that's fully customized by you and no error will be printed in Console Panel! Here is how to do this:

  1. Create an element inside [data-faster-app] with this attribute [data-faster-fallback]. Recently, I've suggested to you that it would be a <section> element.
  2. Attach this extra attribute [data-faster-fallback-type] to the element with one of the fallbacks above.

It'll be like this way:

Code 6 image 2

Pretty cool! :-)

Navigation


FasterJS uses any kind of elements tags for navigation, depending on built-in system for both hash-mode and history-mode using [data-faster-link] attribute with the target route value. It'll be like this way:

Router Events


You can assign a specific event when a route is requested.

For this, and assuming that you'd registered your routeMap and its routeMethod, all what you have to do is to register an event using registerEvents() method in router config. It'll be like this way:

Global Events


As router events, FasterJS has 5 types of global events:

  1. beforeInit: This event is fired when the framework is loaded into the browser and before initializing anything of its methods.
  2. init: This event is fired after beforeInit event but before navigating to the requested URL
  3. beforeRouteEnter: This event is fired before starting router core and matching routes.
  4. routeEntered: Similar to the previous event but this event is fired after calling an existing route map and its method.
  5. loaded: At the end, this event is fired after all events and fallbacks.

Fallbacks events are triggered, when needed, before loaded event, following this order above.

To make this idea clear, let's have a look at this diagram below:

Global events diagram

Here's an example of FasterJS events timeline:

Views


Recently, we've talked about the basics of HTML tags to initialize FasterJS with routes and methods. All what we did before is setting up instructions without linking routes with related sections.

Now, to handle views programmatically, we use view(id) method. It'll be like this way:

Under the hood, FasterJS uses CSS display: block|none; for toggling elements. The display property prevents some of transitions, transforms, animations and any other effects that you need to do.

To avoid this, you have to disable componentsTransitions property in config object. It'll be like this way:

This will stop using display CSS property and still managing [data-faster-component] elements using [data-faster-component-activity] property effected by FasterJs.view() method.

In this case, FasterJS will use visibility: visible|hidden; instead of display: block|none; and you'll be free to draw any animation, transform or transition that you want.

For example: prepare a CSS class and manage your effects by adding and removing it using JS using opacity: 0|1;. It's up to you.

Loading Layer


FasterJS also provides custom loading layer for your website. All what you have to do is creating an element (again I suggest to use <section>) having this attribute [data-faster-loading] and set loadingLayer property in config key to true.

This element doesn't have any CSS predefined styles, so you're free how to design it as you like.

Initialization


And now, after this trip together in preparation and processing FasterJS, it's time to initialize it using init() method.

init() method MUST be in the end of script app file to ensure that all preceded codes are setup as well. It'll be like this:

What's Next?


FasterJS has just started and is in a continuous development process, dozens of new features are being tested and improved before officially released into a new versions here in this repo.

This readme file will be updated in each time that FasterJS has a new release.

So, if you're interested in FasterJS and you're waiting for the next, please give us a "Star" button in our GitLab repo and click the bell button and set the notifications setting to "Watch" to receive all updates by us.

Would you like to support us? All what you could do for us is mention FasterJS repo link in your website with <3

Any questions? Feel free to reach me using my email: nael_d@hotmail.com


Reached the end of this documentation and still reading? You're amazing! :-D

If you reach this page, You'll be eliminated!

Hahaha, just kidding 😜 You are safe.

The route that you've requested is not found.
You can go back using back button in your device, or
Go to home page