-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd7fecd
commit 6914390
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
(function() { | ||
"use strict"; | ||
|
||
var _ = require("lodash"); | ||
var Promise = require("bluebird"); | ||
|
||
module.exports = extension; | ||
|
||
function extension(mach) { | ||
Object.defineProperty(mach.Connection.prototype, "render", { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: render | ||
}); | ||
} | ||
|
||
function render(status, view, locals) { | ||
/*jshint validthis:true */ | ||
var data = locals; | ||
var renderer; | ||
|
||
if (_.isNumber(status)) { | ||
this.status = status; | ||
} else { | ||
data = view; | ||
view = status; | ||
} | ||
|
||
if (!_.isObject(this._controller) || !_.isObject(this._controller._view_manager)) { | ||
return Promise.reject("Cannot render templates without a controller!"); | ||
} | ||
|
||
renderer = this._controller._view_manager.rendererForSignature(view); | ||
|
||
if (!_.isFunction(renderer)) { | ||
return Promise.reject("Template not found "+view); | ||
} | ||
|
||
// TODO: pull content-type from file extension | ||
this.response.contentType = "text/html"; | ||
data = _.extend({ conn: this }, data); | ||
|
||
return renderer(data).bind(this).then(function(content) { | ||
return this.html(content); | ||
}); | ||
} | ||
|
||
}()); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(function() { | ||
"use strict"; | ||
|
||
var assert = require("assert"); | ||
var _ = require("lodash"); | ||
|
||
var renderExtension = require("../../lib/mach/render_extension"); | ||
var mach = require("mach"); | ||
|
||
describe("mach", function() { | ||
describe("renderExtension", function() { | ||
before(function() { | ||
mach.extend(renderExtension); | ||
}); | ||
|
||
it("adds a 'render' function to mach.Connection.prototype", function() { | ||
assert(_.isFunction(mach.Connection.prototype.render)); | ||
}); | ||
}); | ||
}); | ||
|
||
}()); |