我正在使用基諾爲項目創建一個基本的AJAX購物車,並且包含在視圖模型內的可觀察集合中的單個產品的成本需要更新屬性在視圖模型上更新。我一直在嘗試各種解決方案很多小時,我希望有人能指出我正確的方向。我已經包含了一個jsfiddle。從視圖模型的屬性敲除更新可觀察集合
var product = function (title, operationName, description, parent) {
this.title = title;
this.operationName = operationName;
this.description = description;
this.cost = 9;
this.count = ko.observable(parent.recordCount);
this.subtotal = ko.computed(function() {
return this.count * this.cost;
}).bind(this);
};
order = function() {
var listName = ko.observable("not defined"),
listId = ko.observable("not defined"),
recordCount = ko.observable("not defined"),
products = [
new product('Product1', 'EMAIL_VER_DELIVERABLE', 'Description.', this),
new product('Product2', 'EMAIL_BASIC_NO_SUPRESSION_W_VERIFICATION', 'Description.', this),
new product('Product3', 'PHONE_PREM', 'Description.', this)],
total = function() {
var total = 0;
$.each(this.products(), function() {
total += this.subtotal();
});
return total;
};
// anything in the return block is considered public, anything above is private
return {
listName: listName,
listId: listId,
recordCount: recordCount,
products: products,
total: total
};
}();
ko.applyBindings(order);
// when this value changes the product Cost needs to be updated
order.listName('test list')
order.listId(1)
order.recordCount(100)
謝謝, 克里斯