【发布时间】:2015-11-30 15:13:11
【问题描述】:
这是一个模型:
function DiscountItem(minDays, percent) {
var self = this;
self.minDays = ko.observable(minDays);
self.discount = ko.observable(percent);
}
function TableItem(name, from, to, price, hasDiscount) {
var self = this;
self.name = name;
self.from = from;
self.to = to;
self.price = price;
self.discount = hasDiscount;
}
function AppViewModel() {
var self = this;
self.discounts = ko.observableArray([
new DiscountItem(12, 4),
new DiscountItem(20, 6)
]);
self.table = ko.observableArray([
new TableItem("first", "20.03","3.04", 400, 1),
new TableItem("second", "11.04","4.05", 600, 0)
]);
self.addDiscount = function() {
self.discounts.push(new DiscountItem(0, 0));
}
self.removeDiscount = function() {
self.discounts.remove(this);
}
self.addPeriod = function() {
self.table.push(new TableItem("","","", 0, 1));
}
self.removePeriod = function() {
self.table.remove(this);
}
self.pricing = ko.computed(function() {
return JSON.stringify({
durationDiscount : self.discounts(),
periods: self.table()
})
}, self);
}
每次我更改可观察属性之一或在 observableArray 中添加新项目时,我都需要使用来自 self.discounts 和 self.table 的数据的 JSON 表示填充其他输入
类似的东西
{"durationDiscount":[{"minDays":12,"discount":4},{"minDays":20,"discount":6}],"periods":[{"name":"first","from":"20.03","to":"3.04","price":400,"discount":1},{"name":"second","from":"11.04","to":"4.05","price":600,"discount":0}]}
如果我删除模型属性上的 ko.observable() 一切正常,但我需要跟踪它们。
如果我添加 ko.observable(),ko.computed 中的 self.discounts 会返回空数组
【问题讨论】:
-
这是一个很好的问题,但很难理解这个问题。您能否使用jsfiddle.net 或此处的代码 sn-p 重现该问题。
-
当然可以。我创建了一个简化版本,以便更好地理解问题jsfiddle.net/cbqenwy9 每当我更改值或添加新行时,我都需要更新 textarea 中的 JSON 数据
标签: knockout.js