Added view-locals example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 6e26a8d · Browse files · 4 files·+183 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+183 added−0 removed
- other:+183 added−0 removed
Provenance
- Commit
6f102ff- Tree
db36e40- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
6e26a8d
4 files changed+183 added−0 removed
added:examples/view-locals/index.js+131 added
1+2+var express = require('../..')3+ , User = require('./user')4+ , app = express();5+6+app.set('views', __dirname);7+app.set('view engine', 'jade');8+9+// filter ferrets only10+11+function ferrets(user) {12+ return user.species == 'ferret';13+}14+15+// naive nesting approach,16+// delegating errors to next(err)17+// in order to expose the "count"18+// and "users" locals19+20+app.get('/', function(req, res, next){21+ User.count(function(err, count){22+ if (err) return next(err);23+ User.all(function(err, users){24+ if (err) return next(err);25+ res.render('user', {26+ title: 'Users',27+ count: count,28+ users: users.filter(ferrets)29+ });30+ })31+ })32+});33+34+35+36+37+// this approach is cleaner,38+// less nesting and we have39+// the variables available40+// on the request object41+42+function count(req, res, next) {43+ User.count(function(err, count){44+ if (err) return next(err);45+ req.count = count;46+ next();47+ })48+}49+50+function users(req, res, next) {51+ User.all(function(err, users){52+ if (err) return next(err);53+ req.users = users;54+ next();55+ })56+}57+58+app.get('/middleware', count, users, function(req, res, next){59+ res.render('user', {60+ title: 'Users',61+ count: req.count,62+ users: req.users.filter(ferrets)63+ });64+});65+66+67+68+69+// this approach is much like the last70+// however we're explicitly exposing71+// the locals within each middleware72+// 73+// note that this may not always work74+// well, for example here we filter75+// the users in the middleware, which76+// may not be ideal for our application.77+// so in that sense the previous example78+// is more flexible with `req.users`.79+80+function count2(req, res, next) {81+ User.count(function(err, count){82+ if (err) return next(err);83+ res.locals.count = count;84+ next();85+ })86+}87+88+function users2(req, res, next) {89+ User.all(function(err, users){90+ if (err) return next(err);91+ // this would not be ideal for *this*92+ res.locals.users = users.filter(ferrets);93+ next();94+ })95+}96+97+app.get('/middleware-locals', count2, users2, function(req, res, next){98+ // you can see now how we have much less99+ // to pass to res.render(). If we have100+ // several routes related to users this101+ // can be a great productivity booster102+ res.render('user', { title: 'Users' });103+});104+105+106+// let's assume we wanted to load the users107+// and count for every res.render() call, we108+// could use app.locals.use() for this. These109+// are callbacks which run in parallel ONLY110+// when res.render() is invoked. If no views111+// are rendered, there is no overhead.112+113+// This may be ideal if you want to load auxiliary114+// user information, but only for templates. Note115+// that (req, res) are available to you, so you may116+// access req.session.user etc.117+118+// Keep in mind these execute in *parallel*, so these119+// callbacks should not depend on each other, this120+// also makes them slightly more efficient than121+// using middleware which execute sequentially122+123+app.locals.use(count2);124+app.locals.use(users2);125+126+app.get('/locals', function(req, res){127+ res.render('user', { title: 'Users' });128+});129+130+app.listen(3000);131+console.log('Application listening on port 3000');added:examples/view-locals/layout.jade+12 added
1+doctype 52+html3+ head4+ title= title5+ style6+ body {7+ padding: 50px;8+ font: 16px Helvetica, Arial;9+ }10+ body11+ h2= title12+ block contentadded:examples/view-locals/user.jade+8 added
1+2+extends layout3+4+block content5+ for user in users6+ .user7+ h3= user.name8+ p #{user.name} is a #{user.age} year old #{user.species}.added:examples/view-locals/user.js+32 added
1234567891011121314151617181920212223242526272829303132
module.exports = User;
// faux model
function User(name, age, species) { this.name = name; this.age = age; this.species = species;}
User.all = function(fn){ process.nextTick(function(){ fn(null, users); });};
User.count = function(fn){ process.nextTick(function(){ fn(null, users.length); });};
// faux database
var users = [];
users.push(new User('Tobi', 2, 'ferret'));users.push(new User('Loki', 1, 'ferret'));users.push(new User('Jane', 6, 'ferret'));users.push(new User('Luna', 1, 'cat'));users.push(new User('Manny', 1, 'cat'));