我最近注意到使用KO映射插件更新頁面上的數據時,將來的事,一對夫婦。首先我認爲是現在固定在2.1.1下面所示的第二個仍然存在:Knockout貼圖插件或我的代碼中的數組錯誤?
我有一個簡單的模型。問題在於它包含的地址數組。看起來,當我使用映射插件時,它實際上只有一個跟蹤數組中的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]);
}
}
你能複製你這個小提琴的問題?我從來沒有看到多個地址。 http://jsfiddle.net/jearles/xTHFg/2/ – 2012-04-17 00:12:49
好吧......我明白你的意思。雖然沒有第二個地址出現在視圖模型中,但我確實在mappedProperties中看到了兩個條目。 – 2012-04-17 00:37:35
是的,正好。在2.1.0版本中,相同的代碼在每次更新時還會繼續增加include []並增加「_destroy」條目。這似乎現在已經被修復了,但由於我的場景中更新的頻率,我對內存泄漏感到緊張。 – 2012-04-17 09:11:10