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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:06:00+00:00 2026-06-03T20:06:00+00:00

I have a ko.computed() function that proxies a collection like this: function ListView(query) {

  • 0

I have a ko.computed() function that proxies a collection like this:

function ListView(query) {
    var self = this;

    this.query = query;
    this.items = ko.observableArray([]);
    ko.computed( function() {
        self.items( self.query() == null ? [] : self.query().postings() );
    }

    // more code
}

Unfortunately, this causes the self.items() variable to change with every change to every object in the postings() array. What I would rather do is have a computed value items represent the query’s postings directly. But if I do this:

 self.items = ko.computed(function() { return self.query().postings(); });

the value becomes a scalar, not an array, as far as Knockout is concerned. The reasons this matters is that as some of the items in the list change state, I would like to update just those parts of the display, rather than re-rendering the entire list. This is not just a performance optimization (although with hundreds of items performance is also an issue), but also I am trying to log which items are currently being displayed to keep track of what the user might have seen.

I suppose logically, I would like to have something like this:

  self.items = self.query().postings;

but this breaks if the query changes because (I think) the dependencies exist on a different object.

Is there a clean way to build a dependency mechanism and still preserve collection semantics?

EDITED: 13 May 2012

I tried to implement the extension approach suggested by @MichaelBest, but that produced the following error:

Uncaught TypeError: Object function dependentObservable() {
        if (arguments.length > 0) {
            set.apply(dependentObservable, arguments);
        } else {
            return get();
        }
    } has no method 'valueWillMutate'
ko.utils.arrayForEach.ko.observableArray.fn.(anonymous function)    knockout-2.1.0.debug.js:1087
SubviewModel.self.refresh.self.doSort    SummaryViewModel.js:73
ko.ignoreDependencies    knockout-deferred-updates.js:172
subFnObj.(anonymous function).newCallback    knockout-deferred-updates.js:188
ko.subscribable.fn.notifySubscribers    knockout-2.1.0.debug.js:870
ko.utils.arrayForEach    knockout-2.1.0.debug.js:85
ko.subscribable.fn.notifySubscribers    knockout-2.1.0.debug.js:866
evaluateImmediate    knockout-deferred-updates.js:300
evaluatePossiblyAsync    knockout-deferred-updates.js:246
ko.subscribable.fn.notifySubscribers    knockout-2.1.0.debug.js:870
ko.utils.arrayForEach    knockout-2.1.0.debug.js:85
ko.subscribable.fn.notifySubscribers    knockout-2.1.0.debug.js:866
ko.observable.observable.valueHasMutated    knockout-2.1.0.debug.js:946
observable    knockout-2.1.0.debug.js:934
updateViewModel    knockout.mapping-latest.debug.js:514
changes    knockout.mapping-latest.debug.js:389
visitPropertiesOrArrayEntries    knockout.mapping-latest.debug.js:569
updateViewModel    knockout.mapping-latest.debug.js:374
ko.mapping.fromJS    knockout.mapping-latest.debug.js:91
Topic.load    querium.js:1212
...
  • 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-03T20:06:01+00:00Added an answer on June 3, 2026 at 8:06 pm

    You can create a computed observable that returns an array and acts like an observableArray:

    self.items = ko.computed({
        read: function() { return self.query().postings(); }
        write: function(value) { self.query().postings(value); }
    });
    ko.utils.extend(self.items, ko.observableArray.fn);
    

    Now you can use all the functions that an observableArray has with items.

    Edit: This approach will only work if you also add valueWillMutate and valueHasMutated functions to the object:

    self.items.valueWillMutate = function () { self.query().postings.valueWillMutate(); }
    self.items.valueHasMutated = function () { self.query().postings.valueHasMutated(); }
    

    And this will also only work if you use the debug version of Knockout, because in the minified version, the function names will be compressed.

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

Sidebar

Related Questions

I have a Collection of items that I would like to display the Total
I have a table like this with a computed column: CREATE TABLE PhoneNumbers (
I have something like this create function Answers_Index(@id int, @questionID int) returns int as
I have a DOM situation that looks like this: A is an ancestor of
I have a computed function that notifies a button if it should be disabled
I have an Access form where each record has some info that is computed
I have a user defined function (e.g. myUDF(a,b) ) that returns an integer. I
Based on this original idea, that many of you have probably seen before: http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/
I have anchor tag like this <a href=# class=btn btn-success order-btn data-bind=attr:{'data-tiername':$data.tierName, 'data-identifier' :
I have a table TableA with a computed column FieldA bound to a function

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.