Skip to content
Myr

Refactored res.partial()

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 8db1138 · Browse files · 1 file+15 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+15 added−14 removed

  • other:+15 added−14 removed

1 file+15 added−14 removed

modified:lib/express/view.js+15 added-14 removed
147 unmodified lines
148 * @api public149 */150151http.ServerResponse.prototype.partial = function(view, options, ext){152 // Inherit parent view extension when not present153 if (ext && view.indexOf('.') < 0) {154 view += ext;155 }156157 // Allow collection to be passed as second param158 if (options instanceof Array) {159 options = { collection: options };160 }161162 // Defaults163 options = options || {};164 options.locals = options.locals || {};165 options.partial = true;166 options.layout = false;167168 // Collection support169 var collection = options.collection;170 if (collection) {171 var name = options.as || view.split('.')[0],172 len = collection.length;165 var locals = options.locals = options.locals || {};166 locals.collectionLength = len;173 delete options.collection;174 options.locals.collectionLength = len;175 return collection.map(function(val, i){168 if (typeof name === 'string') {169 locals[name] = val;170 } else if (name === global) {171 utils.merge(locals, val);172 } else {173 options.context = val;174 }175 locals.firstInCollection = i === 0;176 locals.indexInCollection = i;177 locals.lastInCollection = i === len - 1;178 return this.render(view, options);176 options.locals.firstInCollection = i === 0;177 options.locals.indexInCollection = i;178 options.locals.lastInCollection = i === len - 1;179 options.object = val;180 return this.partial(view, options);181 }, this).join('');182 } else {181 options.locals = options.locals || {};183 if (options.object) {184 var name = options.as || view.split('.')[0];185 if (typeof name === 'string') {186 options.locals[name] = options.object;
126 unmodified lines