mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 12:19:51 +01:00
Added support for partial(view, array) as collection
This commit is contained in:
parent
8ece01ccda
commit
22100f7c3a
5
.pomo
5
.pomo
|
|
@ -74,3 +74,8 @@
|
|||
description:
|
||||
length: 25
|
||||
name: view mtime check interval
|
||||
- !ruby/object:Pomo::Task
|
||||
complete: false
|
||||
description:
|
||||
length: 25
|
||||
name: next(err) from within view system?
|
||||
|
|
|
|||
|
|
@ -46,17 +46,20 @@ exports.clearCache = function(){
|
|||
* Render `view` partial with the given `options`.
|
||||
*
|
||||
* Options:
|
||||
* - `as` String name for the id used to which "collection" assign it's current value.
|
||||
* - `as` Variable name for each `collection` value, defaults to the view name.
|
||||
* - `collection` Array of objects, the name is derived from the view name itself.
|
||||
* For example _video.html_ will have a object _video_ available to it.
|
||||
*
|
||||
* @param {String} view
|
||||
* @param {Object} options
|
||||
* @param {Object|Array} options or collection
|
||||
* @return {String}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
http.ServerResponse.prototype.partial = function(view, options){
|
||||
if (options instanceof Array) {
|
||||
options = { collection: options };
|
||||
}
|
||||
options = options || {};
|
||||
options.partial = true;
|
||||
options.layout = false;
|
||||
|
|
|
|||
1
test/fixtures/movies.jade
vendored
Normal file
1
test/fixtures/movies.jade
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
ul!= partial('movie.jade', movies)
|
||||
3
test/fixtures/partials/movie.jade
vendored
Normal file
3
test/fixtures/partials/movie.jade
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
li
|
||||
.title= movie.title
|
||||
.director= movie.director
|
||||
|
|
@ -3,7 +3,8 @@
|
|||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var express = require('express');
|
||||
var express = require('express'),
|
||||
connect = require('connect');
|
||||
|
||||
module.exports = {
|
||||
'test #render()': function(assert){
|
||||
|
|
@ -55,12 +56,43 @@ module.exports = {
|
|||
var app = express.createServer();
|
||||
app.set('views', __dirname + '/fixtures');
|
||||
|
||||
// Auto-assigned local w/ collection option
|
||||
app.get('/', function(req, res){
|
||||
res.render('items.jade', { locals: { items: ['one', 'two'] }});
|
||||
});
|
||||
|
||||
assert.response(app,
|
||||
|
||||
assert.response(app,
|
||||
{ url: '/' },
|
||||
{ body: '<html><body><ul><li>one</li><li>two</li></ul></body></html>' });
|
||||
|
||||
// Auto-assigned local w/ collection array
|
||||
var movies = [
|
||||
{ title: 'Nightmare Before Christmas', director: 'Tim Burton' },
|
||||
{ title: 'Avatar', director: 'James Cameron' }
|
||||
];
|
||||
app.get('/movies', function(req, res){
|
||||
res.render('movies.jade', { locals: { movies: movies }});
|
||||
});
|
||||
|
||||
var html = [
|
||||
'<html>',
|
||||
'<body>',
|
||||
'<ul>',
|
||||
'<li>',
|
||||
'<div class="title">Nightmare Before Christmas</div>',
|
||||
'<div class="director">Tim Burton</div>',
|
||||
'</li>',
|
||||
'<li>',
|
||||
'<div class="title">Avatar</div>',
|
||||
'<div class="director">James Cameron</div>',
|
||||
'</li>',
|
||||
'</ul>',
|
||||
'</body>',
|
||||
'</html>'
|
||||
].join('');
|
||||
|
||||
assert.response(app,
|
||||
{ url: '/movies' },
|
||||
{ body: html });
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user