vendredi 20 février 2015

Handlebars / Assemble.io - Handing return data from helper

I have a handlebars helper I want to parse fixture data and then iterate through that data with your standard handlebars {{#each}} helper. Mind you, I'm using Assemble.io, so that may impact things.


Heres my helper:



Handlebars.registerHelper('parseFixture', function(path, options) {
if (!path || typeof path !== 'string') { return false; }

var fs = require('fs');
var nodePath = require('path');

path = nodePath.join(__dirname, '../fixtures/' + path);

fs.readFile(path, function(err, data) {
if (err) {
console.error('ERROR: ', err);
return err;
}

data = data.toString('utf8')
data = JSON.parse(data);

return data;
})
});


And In my template:



<nav>

<ul>
{{#parseFixture 'header.json'}}
{{nav}}
{{#each nav-bar.nav-links}}
<li><a href="javascript:">{{this.topLevel}}</a></li>
{{/each}}
{{/parseFixture}}
</ul>

</nav>


And the fixture data I'm using:



{
"nav": [
{
"topLevel": "Things to Do"
}, {
"topLevel": "Places to Stay"
}, {
"topLevel": "Where to Eat"
},{
"topLevel": "Events"
}, {
"topLevel": "Deals"
}, {
"topLevel": "Plan Your Trip"
}
]
}


So, I'm retrieving the fixture data properly, and it parses fine. If I log it out right above the return statement everything is grand. But even trying to output {{nav}} in the template does nothing.


Another weird thing thats probably either something with my grunt server or assemble.io: I only see the logged output when I completely shutdown and restart the server.


Any ideas on what I'm doing wrong?


Edit: I think the problem is that I'm using readFile() and not readFileSync(). I changed it however and I get:



Warning: Bad arguments



It doesnt give me anything more descriptive, but I can follow added console.log()s right up to the readFileSync().


Does readFileSync() screw up grunt server or assemble?


Aucun commentaire:

Enregistrer un commentaire