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 7761679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:11:57+00:00 2026-06-01T14:11:57+00:00

For some reason I cannot seem to get a ko.computed observable to compute when

  • 0

For some reason I cannot seem to get a ko.computed observable to compute when the associated value changes.

This is an object which represents a single reward points record for a customer

//
// RewardPoints Model
var RewardPointsItemModel = function (data) {
    return {
        DateAwarded: ko.observable((data != null) ? data.DateAwarded : ""),
        Points: ko.observable((data != null) ? data.Points : 0)
    }
};

I define a single viewModel which houses a reference to an “OrderDetailModel”. This OrderDetailModel has a single property defined as a ko.observable and is initialized with an instance of a new, empty OrderDetailModel; initialized with an empty array, and applied to the approprite section of HTML dentified by the “$OrderDetailForm” reference.

$j(function () {
    //
    // initialize global DOM references
    $LoyaltyProgram = $j("#LoyaltyProgram");
    $LoyaltyHistory = $j("#LoyaltyHistory");
    $OrderDetailForm = $j("#OrderDetailForm");
    //
    // define and bind the viewModels
    viewModel = {
        OrderDetailModel: {
            OrderDetail: ko.observable( new OrderDetailModel([]) )
        }
    };
    ko.applyBindings(viewModel.LoyaltyHistoryOrderDetailModel, $OrderDetailForm[0]);

});

Here is the definition of the “OrderDetailModel” object. It takes an array of RewardPointsItemModel object instances (defined above) as the initialization parameter “data”. Ultimately what this does is maintains an array of RewardPointsItemModels and computes the sum of the Points property of each of the RewardPointsItemModels. When the page is first loaded an instance of this is created with an empty array (see above) resulting in a computed “TotalPoints” of 0 (zero).

//
// OrderDetail Model
function OrderDetailModel(data) {
    var self    = this;
    this.RewardPoints = ko.observableArray(data);
    this.TotalPoints = ko.computed(function () {
        var points = 0;

        if (self.RewardPoints.length <= 0)
            return points;

        for (var i = 0; i < self.RewardPoints().length; i++) {
            var record = self.RewardPoints()[i];
            points += record.Points();
        }
        return points;
    });
};

User interaction executes a call to a WebAPI service that returns a list of reward points records, then passes that data to the callback function below as the “data” argument. In the callback function, I create a new, empty array, then populate it with instances of the RewardPointsItemModel objects created from the records retreived from the WebAPI call. I then replace the empty array value in the ko.observableArray property “RewardPoints” in the existing instance of the OrderDetailModel which is stored in the ko.observable property … viewModel.OrderDetailModel.OrderDetail … with my new rewardPoints array.

function Callback(data) {

    var rewardPoints = new Array();
    for (var i = 0; i < data.ServiceModel.length; i++) {
        var model = new RewardPointsItemModel(data.ServiceModel[i]);
        rewardPoints.push(model);
    }
    viewModel.OrderDetailModel.OrderDetail().RewardPoints(rewardPoints);
};

At this point I expect the ko.computed observable property “viewModel.OrderDetailModel.OrderDetail().TotalPoints()” to recompute, but when I put tracking code in it in the computed function it doesn’t even get called which indicates that the function is not executing to update the value. It is my understanding that if the dependency property, in this case RewardPoints, were updated (from empty Array to populated array) that the computed property should re-evaluate.

I know that this has been complicated for me to explain so I hope that I have been clear. I am hoping there is someone more fluent in Knockout than I am that can understand this clearly and find out why the computed values are not computing after initialization.

  • 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-01T14:11:58+00:00Added an answer on June 1, 2026 at 2:11 pm

    It looks like your problem is here:

    if (self.RewardPoints.length <= 0)
            return points;
    

    You need to do:

    if (self.RewardPoints().length <= 0)
            return points;
    

    self.RewardPoints is an observableArray, which is a function. The length of a function is the number of arguments defined for the function. An observableArray does not list any named args, as it reads it from arguments, so the value is 0.

    So, your current code is always returning 0 and not actually accessing the value of the observableArray to create a dependency.

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

Sidebar

Related Questions

This is kind of embarrassing but I cannot for some reason get his CSS
For some reason I cannot seem to get IE8 to display my jqgrid right
I cannot seem to get css transitions to work for some reason. I am
For some reason, no matter how I go about it, I cannot get TortoiseSVN
I'm trying to get video thumbnails from Vimeo, but for some reason, I cannot
I'm developing http://pizzli.com/darrenwp as a WordPress Theme. For some reason, I cannot get the
For some reasons I cannot seem to populate a datagrid I get the following
I cannot figure this one out, but I cannot seem to get android to
For some reason, I cannot get a UIWebView to play nice with my new
For some reason, I can not seem to change the default position of an

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.