The JSON returned from my server has optional attributes (e.g.) CompanyNumber is sometimes NULL and therefore it's removed from the JSON response for that specific company. (JMSSerializeBundle).
If I bind it to render all the companies in a table it crashes as soon as there's a company without a CompanyNumber. Is there way to prevent this from happening?
Current code:
<script type="text/javascript">
$(document).ready(function() {
$.getJSON(Routing.generate('contacts_companies_get_json'), function(data) {
var companies = ko.mapping.fromJS(data);
ko.applyBindings({
'companies': companies
});
})
});
</script>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Company Number</th>
<th>Account</th>
<th>Supplier</th>
<th>Competitor</th>
<th>Other</th>
<th></th>
</tr>
</thead>
<tbody data-bind="foreach: companies">
<tr>
<td><a href="#" class="title"><span data-bind="text: name"></span> <span data-bind="text: legal_form"></span></a></td>
<td><span data-bind="text: company_number"></span></td>
<td><span data-bind="if: type_account" ><i class="icon-check"></i></span></td>
<td><span data-bind="if: type_supplier" ><i data-bind="if: type_supplier" class="icon-check"></i></span></td>
<td><span data-bind="if: type_competitor" ><i data-bind="if: type_competitor" class="icon-check"></i></span></td>
<td><span data-bind="if: type_other" ><i data-bind="if: type_other" class="icon-check"></i></span></td>
<td><a class="btn btn-mini">Details</a><td>
</tr>
</tbody>
</table>