0.0.5 • Published 5 years ago
js-spa-router v0.0.5
js-spa-router
Simple JS Routing Framework for SPA. Doesn't use external libraries. The underlying state management system is also built natively. This is WIP but still you can try and give your feedback.
API
Lifecycle Hooks
In SPA Router every route has a list of state or otherwise called as route hooks which enables the developer to configure appropriate callback actions.
Hooks
mountRouteMounts the current route and setup the foundation. You can initialize the route context here.loadDataLoad the data for the current route. Any call to server can be configured here.renderRender the UI for the current route. Initialize all components and perform other page specific activities.unloadDataRemove unwanted data from the current route context. Unloads all data that are not needed in the future.unmountRouteRemove / Clears the current route context.destroyDestructor for the current route.
Transition
Transition logically notifies the state change. It happens in three phases as given below:
Syntax
JSSPARouter.transitions() Returns all possible transitions
Example
transition.beforecalled before the transition startstransition.startcalled during the transitiontransition.aftercalled after the transition completes
State
Every state get executed in a sequence of five phases. This will be super useful when you logically want to split your action.
Syntax
JSSPARouter.states() Returns all possible states
Example
state.leaveBefore the current state leavesstate.leftAfter the current state leftstate.enterWhen the current state enterstate.reachedWhen the current state reachstate.enteredAfter the current state entered
Usage
// Sample JS SPA Router
/*
* @method callback
* @description Hook to handle route state change and route transitions
* @param {string} type Describes The Transition Type
* @param {object} transition The real transition object
* @param {object} currentRoute The current route object
*/
let callback = function(type, transition, currentRoute) {
};
// Sample JS SPA Router Config Payload
let jssparouter = JSSPARouter.map({
"/": {
id: 'root',
path: '/',
callback: callback,
index: '/',
child: {
"categories": {
id: 'categories',
path: 'categories',
callback: callback,
child: {
"mobile": {
id: 'mobile',
path: 'mobile',
callback: callback
},
"desktop": {
id: 'desktop',
path: 'desktop',
callback: callback
},
"laptop": {
id: 'laptop',
path: 'laptop',
callback: callback
}
}
},
"support": {
id: 'support',
path: 'support',
callback: callback,
child: {
"email": {
id: 'email',
path: 'email',
callback: callback
},
"phone": {
id: 'phone',
path: 'phone',
callback: callback
}
}
}
}
}
}); <button type="button" onclick="JSSPARouter.gotoPath('/categories')">Categories</button>
<button type="button" onclick="JSSPARouter.gotoPath('/categories/mobile')">Mobile</button>
<button type="button" onclick="JSSPARouter.gotoPath('/categories/laptop')">Laptop</button>
<button type="button" onclick="JSSPARouter.gotoPath('/categories/desktop')">Desktop</button>
<button type="button" onclick="JSSPARouter.gotoPath('/support')">Support</button>
<button type="button" onclick="JSSPARouter.gotoPath('/support/email')">Email</button>
<button type="button" onclick="JSSPARouter.gotoPath('/support/phone')">Phone</button>Installation
yarn add js-spa-router