Merge branch 'view-reloading' into connect
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parents 95e6fff, 5d94ab0 · Browse files · 4 files·+82 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+82 added−2 removed
- other:+82 added−2 removed
Provenance
- Commit
40f3de3- Tree
8bca958- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
95e6fff- Parent 2
5d94ab0
4 files changed+82 added−2 removed
modified:examples/jade/app.js+12 added
16 unmodified lines171819202122232425262728293031323334353610 unmodified lines16 unmodified lines connect.compiler({ src: pub, enable: ['sass'] }), connect.staticProvider(pub));
// Optional since express defaults to CWD/views
app.set('views', __dirname + '/views');
// Enable auto-reloading of view contents when changed// with an interval of 2000 milliseconds
app.set('reload views', 2000);// or app.enable('reload views'); for defaults
// Re-compile
// Dummy usersvar users = [ { name: 'tj', email: 'tj@sencha.com' }, { name: 'ciaran', email: 'ciaranj@gmail.com' },10 unmodified linesmodified:lib/express/server.js+7 added
9 unmodified lines10 */11 12 var sys = require('sys'),13 url = require('url'),14+ view = require('./view'),15 connect = require('connect'),16 queryString = require('querystring'),17 router = require('connect/middleware/router');18 48 unmodified lines67 68 Server.prototype.listen = function(port){69 this.set('env', process.env.EXPRESS_ENV || process.connectEnv.name);70 this.runConfig('any', this.set('env'));71+72+ // Setup view reloading73+ if (this.set('reload views')) {74+ view.watcher.call(this, this.set('reload views'));75+ }76+ 77 connect.Server.prototype.listen.call(this, port);78 };79 80 /**85 unmodified linesmodified:lib/express/view.js+63 added-2 removed
11 unmodified lines12 var extname = require('path').extname,13 mime = require('connect/utils').mime,14 utils = require('connect/utils'),15 http = require('http'),16+ sys = require('sys'),17 fs = require('fs');18 19 /**20 * Cache supported template engine exports to23 unmodified lines44 viewCache = {};45 };46 47 /**48+ * Cache view at the given `path`.49+ *50+ * @param {String} padth51+ * @return {String}52+ * @api private53+ */54+55+function cacheView(path) {56+ return viewCache[path] = fs.readFileSync(path, 'utf8');57+}58+59+/**60+ * Return view root path for the given `app`.61+ *62+ * @param {express.Server} app63+ * @return {String}64+ * @api private65+ */66+67+function viewRoot(app) {68+ return app.set('views') || process.cwd() + '/views';69+}70+71+/**72+ * Setup view reloading, checking at the given `interval`73+ * which defaults to 5 minutes.74+ *75+ * @param {Numbers} interval76+ * @api private77+ */78+79+exports.watcher = function(interval){80+ interval = interval === true81+ ? 30000082+ : interval;83+ (function cache(dir){84+ fs.readdir(dir, function(err, files){85+ if (!err) {86+ files.forEach(function(file){87+ file = dir + '/' + file;88+ fs.stat(file, function(err, stats){89+ if (!err) {90+ if (stats.isFile()) {91+ fs.watchFile(file, { interval: interval }, function(curr, prev){92+ if (curr.mtime > prev.mtime) {93+ cacheView(file);94+ }95+ });96+ } else if (stats.isDirectory()) {97+ cache(file);98+ }99+ }100+ });101+ });102+ }103+ });104+ })(viewRoot(this));105+};106+107+/**108 * Render `view` partial with the given `options`.109 *110 * Options:111 * - `as` Variable name for each `collection` value, defaults to the view name.70 unmodified lines182 options = options || {};183 184 // Defaults185 var self = this,186+ root = viewRoot(this.app),187 ext = extname(view),188 partial = options.partial,127- root = options.root || this.app.set('views') || process.cwd() + '/views',189 layout = options.layout === undefined ? true : options.layout,190 layout = layout === true191 ? 'layout' + ext192 : layout;38 unmodified lines231 }232 233 // Cache contents234 try {174- var str = viewCache[path] || (viewCache[path] = fs.readFileSync(path, 'utf8'));235+ var str = viewCache[path] || cacheView(path);236 } catch (err) {237 return error(err);238 }239 23 unmodified linesmodified:support/jadenot shown
Its text is not shown, modified.