Last active
December 21, 2015 22:39
-
-
Save back2dos/6376645 to your computer and use it in GitHub Desktop.
Scoped routers.
This file contains hidden or 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
| function wrapRouter(addRoute, basicFunctionality) { | |
| return { | |
| register: function (rule, handler) { | |
| require("util").inherits(handler, basicFunctionality); | |
| var factory = function (req, res) { | |
| this.request = req; | |
| this.response = res; | |
| }; | |
| factory.prototype = handler; | |
| addRoute(rule, function (req, res) { | |
| new factory(req, res).run(); | |
| }); | |
| } | |
| } | |
| } | |
| var router = wrapRouter(some3rdPartyRouter.addRoute, { | |
| error: someTemplateEngine.compile("error.tpl"), | |
| withErrorHandler: function (successHandler) { | |
| var self = this; | |
| return function (err, data) { | |
| if (err == null) successHandler.call(self, data); | |
| else self.error.render(self.response, err); | |
| } | |
| }, | |
| makeQuery: someFancyParser.urlToMongoDbSelector, | |
| }); | |
| router.register(someRule, { | |
| init: function () { | |
| this.getData(this.request.url, this.withErrorHandler(this.handleData)); | |
| }, | |
| template: someTemplateEngine.compile("showdata.tpl"), | |
| getData: function (query, callback) { | |
| myDb.findAll(makeQuery.process(query), callback) | |
| }, | |
| handleData: function (data) { | |
| this.template.render(this.response, data); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment