Permalink
Browse files

Add global exports.

  • Loading branch information...
1 parent feeef79 commit 516d7d34a1333ffb005af54fe93dabfc80a65dfb @vesln committed Feb 19, 2012
Showing with 58 additions and 2 deletions.
  1. +50 −2 lib/exports.js
  2. +8 −0 test/exports.test.js
View
@@ -6,27 +6,40 @@
*/
/**
+ * Dependencies.
+ */
+var merge = require('super');
+
+/**
* Exports.
*
* @type {Object}
*/
var exports = module.exports;
/**
+ * Global exports.
+ *
+ * @type {Object}
+ */
+var data = {};
+
+/**
* Return helper that renders the exposed variable
*
* @param {String} Namespace.
* @returns {Function}
* @api public
*/
exports.helper = function(namespace) {
- var self = this;
var tpl = '<script type="text/javascript">{:namespace}</script>';
namespace || (namespace = 'exports');
return function(req, res) {
+ var exp = merge(merge(data), res.exports);
+
return tpl.replace('{:namespace}',
- 'var ' + namespace + ' = ' + JSON.stringify(res.exports || {}) + ';');
+ 'var ' + namespace + ' = ' + JSON.stringify(exp) + ';');
};
};
@@ -42,3 +55,38 @@ exports.middleware = function(req, res, next) {
res.exports = {};
next();
};
+
+/**
+ * Reset the data object.
+ *
+ * @returns `this`.
+ * @api public
+ */
+exports.reset = function() {
+ data = {};
+ return this;
+};
+
+/**
+ * Delete a property
+ *
+ * @returns `this`.
+ * @api public
+ */
+exports.del = function(key) {
+ delete data[key];
+ return this;
+};
+
+/**
+ * Sets a global property.
+ *
+ * @param {String} Key.
+ * @param {Mixed} Value.
+ * @returns `this`.
+ * @api public
+ */
+exports.set = function(key, val) {
+ data[key] = val;
+ return this;
+};
View
@@ -28,6 +28,14 @@ describe('exports', function() {
});
describe('when exporting data', function() {
+ it('should presist global exports', function() {
+ var helper = exports.helper();
+ var res = {exports: {}};
+ exports.set('foo', 3);
+ helper({}, res).should.eql('<script type="text/javascript">var exports = {"foo":3};</script>');
+ exports.reset();
+ });
+
it('should convert properties to json', function() {
var res = {exports: {}};
var helper = exports.helper();

0 comments on commit 516d7d3

Please sign in to comment.