最近、KO マッピング プラグインを使用してページのデータを更新する際に気になる点がいくつかあります。最初のものは 2.1.1 で修正されたと思いますが、以下に示す 2 番目のものはまだ存在します。
私は単純なモデルを持っています。問題は、含まれているアドレスの配列にあります。マッピングプラグインを使用すると、実際には1つしかないのに、配列内の2つの要素を追跡しているようです。これが私のコードの問題なのか、マッピング プラグインの問題なのかはわかりません。次の簡単な例を考えてみてください。
//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]);
}
}