-
-
Save pantchox/bdfa2092fa93a2b221d42063be3d61fe to your computer and use it in GitHub Desktop.
the-only-router-I-ever-needed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A usage example that I took from a resent JS project.. | |
const Router = require("core/Router"); | |
const routes = require("routes.json"); | |
const store = require("store"); | |
const ui = require("core/ui"); | |
// this function will load the correct screen | |
function dispatch(path, params, query, router) { | |
const screen = require("screens" + "/" + path + "/index"); | |
const props = _.extend({}, (params || {}), | |
{ | |
router : router, // <- so we can define recursive sub routes, and navigate between screens. | |
query : query || null // <- it's sometimes useful to pass params from the url to screens. | |
}); | |
// will basically call React.Render() sometihng else, depends on the enviorment. | |
ui.setRootComponent(screen, props); | |
} | |
// just an example of a before hook | |
function routeACL(done, path, router) { | |
if (store.isLoggedIn && path === "login") | |
return router.navigateTo("main"); | |
if (!store.isLoggedIn && path !== "login") | |
return router.navigateTo("login"); | |
done(); | |
} | |
function main() { | |
const router = new Router(routes, { | |
handler : dispatch, | |
before : routeACL | |
}); | |
store.fetch() | |
.then(() => router.resolve()) | |
.error(() => router.navigateTo("app-load-error")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require("lodash"); | |
const Navigo = require("navigo"); | |
class Router { | |
constructor(routes, config) { | |
let handler = config.handler || (() => null); | |
let before = config.before || ((d) => d()); | |
let router = new Navigo(null, true); | |
_(routes).each( | |
(path, fragment) => | |
router.on( | |
fragment, | |
(params, query) => handler(path, params, query, this), | |
{ before : (done) => before(done, path, this)} | |
)); | |
this.__router = router; | |
this.__paths = _(routes).omitBy((val,key) => _(key).includes("/:") ).invert().value(); | |
}, | |
navigateTo(route) { this.__router.navigate(this.__paths[route]) }, | |
navigateToURL(route) { this.__router.navigate(route) }, | |
resolve() {this.__router.resolve()} | |
} | |
module.exports = Router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"login" : "login", | |
"" : "main", | |
"the-url-path-is-here" : "screen-name-is-here", | |
"register": "register", | |
"register/:deviceID": "register", | |
"register/:deviceID/:pin": "register", | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment