Hi,
I started playing around with Marko these past days and while I still struggle with some of the concepts I think it's pretty cool and quite easy to use.
One of the concept where I'm a bit stuck is the following.
I'm trying to render a stream of questions within the main template (index.marko). I managed to quite abstract this and make the template be flexible regarding the amount of questions it will eventually display (I don't know that number beforehand and I don't want to know it -> no hardcoding items when delivered to the client).
So I got my stream open and that works well, but everytime I do a flush when I have a satisfying amount of questions ready to be shown (aka at least 1), it does render the whole template again, meaning I end up with this HTML:
<head>
</head>
<body>
<h1>test Async Rendering</h1>
<title>marko-express</title>
<link media="all" type="text/css" rel="stylesheet" href="/static/reset.css">
<link media="all" type="text/css" rel="stylesheet" href="/static/style.css">
<script type="text/javascript">
<h1>test Async Rendering</h1>
<title>marko-express</title>
<link media="all" type="text/css" rel="stylesheet" href="/static/reset.css">
<link media="all" type="text/css" rel="stylesheet" href="/static/style.css">
<script type="text/javascript">
<h1>test Async Rendering</h1>
<div>
</body>
How do I end up with something like this is where I think I went in the wrong direction for the abstraction:
I use the default index.marko provided by the repo so here is the internal of body:
and here is the relevant part of the logic:
function renderOnlyQuestions(questions, res) {
return questionTemplater.render({
provider: function(args, callback) {
callback(null, questions);
}
}, res);
}
function flushRender(questions, res) {
indexTemplate.render({}, res);
renderOnlyQuestions(questions, res);
}
function deferAnswer(questionTemplates, asyncOut, defer){
setTimeout(
function() {
flushRender(questionTemplates, asyncOut);
}, (defer ? Math.floor(Math.random() * 5000 + 1000) : 0)
);
}
app.use('/static', serveStatic(__dirname + '/static'));
app.get('/', function(req, res) {
var form = {};
var questionTemplates = [];
var out = require('async-writer').create(res);
var asyncOut = out.beginAsync();
for (var i = 0; i < 100; i++) {
form = getForm(i);
if (form) {
questionTemplates = prepareTemplates(form);
console.log(questionTemplates);
deferAnswer(questionTemplates, asyncOut, true);
} else {
console.log('no more questions to fetch');
break;
}
}
setTimeout(function(){
asyncOut.end();
out.end();
}, 8000);
});
Thanks for your help, please let me know if the issue is not clear, I'm deep into it so I may not express the simpler concerns. Also I'm not sure if I'm the right group, sorry if I'm not but I couldn't find other sources for help with Marko