Skip to content

Instantly share code, notes, and snippets.

@dolphin278
Last active December 27, 2015 08:19
Show Gist options
  • Save dolphin278/7295153 to your computer and use it in GitHub Desktop.
Save dolphin278/7295153 to your computer and use it in GitHub Desktop.
Использование hyperspace и hyperstream для генерации html.
var es = require('event-stream');
var fs = require('fs');
var hyperspace = require('hyperspace');
var hyperstream = require('hyperstream');
var itemTemplate = fs.readFileSync('./views/item.html');
/*
<div class="item">
<div class="title">Заголовок</div>
<div class="description">Текст записи</div>
</div>
*/
function handler(req, res) {
es.pipeline(
fs.createReadStream('./views/index.html'),
/*
<!DOCTYPE html>
<html>
<body>
<div class="items"></div>
</body>
</html>
*/
hyperstream({
'div.items': es.pipeline(
dbAdapter.getItems(), // Какой-нибудь потоковый источник из базы
hyperspace(itemTemplate, function (row) {
return {
'.item': {
'data-id': row.id, // Это будет атрибут
},
'.title': row.title,
'.description': row.description
};
})
)
}),
res
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment