add "view" constructor setting to override view behaviour
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Caridy Patino · committed by TJ Holowaychuk · parent 49cc1a7 · Browse files · 5 files·+131 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+131 added−8 removed
- tests:+28 added−5 removed
- other:+103 added−3 removed
Provenance
- Commit
0b4e2df- Tree
cc61a1b- Author
Caridy Patino <caridy@gmail.com>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
49cc1a7
5 files changed+131 added−8 removed
modified:examples/markdown/index.js+2 added-2 removed
12345667891030 unmodified lines414243444545
/** * Module dependencies. */
var express = require('../../')var express = require('../..') , fs = require('fs') , md = require('github-flavored-markdown').parse;
var app = module.exports = express();30 unmodified lines
if (!module.parent) { app.listen(3000); console.log('Express started on port 3000');}No newline at end of file}added:examples/view-constructor/github-view.js+52 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
/** * Module dependencies. */
var http = require('http') , path = require('path') , extname = path.extname
/** * Expose `GithubView`. */
module.exports = GithubView;
/** * Custom view that fetches and renders * remove github templates. You could * render templates from a database etc. */
function GithubView(name, options){ this.name = name; options = options || {}; this.engine = options.engines[extname(name)]; // "root" is the app.set('views') setting, however // in your own implementation you could ignore this this.path = '/' + options.root + '/master/' + name;}
/** * Render the view. */
GithubView.prototype.render = function(options, fn){ var self = this; var opts = { host: 'rawgithub.com', port: 80, path: this.path, method: 'GET' };
http.request(opts, function(res) { var buf = ''; res.setEncoding('utf8'); res.on('data', function(str){ buf += str }); res.on('end', function(){ self.engine(buf, options, fn); }); }).end();};added:examples/view-constructor/index.js+47 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
/** * Module dependencies. */
var express = require('../../') , http = require('http') , GithubView = require('./github-view') , md = require('github-flavored-markdown').parse;
var app = module.exports = express();
// register .md as an engine in express view systemapp.engine('md', function(str, options, fn){ try { var html = md(str); html = html.replace(/\{([^}]+)\}/g, function(_, name){ return options[name] || ''; }) fn(null, html); } catch(err) { fn(err); }})
// pointing to a particular github repo to load files from itapp.set('views', 'visionmedia/express');
// register a new view constructorapp.set('view', GithubView);
app.get('/', function(req, res){ // rendering a view relative to the repo. // app.locals, res.locals, and locals passed // work like they normally would res.render('examples/markdown/views/index.md', { title: 'Example' });})
app.get('/Readme.md', function(req, res){ // rendering a view from https://github.com/visionmedia/express/blob/master/Readme.md res.render('Readme.md');})
if (!module.parent) { app.listen(4000); console.log('Express started on port 3000');}modified:lib/application.js+2 added-1 removed
77 unmodified lines78 // default locals79 this.locals.settings = this.settings;80 81 // default configuration82+ this.set('view', View);83 this.set('views', process.cwd() + '/views');84 this.set('jsonp callback name', 'callback');85 86 this.configure('development', function(){392 unmodified lines479 if (opts.cache) view = cache[name];480 481 // view482 if (!view) {482- view = new View(name, {483+ view = new (this.get('view'))(name, {484 defaultEngine: this.get('view engine'),485 root: this.get('views'),486 engines: engines487 });44 unmodified linesmodified:test/app.render.js+28 added-5 removed
12 unmodified lines13 str.should.equal('<p>tobi</p>');14 done();15 })16 })17- 17+18 it('should support absolute paths with "view engine"', function(done){19 var app = express();20 21 app.set('view engine', 'jade');17 unmodified lines39 str.should.equal('<p>tobi</p>');40 done();41 })42 })43- 43+44 it('should support index.<engine>', function(done){45 var app = express();46 47 app.set('views', __dirname + '/fixtures');60 unmodified lines108 done();109 })110 })111 })112+113+ describe('when a "view" constructor is given', function(){114+ it('should create an instance of it', function(done){115+ var app = express();116+117+ function View(name, options){118+ this.name = name;119+ this.path = 'path is required by application.js as a signal of success even though it is not used there.';120+ }121+122+ View.prototype.render = function(options, fn){123+ fn(null, 'abstract engine');124+ };125+126+ app.set('view', View);127+128+ app.render('something', function(err, str){129+ if (err) return done(err);130+ str.should.equal('abstract engine');131+ done();132+ })133+ })134+ })135 })113- 136+137 describe('.render(name, options, fn)', function(){138 it('should render the template', function(done){139 var app = express();140 6 unmodified lines147 str.should.equal('<p>tobi</p>');148 done();149 })150 })128- 151+152 it('should expose app.locals', function(done){153 var app = express();154 155 app.set('views', __dirname + '/fixtures');4 unmodified lines160 str.should.equal('<p>tobi</p>');161 done();162 })163 })141- 164+165 it('should give precedence to app.render() locals', function(done){166 var app = express();167 168 app.set('views', __dirname + '/fixtures');11 unmodified lines