从我的服务器返回的 JSON 具有可选属性(例如)CompanyNumber 有时为 NULL,因此它已从该特定公司的 JSON 响应中删除。(JMSSerializeBundle)。
如果我绑定它以呈现表中的所有公司,一旦有公司没有 CompanyNumber,它就会崩溃。有没有办法防止这种情况发生?
当前代码:
<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>