* This is used to generate the render methods used with * * - `displayLogin` * - `respondToLoginFail` * - `displayRegister` * - `respondToRegistrationFail` * * The generated functions have the signature * * function (req, res, *extraParams) {...} * * @param {String} type is either 'lo
(type, localsDefault)
| 31 | * function (req, res, *extraParams) {...} |
| 32 | */ |
| 33 | function renderGenerator (type, localsDefault) { |
| 34 | return function render (req, res) { |
| 35 | var locals, render, layout |
| 36 | , extraLocals |
| 37 | , view = this[type + 'View'](), arity |
| 38 | , trailingArgs = Array.prototype.slice.call(arguments, 2); |
| 39 | |
| 40 | if (res.render) { |
| 41 | locals = |
| 42 | localsDefault.apply(this, trailingArgs); |
| 43 | layout = this['_' + type + 'Layout']; |
| 44 | render = function render (locals) { |
| 45 | if ('undefined' !== typeof layout) { |
| 46 | locals.layout = layout; |
| 47 | } |
| 48 | res.render(view, locals); |
| 49 | }; |
| 50 | |
| 51 | extraLocals = this['_' + type + 'Locals']; |
| 52 | if ('function' !== typeof extraLocals) { |
| 53 | for (var k in extraLocals) { |
| 54 | locals[k] = extraLocals[k]; |
| 55 | } |
| 56 | return render(locals); |
| 57 | } |
| 58 | |
| 59 | arity = extraLocals.length; |
| 60 | if (arity === 2) { |
| 61 | // Dynamic sync locals |
| 62 | extraLocals = extraLocals(req, res); |
| 63 | for (var k in extraLocals) { |
| 64 | locals[k] = extraLocals[k]; |
| 65 | } |
| 66 | return render(locals); |
| 67 | } else if (arity === 3) { |
| 68 | // Dynamic async locals |
| 69 | return extraLocals(req, res, function (err, extraLocals) { |
| 70 | if (err) throw err; // TODO Call global configurable error handler |
| 71 | for (var k in extraLocals) { |
| 72 | locals[k] = extraLocals[k]; |
| 73 | } |
| 74 | return render(locals); |
| 75 | }); |
| 76 | } else { |
| 77 | throw new Error('Your `locals` function must have arity 2 or 3'); |
| 78 | } |
| 79 | } else { |
| 80 | res.writeHead(200, {'Content-Type': 'text/html'}); |
| 81 | if ('function' === typeof view) { |
| 82 | res.end(view.apply(this, trailingArgs)); |
| 83 | } else { |
| 84 | res.end(view); |
| 85 | } |
| 86 | } |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | var renderLogin = renderGenerator('login', |