简体   繁体   中英

Issues with Knockout computed

I have a computed observable:

passengerDocs.passengerDocsViewModel = function () {
var self = this;
self.isFunctionsDone = ko.observableArray([false, false, false, false, false, false, false]);
self.IsCompleted = ko.computed(function () {
    var isFinished = true;
    ko.utils.arrayForEach(self.isFunctionsDone(), function (x) {
        if (x == false) isFinished = false;
    });

    return isFinished;
});

problem is that it always returns false even if all the elelments of isFunctionDone are true. Any ideas?

ko.utils.arrayForEach not all time correctly interacts with ko.computed, it will be better if it will be replaced with simple for. Also, probably, you have issue in if statement, because it only checks last value in array.

function Model() {
    var self = this;
    self.isFunctionsDone = ko.observableArray([false, false, false, false, false, false, true]);

    self.IsCompleted = ko.computed(function () {
        for(var i = 0; i <= self.isFunctionsDone().length; i++)
             if (self.isFunctionsDone()[i] === false) 
                return false;
        return true;
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM