welsonjs/lib/router.js

40 lines
1005 B
JavaScript
Raw Normal View History

// router.js
// Namhyeon Go <abuse@catswords.net>
// https://github.com/gnh1201/welsonjs
function RouteModel(path, callback) {
this.path = path;
this.callback = callback;
}
2023-10-30 07:11:51 +00:00
function RouterObject() {
var routes = [];
2023-10-30 07:11:51 +00:00
this.render = function(filename, data) {};
2023-10-30 07:11:51 +00:00
this.setRender = function(render) {
this.render = render;
};
2023-10-30 07:11:51 +00:00
this.add = function(path, callback) {
routes.push(new RouteModel(path, callback));
};
2023-10-30 07:11:51 +00:00
this.go = function(path) {
var model = routes.find(function(x) {
2023-10-30 07:11:51 +00:00
return (x.path == path);
});
if (typeof model !== "undefined") {
2024-03-20 12:12:59 +00:00
//try {
2023-10-30 07:11:51 +00:00
model.callback(this.render);
2024-03-20 12:12:59 +00:00
//} catch (e) {
// console.error(path, e.message);
//}
2023-10-30 07:11:51 +00:00
}
};
2023-10-30 07:11:51 +00:00
}
exports.Router = new RouterObject();
exports.VERSIONINFO = "URI Router (router.js) version 0.1.1";
2023-10-30 07:11:51 +00:00
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;