Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:33:38+00:00 2026-06-17T12:33:38+00:00

I’m trying to implement a HasOpenIssues computed observable that binds to a UI element

  • 0

I’m trying to implement a HasOpenIssues computed observable that binds to a UI element that updates if any member of a nested observableArray meets a condition, and can’t get it to work. I’m using KO 2.2.0.

My viewmodel has an observableArray of Visits; each Visit has an observableArray of Issues; each Issues array can contain instances of Issue, which has an observable IsFixed property. I also have a computed observable LatestVisit, which returns the last Visit in the Visits array:

function myVM( initialData ) {
    var self = this;

    var Issue = function( id, isFixed, description ) {
        var self = this;
        self.Id             = id;
        self.IsFixed        = ko.observable( isFixed );
        self.Description    = ko.observable( description );
    };
    var Visit = function( id, visitDate, issues ) {
        var self = this;
        self.Id             = id;
        self.VisitDate      = ko.observable( visitDate );
        self.Issues         = ko.observableArray([]);

        // init the array
        issues && ko.utils.arrayForEach( issues, function( issue ) {
            self.addIssue( issue.Id, issue.Fixed, issue.Description );
        });
    }
    Visit.prototype.addIssue    = function( id, isFixed, description ) {
        this.Issues.push( new Issue( id, isFixed, description ) );
    };

    self.LatestVisit                = ko.computed( function() {
        var visits = self.Visits();
        return visits[ visits.length - 1 ];
    });
... vm continues

All these start out empty, and before I ko.applyBindings(), I get some initial data from the server and pass it in as an argument to my viewmodel, which uses it to initialize the various observables:

... continued from above...
    self.init = function() {
        // init the Visits observableArray
        ko.utils.arrayForEach( initialData.Visits, function( visit ) {
            self.Visits.push( new Visit( visit.Id, visit.InspectionDateDisplay, visit.Issues ) );
        });
        ... more initialization...
    };
    self.init();
}

function registerVM() {
    vm = new myVM( initialDataFromServer );
    ko.applyBindings( vm );
}

So, at one point in the process, I can’t observe the LatestVisit, since the Visits array hasn’t yet been populated, nor the Issues array, since its parent Visit hasn’t yet been populated. After I initialize, these structures have data, and I need to update HasOpenIssues to reflect the state of the initial data.

Then, I allow the user to add new Issues to the LatestVisit, and to mark the new or existing Issues as Fixed. So I need HasOpenIssues to react to those changes, too.

I’ve tried to add a HasOpenIssues computed as a property on the root of the viewmodel, on the Visits array, on the Visit prototype, and directly on the LatestVisit computed, and none work. It looks something like this:

    self.LatestVisit().HasOpenIssues = ko.computed( function() {
        var unfixed = ko.utils.arrayFirst( this.Issues(), function( issue ) {
            return issue.IsFixed == false;
        });
        console.log('HasUnfixedIssues:', unfixed);
        if ( unfixed ) { return true; }
    });

If I let it run before initialization, I get some variation of

Uncaught TypeError: Object [object Window] has no method 'Issues' 

or, if I add , root, { deferEvaluation: true } arguments to the computed function call, I get

Uncaught TypeError: Cannot set property 'HasUnfixedIssues' of undefined 

If I leave off the (), like self.LatestVisit.HasOpenIssues, then I get

Uncaught TypeError: Object [object Window] has no method 'Issues' 

if I don’t use the deferred option. If I add it, I don’t get an error on initialization, but nothing happens when I update the Issues later.

Any advice on how to implement this?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:33:39+00:00Added an answer on June 17, 2026 at 12:33 pm

    I would make HasOpenIssues a property of all Visits. That way you can check if any visit has open issues, not just the latest one.

    function Visit(id, visitDate, issues) {
        this.Id             = id;
        this.VisitDate      = ko.observable(visitDate);
        this.Issues         = ko.observableArray();
    
        this.HasOpenIssues  = ko.computed(function() {
            var unfixed = ko.utils.arrayFirst(this.Issues(), function (issue) {
                return !issue.IsFixed();
            });
            return unfixed !== null;
        }, this);
    
        // init the array
        this.Issues(ko.utils.arrayMap(issues, function (issue) {
            return new Issue(issue.Id, issue.Fixed, issue.Description);
        }));
    }
    

    This way, even if there are no issues, you’re not attempting to add a property to an undefined value.

    contrived demo 1


    If you only cared about the latest visit, then attaching the HasOpenIssues property to the LatestVisit is a good place to put it. You just gotta do it right. Check if you have a LatestVisit first, then return the appropriate value, otherwise some default value (false in this case).

    this.LatestVisit.HasOpenIssues  = ko.computed(function() {
        var visit = this.LatestVisit();
        if (!visit) {
            return false;
        }
        var unfixed = ko.utils.arrayFirst(visit.Issues(), function (issue) {
            return !issue.IsFixed();
        });
        return unfixed !== null;
    }, this);
    

    contrived demo 2

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.