我描述成我的Javascript代碼有一個數據對象如下:KnockoutJS從AJAX調用綁定對象的列表
//Data Object that represents products
function Product(n, p, t, d) {
this.name = ko.observable(n);
this.price = ko.observable(p);
tags = typeof (t) !== 'undefined' ? t : [];
this.tags = ko.observableArray(tags);
discount = typeof (d) !== 'undefined' ? d : 0;
this.discount = ko.observable(discount);
this.formattedDiscount = ko.computed(function()
{ return (this.discount() * 100) + "%"; }
,this);
}
然後,我有一個AJAX調用JSON格式檢索數據
$(document).ready(function() {
$.ajax({
type: "POST",
url: "ShoppingCartExampleExample.aspx/SendData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
}
});
});
AJAX調用工作,我得到這個結果爲:
[
{"Discount":0,
"Name":"Chocolate",
"Price":"7.99"
"tags": ["Mars","Snickers"]
},
{"Discount":0.05,
"Name":"Beer",
"Price":"3.99"
"tags": ["Large","Extra"]
}
]
我怎麼能映射對象名單從AJAX調用收到我的數據對象? 我想將這個列表映射到一個可觀察數組,並將標籤數組映射到一個可觀察數組,因爲我有一個foreach綁定來填充一個HTML表來呈現產品和標籤。
謝謝