Added view reloading
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 · parent 95e6fff · Browse files · 3 files·+79 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
3 files+79 added−2 removed
- other:+79 added−2 removed
Provenance
- Commit
e2570be- Tree
8e337ea- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
95e6fff
3 files changed+79 added−2 removed
modified:examples/jade/app.js+11 added
16 unmodified lines1718192021222324252627282930313233343510 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
app.set('view reloading', { interval: 2000 });//app.enable('view reloading');
// 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('view reloading')) {74+ view.watcher.call(this, this.set('view reloading'));75+ }76+ 77 connect.Server.prototype.listen.call(this, port);78 };79 80 /**85 unmodified linesmodified:lib/express/view.js+61 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 {Object} options76+ * @api private77+ */78+79+exports.watcher = function(options){80+ options = options || { interval: 300000 };81+ (function cache(dir){82+ fs.readdir(dir, function(err, files){83+ if (!err) {84+ files.forEach(function(file){85+ file = dir + '/' + file;86+ fs.stat(file, function(err, stats){87+ if (!err) {88+ if (stats.isFile()) {89+ fs.watchFile(file, options, function(curr, prev){90+ if (curr.mtime > prev.mtime) {91+ cacheView(file);92+ }93+ });94+ } else if (stats.isDirectory()) {95+ cache(file);96+ }97+ }98+ });99+ });100+ }101+ });102+ })(viewRoot(this));103+};104+105+/**106 * Render `view` partial with the given `options`.107 *108 * Options:109 * - `as` Variable name for each `collection` value, defaults to the view name.70 unmodified lines180 options = options || {};181 182 // Defaults183 var self = this,184+ root = viewRoot(this.app),185 ext = extname(view),186 partial = options.partial,127- root = options.root || this.app.set('views') || process.cwd() + '/views',187 layout = options.layout === undefined ? true : options.layout,188 layout = layout === true189 ? 'layout' + ext190 : layout;38 unmodified lines229 }230 231 // Cache contents232 try {174- var str = viewCache[path] || (viewCache[path] = fs.readFileSync(path, 'utf8'));233+ var str = viewCache[path] || cacheView(path);234 } catch (err) {235 return error(err);236 }237 23 unmodified lines