3.3000.0 • Published 8 months ago
autor v3.3000.0
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.