I've recently noticed a couple concerning things when using the KO mapping plugin to update data on a page. The first I believe is now fixed in 2.1.1 the second shown below still exists:
I have a simple model. The problem is with an array of addresses it contains. It seems when I use the mapping plugin it tracks 2 elements in the array when there is actually only ever one. I'm not certain if this a problem w/ my code or the mapping plugin. Please consider the following simple example:
//Retrieved thru AJAX
var serverData = { name: "Bob", Addresses: [{ AddressLine: "", City: "", PostalCode: "", StateID: 10}] };
load(serverData);
//Seems OK at this point
//this.vm.__ko_mapping__.mappedProperties shows properties for Addresses[0] & name which makes sense
//Now some time goes by and we want to update the bound VM w/ new data from the server
load(serverData);
//Problem!
//this.vm.__ko_mapping__.mappedProperties shows properties for Addresses[0] & Addresses[1]
//But there is no Addresses[1]!!
//Lets simulate an update of data (1 more time)
load(serverData);
//Interestingly it doesn't get any worse, still just Addresses[0] & Addresses[1]
function load(d)
{
if (this.vm) //Refresh existing VM
{
ko.mapping.fromJS(serverData, vm);
}
else //On 1st Load have mapping create the VM and bind it
{
this.vm = ko.mapping.fromJS(serverData); //Mapping creates object from server data
ko.applyBindings(this.vm, $("body")[0]);
}
}