Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Make it possible to expose application level variables as separate files... #15

Closed
wants to merge 3 commits into from

1 participant

Forbes Lindesay
Forbes Lindesay

... at URLs which look like /bundle/:name.js where the default exposed items are at /bundle/javascript.js

To use this feature, just call app.exposeBundles(); It can be passed a function to call before (useful if you want to check a cache first) and a function to call after (useful for minification and saving to the cache).

Forbes Lindesay ForbesLindesay Make it possible to expose application level variables as separate fi…
…les at URLs which look like /bundle/:name.js where the default exposed items are at /bundle/javascript.js


TO use this feature, just call app.exposeBundles();  It can be passed a function to call before (useful if you want to check a cache first) and a function to call after (useful for minification and saving to the cache).
5eb06b0
Forbes Lindesay

The reason for doing this, in case that's not clear, is that for websites where many requests are made, you only want to load the stuff which is different for each request, not the entirety of all JavaScript the site serves. It also offers useful interfaces for people who want to compress/minify/cache their exposed values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 3, 2012
  1. Forbes Lindesay

    Make it possible to expose application level variables as separate fi…

    ForbesLindesay authored
    …les at URLs which look like /bundle/:name.js where the default exposed items are at /bundle/javascript.js
    
    
    TO use this feature, just call app.exposeBundles();  It can be passed a function to call before (useful if you want to check a cache first) and a function to call after (useful for minification and saving to the cache).
  2. Forbes Lindesay

    Fixed typo

    ForbesLindesay authored
  3. Forbes Lindesay
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 8 deletions.
  1. +33 −8 lib/express-expose.js
41 lib/express-expose.js
View
@@ -1,4 +1,3 @@
-
/*!
* express-expose
* Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
@@ -36,6 +35,8 @@ exports.namespace = 'express';
exports.name = 'javascript';
+exports.useBundles = false;
+
/**
* Expose the given `obj` to the client-side, with
* an optional `namespace` defaulting to "express".
@@ -51,7 +52,7 @@ res.expose =
HTTPServer.prototype.expose =
HTTPSServer.prototype.expose = function(obj, namespace, name){
var app = this.app || this;
-
+
app._exposed = app._exposed || {};
// support second arg as name
@@ -68,13 +69,20 @@ HTTPSServer.prototype.expose = function(obj, namespace, name){
var helpers = {};
app._exposed[name] = true;
helpers[name] = function(req, res){
- var appjs = app.exposed(name)
- , resjs = res.exposed(name)
+ var resjs = res.exposed(name)
+ , appjs
, js = '';
-
+ if(!exports.useBundles){
+ appjs = app.exposed(name);
+ }
+
if (appjs || resjs) {
- js += '// app: \n' + appjs;
- js += '// res: \n' + resjs;
+ if(exports.useBundles){
+ js += resjs;
+ } else {
+ js += '// app: \n' + appjs;
+ js += '// res: \n' + resjs;
+ }
}
return js;
@@ -257,4 +265,21 @@ function string(obj) {
} else {
return JSON.stringify(obj);
}
-}
+}
+
+HTTPServer.prototype.exposeBundles =
+HTTPSServer.prototype.exposeBundles = function(before, after){
+ var app = this.app || this;
+ exports.useBundles = true;
+ app.get("/bundle/:name.js", function(req, res){
+ var name = req.params.name || exports.name;
+ before = before || function(name, cb){cb();};
+ after = after || function(name, javascript, cb){cb(javascript);};
+ before(name, function(){
+ var javascript = app.exposed(name);
+ after(name, javascript, function(js){
+ res.end(js);
+ });
+ });
+ });
+};
Something went wrong with that request. Please try again.