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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:29:54+00:00 2026-06-12T10:29:54+00:00

When we apply the ViewModel for the first time everything works fine, but if

  • 0

When we apply the ViewModel for the first time everything works fine, but if we reapply the mapping after the computed fields has been created, they are deleted. This code obviously works if we don’t change the key

http://jsbin.com/ojabaf/14/edit

var modelJs = {test:{
  list: [{name: 'a', val: 5, list: [ {name:'b', val: 11}, {name: 'c', val: 13}  ]}]}
};

var mapping = { 
    'list': {
        key: function(data) { return ko.utils.unwrapObservable(data.name); }
    },
  'name': {
    key: function(data) { return ko.utils.unwrapObservable(data.name); }
  }
};

var model = ko.mapping.fromJS(modelJs, mapping);

ko.utils.arrayForEach(model.test.list(), function(item) {
  item.comp = ko.computed({read: function() { return this.val() * 2; }  , owner: item });
  ko.utils.arrayForEach(item.list(), function(item2) {
    item2.comp = ko.computed({read: function() { return this.val() * 3; }  , owner: item2 });  
  });
});

ko.applyBindings(model);

$('button').click(function () {
  var modelJs = {test: {val: 3, list: [{name: 'b', val: 7, list: [ {name:'s', val: 15}, {name: 'c', val: 17} , {name: 'd', val: 17}, {name: 'f', val: 17}]}]}};
  ko.mapping.fromJS(modelJs, model);
});

Is there a way to recalculate those computed fields with the ko.mapping, and without refreshing the page?

I also tryed with templates…
http://jsbin.com/ojabaf/15/edit

  • 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-12T10:29:55+00:00Added an answer on June 12, 2026 at 10:29 am

    Rather than looping over your arrays and adding the comp computed observables to each item, set up your mapping so they are added when an item is created.

    var mapping = {
      'list': {
        key: function (data) {
          return ko.utils.unwrapObservable(data.name);
        },
        create: function (options) {
          var mapped = ko.mapping.fromJS(options.data, {
            'list': {
              key: function (data) {
                return ko.utils.unwrapObservable(data.name);
              },
              create: function (options) {
                var mapped = ko.mapping.fromJS(options.data);
                return ko.utils.extend(mapped, {
                  comp: ko.computed(function () {
                    return this.val() * 3;
                  }, mapped)
                });
              }
            }
          });
          return ko.utils.extend(mapped, {
            comp: ko.computed(function () {
              return this.val() * 2;
            }, mapped)
          });
        }
      },
      'name': {
        key: function (data) {
          return ko.utils.unwrapObservable(data.name);
        }
      }
    };
    var model = ko.mapping.fromJS(modelJs, mapping);
    

    http://jsbin.com/asomiq/1/edit

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

Sidebar

Related Questions

I hope that this question has not been asked elsewhere, but I'm having difficulty
I want to apply texture to the transparent png(bitmap), but I don't want to
I need to apply tint effect on image in Android . But the tint
I wanted to apply style only to the myPage id. But I am not
I'm trying to find a way to apply a css class to the first
I am creating a customized listview header that has the header text but also
I have been making some hamfisted attempts to apply correct OOP principles to my
I apply sepia effect on image but when I click on button to apply
NARROWED DOWN SOLUTION I'm much closer, but don't know how to apply XAML to
I have a ViewModel that has a List of objects that have the same

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.