I have a Handlebars script that I am trying to get to iterate through an object to my ‘template’ below. Even though I can see the ‘badNodes’ in the object (an object when i do a console.log(row) it doesn’t ever pass to the template and all I see is a blank entry next to address in the view/template.
Can anyone suggest what I may have done wrong – P.S I am new to Handlebars.js
<script class="template" type="text/html">
<div class="badNodesContainer">
{{#row}}
<div class="hash{{badNodes.hash}} help-tip">
<p>
<strong>Address: </strong>{{badNodes.address}}</br>
</p>
</div>
{{/row}}
</div>
</script>
// latter part of ajax request
.done(function (data, result, jqXHR) {
var checkinData = JSON.parse(data).data;
var rows = [];
//_.templateSettings.variable = "badNode";
var source = $("script.template").html();
var template = Handlebars.compile(source);
$.each(checkinData, function (index, row) {
row.badNodesHTML = "";
var AB_action_login = '<a class="btn btn-default btn-sm" style="width:70px;text-align:left;" target="_blank" href="http://asite.com/app/login.php?username=' + row.user + '&hash=' + row.signature + '"><i class="fa fa-external-link"></i> App</a>';
if (row.postid == null) {
var AB_action_order = '<a class="btn btn-default btn-sm hidden" disabled="disabled" style="width:70px;text-align:left;" target="_blank" href="http://www.mysite.co.uk/wp-admin/post.php?post=' + row.postid + '&action=edit"><i class="fa fa-external-link"></i> Order</a>';
} else {
var AB_action_order = '<a class="btn btn-default btn-sm" style="width:70px;text-align:left;" target="_blank" href="http://www.mysite.co.uk/wp-admin/post.php?post=' + row.postid + '&action=edit"><i class="fa fa-external-link"></i> Order</a>';
}
row.AB_login = AB_action_login;
row.AB_order = AB_action_order;
row.AB_actions = AB_action_login + AB_action_order;
if (row.badNodes.length) {
$.each(row.badNodes, function (idx, badNode) {
badNode.address.toString()
badNode.address = /:(.+)/.exec(badNode.address)[1];
row.badNodesHTML += template(badNode);
row.AB_actions;
});
}
}
);
You are accessing the object via
#row
inside your template even though the object you pass into the template looks like this:So, in order to bind it try to access it by key instead like this:
Also, assuming that
badNodes
is an array of bad nodes, you could useeach
in your declaration when you passrow
to the template directly. Try this:I suggest you prepare a javascript object that has the exact shape and form you would like and then pass it to the template. You are formatting the output in an
$.each
loop and pass each row individually which could get messy from an organizational perspective. Also, you are creating html inside your javascript for the button which you could put in the template as well. Then, all you have to do is pass onerow
object to the template that has all required information.A refactored version could look like this:
Note, I put the button templates in an arbitrary spot. I’m sure you would want to re-organize where they belong. This is for demonstration only.