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

  • SEARCH
  • Home
  • 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 7840651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:51:35+00:00 2026-06-02T15:51:35+00:00

I have an array of items coming back from the service. I’m trying to

  • 0

I have an array of items coming back from the service. I’m trying to define a computed observable for every Item instance, so my instinct tells me to put it on the prototype.

One case for the computed observable: the system calculates points, but the user can choose to override the calculated value. I need to keep the calculated value available in case the user removes the override. I also need to coalesce user-assigned and calculated points, and add up the totals.

I’m using mapping to do the following:

var itemsViewModel;
var items = [
    { 'PointsCalculated' : 5.1 },
    { 'PointsCalculated' : 2.37, 'PointsFromUser' : 3 }
];

var mapping = {
    'Items' : {
        create : function(options) {
            return new Item(options.data);
        }
    }
};

var Item = function(data) {
    var item = this;
    ko.mapping.fromJS(data, mapping, item);
};

Item.prototype.Points = function () {
    var item = this;
    return ko.computed(function () {
        // PointsFromUser may be 0, so only ignore it if the value is undefined/null
        return (item.PointsFromUser != null) ? item.PointsFromUser : item.PointsCalculated;
    });
};

ko.mapping.fromJS(items, mapping, itemsViewModel);

The way it works now, I have to call the anonymous function to return the computed observable. That appears to create a new instance of the computed observable for each binding, which defeats most of the point of putting it on the prototype. And it’s a little annoying having to decipher how many parentheses to use each time I access an observable.

It’s also somewhat fragile. If I attempt to access Points() in code, I can’t do

var points = 0;
var p = item.Points;
if (p && typeof p === 'function') {
    points += p();
}

because that changes to context of Points() to DOMWindow, instead of item.

If I put the computed in create() in the mapping, I could capture the context, but then there’s a copy of the method on each object instance.

I’ve found Michael Best’s Google Groups post (http://groups.google.com/group/knockoutjs/browse_thread/thread/8de9013fb7635b13). The prototype returns a new computed observable on “activate”. I haven’t figured out what calls “activate” (maybe Objs?), but I’m guessing it still happens once per object, and I haven’t a clue what scope ‘this’ will get.

At this point, I believe I’m past what’s available in published docs, but I’m still working up to deciphering what’s going on from the source.

  • 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-02T15:51:37+00:00Added an answer on June 2, 2026 at 3:51 pm

    You mention that you don’t want to have an instance of the ko.computed function on each instance of your javascript class, however, that won’t really work with how ko’s functionality has been built. When you use ko.computed or ko.observable they create specific memory pointers to private variables inside that you would not normally want to be shared across class instances (although in rare cases you might).

    I do something like this:

    var Base = function(){
        var extenders = [];
    
        this.extend = function(extender){
            extenders.push(extender);
        };
    
        this.init = function(){
            var self = this; // capture the class that inherits off of the 'Base' class
    
            ko.utils.arrayForEach(extenders, function(extender){
    
                 // call each extender with the correct context to ensure all
                 // inheriting classes have the same functionality added by the extender
                 extender.call( self );
            });
        };
    };
    
    var MyInheritedClass = function(){
        // whatever functionality you need
    
       this.init(); // make sure this gets called
    };
    
    // add the custom base class
    MyInheritedClass.prototype = new Base();
    

    then for the computed observables (which HAVE to be instance functions on each instance of your MyInheritedClass) I just declare them in an extender like so:

    MyInheritedClass.prototype.extend(function(){
    
         // custom functionality that i want for each class 
         this.something = ko.computed(function() {
             return 'test';
         });
    });
    

    Given your example and the Base class defined above, you could easily do:

    var Item = function(data) {
        var item = this;
    
        ko.mapping.fromJS(data, mapping, item);
    
        this.init(); // make sure this gets called
    };
    Item.prototype = new Base();
    
    Item.prototype.extend(function () {
        var self = this;
    
        this.Points = ko.computed(function () {
    
            // PointsFromUser may be 0, so only ignore it if the value is undefined/null
            return (self.PointsFromUser != null) ? 
                   self.PointsFromUser : self.PointsCalculated;
        });
    };
    

    Then all instances of your Item class will have a Points property, and it will correctly handle the ko.computed logic per instance.

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

Sidebar

Related Questions

I have this $_POST array coming from a paypal transaction Array ( [address_city] =>
I have a newbie question! I have a problem coming back from a view
I have an array Items which has 10 elements. I need to remove the
I have an array of items: array( 'apples', 'oranges', 'pineapples' ) that I would
I have an array of items that are time sensitive. After an amount of
I have an array of items that should be selected in my tree control.
I have an array of 50 items and I would like to choose 5
I have an array with lots of items with same names like CloudObserverCMSStub edited
I have an array that has 22 items in it (it may also have
I have an array which contains sets of three similar named items; however, sometimes

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.