3.3000.0 • Published 8 months ago

autor v3.3000.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

autor

Handles client side routing—that’s it.

autor.min.js = 4kb
autor.min.js gzip = 1.9kb

Setup

Install

npm install autor

Require

Require the module

const tr = require('tr');

Or include the script in HTML

<script src="./public/scripts/autor.js"></script>

Routes

Create a routes module

const StandardService = require('./standard.service.js');

const routes = [
  {
    path: '/',
    service: StandardService
  },
  {
    path: '/about',
    service: StandardService
  }
];

module.exports = routes;

Initialise

Initialise Router with Routes

const Routes = require('./routes.js');
const Router = tr(Routes);

Route Services

A route service is an optional parameter which tells the router how to load each page.

Each route service consists of a get and cancel method.

A standard HTTP request service can be seen below.

function get(route, resolve) {
  let req = new XMLHttpRequest();
  req.addEventListener("load", function() {
    document.querySelector('main').innerHTML = this.responseText;
    resolve();
  });
  req.open("GET", document.origin + route.path + '?partial');
  req.send();
  return req;
}

function cancel(req) {
  req.abort();
}

module.exports = {
  get,
  cancel
}

Note the resolve() method being called to tell the router the load is complete.

0.560.0

10 months ago

0.530.0

10 months ago

0.550.0

10 months ago

0.510.0

10 months ago

0.500.0

10 months ago

0.501.0

10 months ago

1.1000.1

10 months ago

1.1000.0

10 months ago

0.500.0-alpha

10 months ago

3.3000.0

8 months ago

1.1100.0

10 months ago

0.1.1

7 years ago