How to display Nested Attributes in Underscore Java script Templates
I have a backbone model nutrition item which holds quantity, percent and
unit of measurement. for example
var nut = Backbone.Model.extend({
defaults: {
quantity: '0',
percent: 0,
uom: 'g',
}
});
And i have another model which contains the whole nutrition of product
var nutrition = Backbone.Model.extend({
defaults:{
servingSize: new Mywld.Model.Nutrient(),
calories: new Mywld.Model.Nutrient(),
caloriesFromFat: new Mywld.Model.Nutrient(),
totalFat: new Mywld.Model.Nutrient(),
saturatedFat: new Mywld.Model.Nutrient(),
cholesterol: new Mywld.Model.Nutrient(),
sodium: new Mywld.Model.Nutrient(),
totalCarbohydrate: new Mywld.Model.Nutrient(),
dietaryFiber: new Mywld.Model.Nutrient(),
sugars: new Mywld.Model.Nutrient(),
protein: new Mywld.Model.Nutrient(),
vitaminA: new Mywld.Model.Nutrient(),
vitaminC: new Mywld.Model.Nutrient(),
calcium: new Mywld.Model.Nutrient(),
iron: new Mywld.Model.Nutrient(),
},
});
But when am printing the nutrition by passing nutrition model to the
underscore template am seeing undefined. Is it underscore wont support
nested attributes ?
<tr>
<td><strong>Total Fat <%=totalFat.quantity%> <%=totalFat.uom%></strong></td>
<td class="dv"><%=totalFat.percent+'%'%></td>
</tr>
<tr>
<td class="sub">Saturated Fat <%=saturatedFat.quantity%>
<%=saturatedFat.uom%></td>
<td class="dv"><%=saturatedFat.percent+'%'%></td>
</tr>
Thanks.
No comments:
Post a Comment