Skip to content

Instantly share code, notes, and snippets.

@back2dos
Last active December 21, 2015 22:39
Show Gist options
  • Select an option

  • Save back2dos/6376645 to your computer and use it in GitHub Desktop.

Select an option

Save back2dos/6376645 to your computer and use it in GitHub Desktop.
Scoped routers.
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