knockoutjs没有更新

时间:2012-12-20 21:17:33

标签: knockout.js

我必须在这个例子中遗漏了一些东西。谁能让我知道可能出现什么问题?

链接:http://jsfiddle.net/EBxy4/42/

屏幕:shared screen

更新第1行第1行中的值时,下角的值不会改变。

代码是:

function revenueStream(w1, w2, w3) {
    var self = this;
    self.week1Amount = ko.observable(w1);
    self.week2Amount = ko.observable(w2);
    self.week3Amount = ko.observable(w3);
}

function revenueStreamCategory(revenueStreams) {
    var self = this;
    self.revenueStreams = ko.observalrevenueStreams;

    self.week1Sum = ko.computed(function () {
        var sum = 0.0;

        ko.utils.arrayForEach(self.revenueStreams, function(item) {
            sum += item.week1Amount();
        });      
        return sum;
    });    
}

var rs = [];
rs.push(new revenueStream(10, 11, 12));
rs.push(new revenueStream(13, 14, 15));    
rs.push(new revenueStream(16, 17, 18));

var vm = new revenueStreamCategory(rs);      

ko.applyBindings(vm, document.getElementById("test"));​

1 个答案:

答案 0 :(得分:1)

您的问题在于您的绑定,请将其替换为:

<td><input data-bind="value: week1Amount()" /></td>

使用:

<td><input data-bind="value: week1Amount" /></td>

这是一个工作小提琴:

http://jsfiddle.net/EBxy4/44/

我甚至修改了连接而不是求和的小计!