mod*_*tos 5 templates stream node.js handlebars.js
For example, I am using npm's accountdown module which has a list() method to list all accounts. list() returns a readable object stream, so my current solution is to create a list from that stream and pass it into my template.
Is there a way for Handlebars to read a stream directly, and avoid having to read the stream into memory as a list, so it can be read from my Handlebars template?
Here is an example of my situation:
Here I am constructing a list results from my account list stream stream so I can pass it to my template account-list-template:
var results = [];
var stream = server.accounts.list();
stream
.on('data', function (data) {
results.push(data);
})
.on('error', function (err) {
return console.log(err);
})
.on('end', function () {
var ctx = {accounts: results};
return response().html(server.render('account-list-template', ctx)).pipe(res);
});
Run Code Online (Sandbox Code Playgroud)
I tried using the handlebars-stream module, but I could not pipe my list object stream to my template. I was only able to pipe my template to the object, shown here:
return server.accounts.list().pipe(handlebars(server.render('account-list-template'))).pipe(res); // produces x times the template
Run Code Online (Sandbox Code Playgroud)
but the results are incorrect in that the entire template is rendered for each account in the stream, rendering x pages for x number of accounts.
Here is my template:
{{#extend "layout"}}
{{#replace "body"}}
<div class="container">
<hr>
<div class="sheet-list">
<h1>Master account list:</h1>
{{#each accounts}}
<div class="list-item clearfix">
<a href="/account/edit/{{ key }}">
<div class="list-item-info">
{{ key }}
{{ value.email }}
<h3 class="list-item-name">{{ key }}</h3>
<div class="list-item-description">{{ value.email }}</div>
</div>
<div class="list-item-actions">
<form action="/account/delete/{{ key }}" method="post">
<button type="submit" class="destroy-sheet">
<i class="fa fa-trash-o"></i> destroy
</button>
</form>
</div>
</a>
</div>
{{/each }}
</div>
</div>
{{/replace}}
{{/extend}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
456 次 |
| 最近记录: |