Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix Request-Level exposure. #27

Merged
merged 3 commits into from

2 participants

@jonpacker

locals was only being called on the request level if something on the same namespace had not been previously exposed globally - and even then, it was being called before the value itself had been saved. I moved the locals call to after the values have been transformed/saved, and made sure it is called every time for request-level calls to expose.

The only way around this would be to delay calling locals until right before the template is rendered. I couldn't see any obvious way of doing that.

@tj tj merged commit 1b24427 into expressjs:master
@tj
Owner
tj commented

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 59 additions and 34 deletions.
  1. +26 −32 lib/express-expose.js
  2. +1 −0  package.json
  3. +32 −2 test/express-expose.test.js
View
58 lib/express-expose.js
@@ -56,38 +56,6 @@ app.expose = function(obj, namespace, name){
namespace = namespace || exports.namespace;
}
- // locals
- function locals(req, res) {
- var appjs = app.exposed(name)
- , resjs = res.exposed(name)
- , js = '';
-
- if (appjs || resjs) {
- js += '// app: \n' + appjs;
- js += '// res: \n' + resjs;
- }
-
- res.locals[name] = js;
- }
-
- // locals
-
- if (!app._exposed[name]) {
- var helpers = {};
- app._exposed[name] = true;
-
- // request level
- if (req) locals(req, this);
-
- // app level
- if (!req) {
- app.use(function(req, res, next){
- locals(req, res);
- next();
- });
- }
- }
-
// buffer string
if ('string' == typeof obj) {
this.js = this.js || {};
@@ -106,6 +74,32 @@ app.expose = function(obj, namespace, name){
this.expose('\n');
}
+ // locals
+ function locals(req, res) {
+ var appjs = app.exposed(name)
+ , resjs = res.exposed(name)
+ , js = '';
+
+ if (appjs || resjs) {
+ js += '// app: \n' + appjs;
+ js += '// res: \n' + resjs;
+ }
+
+ res.locals[name] = js;
+ }
+
+ // app level locals
+ if (!req && !app._exposed[name]) {
+ app._exposed[name] = true;
+ app.use(function(req, res, next){
+ locals(req, res);
+ next();
+ });
+ // request level locals
+ } else if (req) {
+ locals(req, this);
+ }
+
return this;
};
View
1  package.json
@@ -8,6 +8,7 @@
, "mocha": "*"
, "should": "*"
, "jade": "*"
+ , "supertest": "*"
}
, "main": "index"
}
View
34 test/express-expose.test.js
@@ -7,7 +7,8 @@ var express = require('express')
, expose = require('../')
, assert = require('assert')
, should = require('should')
- , vm = require('vm');
+ , vm = require('vm')
+ , request = require('supertest');
module.exports = {
'test app.expose(name)': function(){
@@ -132,5 +133,34 @@ module.exports = {
vm.runInNewContext(js, scope);
scope.sub(8,7).should.equal(1);
scope.should.not.have.property('add');
- }
+ },
+
+ 'test res.expose(str)': function(done){
+ var app = express();
+ app.set('view engine', 'jade');
+ app.set('views', __dirname + '/views');
+
+ app.expose('var user = { name: "tj" };')
+ app.expose('user.id = 50;')
+
+ app.get('/', function(req, res) {
+ res.expose('var lang = "en";');
+ res.expose('var country = "no";');
+ res.render('index');
+ });
+
+ request(app)
+ .get('/')
+ .end(function(err, res) {
+ if (err) throw err;
+
+ var scope = {};
+ vm.runInNewContext(res.text, scope);
+ scope.user.name.should.equal('tj');
+ scope.user.id.should.equal(50);
+ scope.country.should.equal('no');
+ scope.lang.should.equal('en');
+ done();
+ });
+ },
};
Something went wrong with that request. Please try again.