Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed default namespace to "app"

  • Loading branch information...
commit 7f67f425d8d3d5946f2b9c2f45501e3a84f8c784 1 parent 9ae6cfb
TJ Holowaychuk tj authored
Showing with 20 additions and 62 deletions.
  1. +6 −48 Readme.md
  2. +1 −1  lib/express-expose.js
  3. +13 −13 test/express-expose.test.js
54 Readme.md
View
@@ -18,7 +18,7 @@
### Exposing Objects
- A common use-case for exposing objects to the client-side would be exposing some properties, perhaps the express configuration. The call to `app.expose(obj)` below defaults to exposing the properties to `express.*`, so for example `express.views`, `express.title`, etc.
+ A common use-case for exposing objects to the client-side would be exposing some properties, perhaps the express configuration. The call to `app.expose(obj)` below defaults to exposing the properties to `app.*`, so for example `app.views`, `app.title`, etc.
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
@@ -32,9 +32,9 @@
var math = { add: function(a,b){ return a + b; } };
app.expose(math, 'utils').helpers(math);
- Sometimes you might want to output to a different area, so for this we can pass an additional param "languages" which tells express which buffer to write to, which ends up providing us with the local variable "languages" in our template, where the default is "javascript".
+ Sometimes you might want to output to a different area, so for this we can pass an additional param "languages" which tells express which buffer to write to, which ends up providing us with the local variable "languages" in our template, where the default is "javascript". The "app" string here is the namespace.
- app.expose({ en: 'English', fr: 'French' }, 'express', 'languages');
+ app.expose({ en: 'English', fr: 'French' }, 'app', 'languages');
You'll then want to output the default buffer (or others) to your template, in Jade this would look something like:
@@ -73,63 +73,21 @@
notify();
});
-### Exposing Entire Modules
-
- Exposing an entire module as-is is possible as well, this primarily
- useful when the module relies on internal closures and state.
-
- The following exposes "color.dark()", "color.light()" etc by default based
- on the basename of the `path` given, however we pass "utils.color" as a custom namespace.
-
- app.exposeModule(__dirname + '/color', 'utils.color');
-
### Request-Level Exposure
- Finally we can apply all of the above at the request-level as well, below we expose "express.current.user" as `{ name: 'tj' }`, for the specific request only.
+ Finally we can apply all of the above at the request-level as well, below we expose "app.current.user" as `{ name: 'tj' }`, for the specific request only.
app.get('/', function(req, res){
var user = { name: 'tj' };
- res.expose(user, 'express.current.user');
+ res.expose(user, 'app.current.user');
res.render('index', { layout: false });
});
-### CommonJS Modules
-
- Similarly we can enable a light-weight commonjs require() implementation simply by calling:
-
- app.exposeRequire();
-
- From that point on, the `namespace` is no longer a dot-delimited property, but a slash-delimited path, for example the following would allow us to `require('utils/color')` within the browser.
-
- app.exposeModule(__dirname + '/color', 'utils/color');
-
- By default the path defaults to the basename of the path used to load the module's contents, so we could remove "utils/color", allowing us to `require('color')`.
-
- The primary benefit of utilizing require() here, is that `color`, or any other module can use require() internally, and as long as we expose those modules as well, they will work in the browser.
-
- This of course works for things a side from modules as well:
-
- app.expose(app.settings, 'settings');
-
- Which we can then require in our client:
-
- require('settings')
-
- // => {
- default language: "en"
- , env: "development"
- , hints: true
- , home: "/"
- , title: "Example"
- , view engine: "jade"
- , views: "/Users/tj/Projects/express-expose/examples/views"
- }
-
## License
(The MIT License)
-Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
+Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
2  lib/express-expose.js
View
@@ -27,7 +27,7 @@ exports.version = '0.2.2';
* Default namespace.
*/
-exports.namespace = 'express';
+exports.namespace = 'app';
/**
* Default local variable name.
26 test/express-expose.test.js
View
@@ -15,9 +15,9 @@ module.exports = {
},
'test app.expose(name)': function(){
- var app = express.createServer();
+ var app = express();
app.expose({ one: 1, two: 2, three: 3 });
- app.expose({ title: 'My Site' }, 'express.settings');
+ app.expose({ title: 'My Site' }, 'app.settings');
app.expose({ add: function(a, b){ return a + b; } }, 'utils');
app.expose({ en: 'English' }, 'langs', 'langs');
@@ -26,11 +26,11 @@ module.exports = {
scope.window = scope;
vm.runInNewContext(js, scope);
- scope.express.one.should.equal(1);
- scope.express.two.should.equal(2);
- scope.express.three.should.equal(3);
+ scope.app.one.should.equal(1);
+ scope.app.two.should.equal(2);
+ scope.app.three.should.equal(3);
- scope.express.settings.title.should.equal('My Site');
+ scope.app.settings.title.should.equal('My Site');
scope.utils.add(1,5).should.equal(6);
var js = app.exposed('langs')
@@ -43,7 +43,7 @@ module.exports = {
},
'test app.expose(str)': function(){
- var app = express.createServer();
+ var app = express();
app
.expose('var user = { name: "tj" };')
@@ -58,7 +58,7 @@ module.exports = {
},
'test app.expose(str, null, scope)': function(){
- var app = express.createServer();
+ var app = express();
app
.expose('var user = { name: "tj" };', 'foot')
@@ -78,7 +78,7 @@ module.exports = {
},
'test app.expose(fn) self-calling function': function(){
- var app = express.createServer()
+ var app = express()
, err;
app.expose('var foo;')
@@ -110,7 +110,7 @@ module.exports = {
},
'test app.expose(fn) named function': function(){
- var app = express.createServer()
+ var app = express()
, err;
app.expose(function add(a, b){
@@ -139,7 +139,7 @@ module.exports = {
},
'test app.exposeModule(path)': function(){
- var app = express.createServer();
+ var app = express();
app.exposeModule(__dirname + '/fixtures/color');
@@ -153,7 +153,7 @@ module.exports = {
},
'test app.exposeModule(path, namespace)': function(){
- var app = express.createServer();
+ var app = express();
app.exposeModule(__dirname + '/fixtures/color', 'utils.color');
@@ -205,7 +205,7 @@ module.exports = {
// },
'test app.exposeRequire()': function(){
- var app = express.createServer();
+ var app = express();
app.set('title', 'something');
app.exposeRequire();
Please sign in to comment.
Something went wrong with that request. Please try again.