Added `app.local(name, val)`
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 f98f46c · Browse files · 3 files·+37 added−20 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+37 added−20 removed
- other:+37 added−20 removed
Provenance
- Commit
a4db994- Tree
3218a43- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
f98f46c
3 files changed+37 added−20 removed
modified:docs/guide.md+7 added-9 removed
1157 unmodified lines1158 When mounted, _res.redirect()_ will respect the mount-point. For example if a blog app is mounted at _/blog_, the following will redirect to _/blog/posts_:1159 1160 res.redirect('/posts');1161 1162-### app.helpers(obj)1162+### app.locals(obj)1163 1164-Registers static view helpers.1164+Registers static view locals.1165 1166- app.helpers({1166+ app.locals({1167 name: function(first, last){ return first + ', ' + last }1168 , firstName: 'tj'1169 , lastName: 'holowaychuk'1170 });8 unmodified lines1179 - `settings` the app's settings object1180 - `filename` the view's filename1181 - `layout(path)` specify the layout from within a view1182 1183-This method is aliased as _app.locals()_.1183+### app.dynamicLocals(obj)1184 1185-### app.dynamicHelpers(obj)1186-1187-Registers dynamic view helpers. Dynamic view helpers1185+Registers dynamic view locals. Dynamic locals1186 are simply functions which accept _req_, _res_, and are1189-evaluated against the _Server_ instance before a view is rendered. The _return value_ of this function1187+evaluated against the _Server_ instance before a view is rendered, and are unique to that specific request. The _return value_ of this function1188 becomes the local variable it is associated with.1189 1192- app.dynamicHelpers({1190+ app.dynamicLocals({1191 session: function(req, res){1192 return req.session;1193 }1194 });169 unmodified linesmodified:lib/http.js+25 added-4 removed
14 unmodified lines15 , Router = require('./router')16 , view = require('./view')17 , toArray = require('./utils').toArray18 , methods = router.methods.concat('del', 'all')19+ , res = require('./response')20 , url = require('url')21 , utils = connect.utils;22 23 /**230 unmodified lines254 return this;255 };256 257 /**257- * Register the given view helpers `obj`. This method258- * can be called several times to apply additional helpers.258+ * Assign the view local variable `name` to `val` or return the259+ * local previously assigned to `name`.260 *261+ * @param {String} name262+ * @param {Mixed} val263+ * @return {Mixed} val264+ * @api public265+ */266+267+app.local = function(name, val){268+ this._locals = this._locals || {};269+ return undefined === val270+ ? this._locals[name]271+ : this._locals[name] = val;272+};273+274+/**275+ * Assign several locals with the given `obj`,276+ * or return the locals.277+ *278 * @param {Object} obj261- * @return {Server} for chaining279+ * @return {Object|HTTPServer}280 * @api public281 */282 283 app.locals = function(obj){266- utils.merge(this._locals, obj);284+ if (0 == arguments.length) return this._locals;285+ for (var key in obj) {286+ this.local(key, obj[key]);287+ }288 return this;289 };290 291 /**230 unmodified linesmodified:lib/response.js+5 added-7 removed
440 unmodified lines441 * Assign several locals with the given `obj`,442 * or return the locals.443 *444 * @param {Object} obj445- * @return {Object|Undefined}445+ * @return {Object|ServerResponse}446 * @api public447 */448 449 res.locals = function(obj){450- if (obj) {451- for (var key in obj) {452- this.local(key, obj[key]);453- }454- } else {455- return this._locals;450+ if (0 == arguments.length) return this._locals;451+ for (var key in obj) {452+ this.local(key, obj[key]);453 }454+ return this;455 };