I am building an app using node.js and am using handlebars for server and client side templating. However, I'm having trouble iterating over an object. Here's my code:
My .js file:
$(function () {
var theTemplateScript = $("#update_address_distributor_template").html();
context.city = 'london';
var theTemplate = Handlebars.compile(theTemplateScript);
var theCompiledHtml = theTemplate(context);
$('.update_address_distributor_template_placeholder').html(theCompiledHtml);
});
My html file:
<script id="update_address_distributor_template" type="text/x-handlebars-template">
<form class="form-horizontal" role="form" id="update_distributor_form">
\{{city}} //correctly prints out london
\{{address_distributors.length}} //returns 2, so i know the object exists
{{#each address_distributors}}
\{{name}} //does not print anything
{{/each}}
</form>
</script>
My context object:
{
address_distributors: {
[
{
name: 'nike',
id: '1'
},
{
name: 'yokohama',
id: '4'
}
]
},
city: 'london'
}
My {{#each ../address_distributors}} ... {{/each}}
does not print anything. I know my addres_distributors
object exits because \\{{address_distributors.length}}
prints 2 on the html page. Also, to test that my handlebars template is properly setup, I set context.city = 'london';
to see if it would print out, which it did. So I know the issue has something to do with the object...
Can someone help?
Thanks in advance!