Permalink
Browse files

remove app.locals.use and res.locals.use

there are a few reasons for this:

  a) less API, simpler implementation ...
  b) difficult to inherit cleanly from subapps
  c) effectively the same as parallelized middleware (use connect-parallel for example)

lastly this api in a sense promotes some obscure uses since
they may be scattered throughout rather than explicitly
given to specific routes or used globally as middleware etc
  • Loading branch information...
1 parent 78845e7 commit 4f7c4d10513bf2b04e7ddf5c1fafacc15fa3ecf2 @tj tj committed Jul 6, 2012
Showing with 13 additions and 303 deletions.
  1. +1 −2 lib/application.js
  2. +10 −36 lib/response.js
  3. +0 −12 lib/utils.js
  4. +2 −2 test/app.locals.js
  5. +0 −114 test/app.locals.use.js
  6. +0 −137 test/res.locals.use.js
View
3 lib/application.js
@@ -58,12 +58,11 @@ app.defaultConfiguration = function(){
this.use(connect.query());
this.use(middleware.init(this));
- // inherit view callbacks
+ // inherit protos
this.on('mount', function(parent){
this.request.__proto__ = parent.request;
this.response.__proto__ = parent.response;
this.engines.__proto__ = parent.engines;
- parent.viewCallbacks = parent.viewCallbacks.concat(this.viewCallbacks);
});
// router
View
46 lib/response.js
@@ -574,41 +574,15 @@ res.render = function(view, options, fn){
fn = options, options = {};
}
- function render() {
- // merge res.locals
- options.locals = self.locals;
-
- // default callback to respond
- fn = fn || function(err, str){
- if (err) return req.next(err);
- self.send(str);
- };
-
- // render
- app.render(view, options, fn);
- }
+ // merge res.locals
+ options.locals = self.locals;
- // invoke view callbacks
- var callbacks = app.viewCallbacks.concat(self.viewCallbacks)
- , pending = callbacks.length
- , len = pending
- , done;
-
- if (len) {
- for (var i = 0; i < len; ++i) {
- callbacks[i](req, self, function(err){
- if (done) return;
-
- if (err) {
- req.next(err);
- done = true;
- return;
- }
-
- --pending || render();
- });
- }
- } else {
- render();
- }
+ // default callback to respond
+ fn = fn || function(err, str){
+ if (err) return req.next(err);
+ self.send(str);
+ };
+
+ // render
+ app.render(view, options, fn);
};
View
12 lib/utils.js
@@ -23,18 +23,6 @@ exports.locals = function(obj){
return obj;
};
- locals.use = function(fn){
- if (3 == fn.length) {
- obj.viewCallbacks.push(fn);
- } else {
- obj.viewCallbacks.push(function(req, res, done){
- fn(req, res);
- done();
- });
- }
- return obj;
- };
-
return locals;
};
View
4 test/app.locals.js
@@ -6,10 +6,10 @@ describe('app', function(){
describe('.locals(obj)', function(){
it('should merge locals', function(){
var app = express();
- Object.keys(app.locals).should.eql(['use', 'settings']);
+ Object.keys(app.locals).should.eql(['settings']);
app.locals({ user: 'tobi', age: 1 });
app.locals({ age: 2 });
- Object.keys(app.locals).should.eql(['use', 'settings', 'user', 'age']);
+ Object.keys(app.locals).should.eql(['settings', 'user', 'age']);
app.locals.user.should.equal('tobi');
app.locals.age.should.equal(2);
})
View
114 test/app.locals.use.js
@@ -1,114 +0,0 @@
-
-var express = require('../')
- , request = require('./support/http');
-
-describe('app', function(){
- describe('.locals.use(fn)', function(){
- it('should run in parallel on res.render()', function(done){
- var app = express();
- var calls = [];
- app.set('views', __dirname + '/fixtures');
-
- app.locals.first = 'tobi';
-
- app.locals.use(function(req, res, done){
- process.nextTick(function(){
- calls.push('one');
- res.locals.last = 'holowaychuk';
- done();
- });
- });
-
- app.locals.use(function(req, res, done){
- process.nextTick(function(){
- calls.push('two');
- res.locals.species = 'ferret';
- done();
- });
- });
-
- app.use(function(req, res){
- calls.push('use');
- res.render('pet.jade');
- });
-
- request(app)
- .get('/')
- .end(function(err, res){
- if (err) return done(err);
- calls.should.eql(['use', 'one', 'two']);
- res.text.should.equal('<p>tobi holowaychuk is a ferret</p>');
- done();
- })
- })
-
- describe('with arity < 3', function(){
- it('should done() for you', function(done){
- var app = express();
-
- app.set('views', __dirname + '/fixtures');
- app.locals.first = 'tobi';
-
- app.locals.use(function(req, res){
- res.locals.last = 'holowaychuk';
- res.locals.species = 'ferret';
- });
-
- app.use(function(req, res){
- res.render('pet.jade');
- });
-
- request(app)
- .get('/')
- .expect('<p>tobi holowaychuk is a ferret</p>', done);
- })
- })
-
- it('should not override res.render() locals', function(done){
- var app = express();
-
- app.set('views', __dirname + '/fixtures');
- app.locals.first = 'tobi';
-
- app.locals.use(function(req, res){
- res.locals.last = 'holowaychuk';
- res.locals.species = 'ferret';
- });
-
- app.use(function(req, res){
- res.render('pet.jade', { last: 'ibot' });
- });
-
- request(app)
- .get('/')
- .expect('<p>tobi ibot is a ferret</p>', done);
- })
-
- it('should work when mounted', function(done){
- var app = express();
- var pet = express();
-
- app.set('views', __dirname + '/fixtures');
-
- app.locals.first = 'tobi';
-
- app.locals.use(function(req, res){
- res.locals.last = 'holowaychuk';
- });
-
- pet.locals.use(function(req, res){
- res.locals.species = 'ferret';
- });
-
- app.use(function(req, res){
- res.render('pet.jade');
- });
-
- app.use(pet);
-
- request(app)
- .get('/')
- .expect('<p>tobi holowaychuk is a ferret</p>', done);
- })
- })
-})
View
137 test/res.locals.use.js
@@ -1,137 +0,0 @@
-
-var express = require('../')
- , request = require('./support/http');
-
-describe('res', function(){
- describe('.locals.use(fn)', function(){
- it('should run in parallel on res.render()', function(done){
- var app = express();
- var calls = [];
- app.set('views', __dirname + '/fixtures');
-
- app.locals.first = 'tobi';
-
- app.use(function(req, res, next){
- res.locals.use(function(req, res, done){
- process.nextTick(function(){
- calls.push('one');
- res.locals.last = 'holowaychuk';
- done();
- });
- });
- next();
- });
-
- app.use(function(req, res, next){
- res.locals.use(function(req, res, done){
- process.nextTick(function(){
- calls.push('two');
- res.locals.species = 'ferret';
- done();
- });
- });
- next();
- });
-
- app.use(function(req, res){
- calls.push('render');
- res.render('pet.jade');
- });
-
- request(app)
- .get('/')
- .end(function(err, res){
- calls.should.eql(['render', 'one', 'two']);
- res.text.should.equal('<p>tobi holowaychuk is a ferret</p>');
- done();
- })
- })
-
- describe('with arity < 3', function(){
- it('should done() for you', function(done){
- var app = express();
-
- app.set('views', __dirname + '/fixtures');
- app.locals.first = 'tobi';
-
- app.use(function(req, res, next){
- res.locals.use(function(req, res){
- res.locals.last = 'holowaychuk';
- res.locals.species = 'ferret';
- });
- next();
- });
-
- app.use(function(req, res){
- res.render('pet.jade');
- });
-
- request(app)
- .get('/')
- .end(function(err, res){
- res.text.should.equal('<p>tobi holowaychuk is a ferret</p>');
- done();
- })
- })
- })
-
- it('should not override res.render() locals', function(done){
- var app = express();
-
- app.set('views', __dirname + '/fixtures');
- app.locals.first = 'tobi';
-
- app.use(function(req, res, next){
- res.locals.use(function(req, res){
- res.locals.last = 'holowaychuk';
- res.locals.species = 'ferret';
- });
- next();
- });
-
- app.use(function(req, res){
- res.render('pet.jade', { last: 'ibot' });
- });
-
- request(app)
- .get('/')
- .end(function(err, res){
- res.text.should.equal('<p>tobi ibot is a ferret</p>');
- done();
- })
- })
-
- it('should work when mounted', function(done){
- var app = express();
- var pet = express();
-
- app.set('views', __dirname + '/fixtures');
-
- app.locals.first = 'tobi';
-
- app.use(function(req, res, next){
- res.locals.use(function(){
- res.locals.last = 'holowaychuk';
- });
- next();
- });
-
- pet.use(function(req, res, next){
- res.locals.use(function(){
- res.locals.species = 'ferret';
- });
- next();
- });
-
- app.use(function(req, res){
- res.render('pet.jade');
- });
-
- app.use(pet);
-
- request(app)
- .get('/')
- .expect('<p>tobi holowaychuk is a ferret</p>', done);
- })
- })
-})

1 comment on commit 4f7c4d1

@smagch

Yeah, Simple is the best.

Please sign in to comment.