First some backstory. The question is at the end, if you want to jump there.
I'm loading a JSON array that represents records in a database from a REST endpoint. When a record's field has no value, it's not included as a property of that object in the JSON array.
Now, when I load that JSON string into the mapping plugin to create my view model, the mapping plugin crashes, citing "Uncaught Error: Unable to parse bindings." for the attribute that wasn't included.
Here's the table cell to which this property is bound.
<td class="cityField" data-bind="text: City"></td>
Here's the mock data I'm using:
var testData = [{Id:"a", FirstName:"Me", LastName:"LastName", City:"Chicago", State:"IL", Company:"MyCo"}, {Id:"b", FirstName:"You", LastName:"LastName"}];
self.leadsFromServer = ko.mapping.fromJS(testData, self.mapping, self.personCache);
The first record seems to load fine, but it chokes on the second one. I'm guessing that as it loads this data, it is trying to update the UI. In the table loop, it loops over each object in the array and grabs the property (City, in this case). Because the 'City' property isn't on that object, an exception is thrown. Does this sound right? It seems like Knockout should just fill it with an 'error' value and continue, instead of crashing everything. Am I mistaken?
My question: What's the normal way of dealing with this non-normalized data? Can Knockout handle incomplete JSON like this with a plugin? Or is this something the dev must handle before passing it to the mapping plugin?