Skip to content
Browse files

move express stuff into it's own file

  • Loading branch information...
1 parent 2969f2d commit e91ff54b95052250d4f8c50ca56d51c87f0946ba @Dashron committed Nov 5, 2012
Showing with 45 additions and 35 deletions.
  1. +5 −35 bifocals.js
  2. +40 −0 lib/express_extensions.js
View
40 bifocals.js
@@ -590,42 +590,12 @@ Renderer.prototype.end = function renderer_end(fn) {
* }));
*
*
- *
* @param {[type]} options [description]
* @return {[type]} [description]
*/
module.exports.__express = function (options) {
- var app = options.app;
-
- return function (req, res, next) {
- var root = new Bifocals(res);
- root.dir = app.get('views') + '/';
- root.content_type = 'text/html';
-
- res.child = function (key, template) {
- return root.child(key, template);
- };
-
- res.render = function (name, opts, fn) {
- if (typeof opts === "object") {
- Object.keys(opts).forEach(function (value, key) {
- root.set(key, value);
- });
- }
-
- if (typeof fn === "function") {
- root.error(function (err) {
- fn(err);
- });
-
- root.end(function() {
- fn(null);
- });
- }
-
- root.render(name);
- };
-
- next();
- }
-}
+ // lazy load, to help modularize some parts the library.
+ var middleware = require('./lib/express_extensions').middleware;
+ module.exports.__express = middleware;
+ return middleware(options);
+};
View
40 lib/express_extensions.js
@@ -0,0 +1,40 @@
+"use strict";
+
+var Bifocals = require('../bifocals').Bifocals;
+
+module.exports.middleware = function (options) {
+ var app = options.app;
+
+ return function (req, res, next) {
+ var root = new Bifocals(res);
+ root.dir = app.get('views') + '/';
+ // todo: pull this from the request's accept headers
+ root.content_type = 'text/html';
+
+ res.child = function (key, template) {
+ return root.child(key, template);
+ };
+
+ res.render = function (name, opts, fn) {
+ if (typeof opts === "object") {
+ Object.keys(opts).forEach(function (value, key) {
+ root.set(key, value);
+ });
+ }
+
+ if (typeof fn === "function") {
+ root.error(function (err) {
+ fn(err);
+ });
+
+ root.end(function() {
+ fn(null);
+ });
+ }
+
+ root.render(name);
+ };
+
+ next();
+ };
+};

0 comments on commit e91ff54

Please sign in to comment.
Something went wrong with that request. Please try again.