【发布时间】:2014-03-12 23:18:36
【问题描述】:
我正在使用敲除为一个项目创建一个基本的 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)
谢谢, 克里斯
【问题讨论】:
标签: javascript mvvm knockout.js collections observable