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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:02:10+00:00 2026-06-15T14:02:10+00:00

In my KnockoutJS view model logic I have an observable array of currently displayed

  • 0

In my KnockoutJS view model logic I have an observable array of currently displayed objects (project view models): self.currentProjects. Depending on an item filter setting (user chooses project types dynamically) self.currentProjects holds different objects that a UI is bound to.

At some point, where I need to load different projects into the view, I need to empty the currentProjects and put different set of models into it. I do this by calling self.currentProjects.removeAll(); The problem is when I call it, another observable array self.ProjectModels that should hold the reference to the same set of models also appears to be emptied up. How should I manage objects in an observable array, so that removal from one array doesn’t cause the other to become empty.

Here is a manipulation related code:

    //Another menu item is selected
    self.selectItem = function(newId) {
        self.selectedItemId(newId);
        self.currentProjects.removeAll(); //This is the point where self.ProjectModels also looses model references
        var someProjectsLoaded = false;
        jQuery.each(self.projectModels, function (i, val) {
            if (val.type == self.selectedItemId()) {
                self.currentProjects(val.models);
                var projectsPerPage = parseInt($('#ProjectsPerPage').val(), "10");
                self.page(val.models.length / projectsPerPage);
                someProjectsLoaded = true;
            }
        });
        if (!someProjectsLoaded) {
            self.page(1);
            self.LoadMoreProjects();
        }
    };

    self.LoadMoreProjects = function() {
        var getProjectsUrl = $("#GetNextProjectsUrl").val();
        $.ajax({
            url: getProjectsUrl,
            data: {
                page: parseInt(self.page(), "10"),
                type: self.selectedItemId()
            },
            beforeSend: function () {
                //$("#ajaxload").show();
            },
            success: function (result) {
                var newlyAddedModelsArray = jQuery.map(result, function (val, i) {
                    var vm = new ProjectViewModel(val, self);
                    self.currentProjects.push(vm);
                    return vm;
                });
                self.page(parseInt(self.page(), "10") + 1);
                var typeAlreadyInChache = false;
                jQuery.each(self.projectModels, function (i, val) {
                    if(val.type == self.selectedItemId()) {
                        val.models = val.models.concat(newlyAddedModelsArray); //HERE IS WHERE I SUSPECT A COPY BE REFERENCE TAKES PLACE
                        typeAlreadyInChache = true;
                    }
                });
                if(!typeAlreadyInChache) {
                    self.projectModels.push({ type: self.selectedItemId(), models: newlyAddedModelsArray });
                }
            },
            error: function () {
                $("#error").show();
            }
        });
    };
  • 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-15T14:02:11+00:00Added an answer on June 15, 2026 at 2:02 pm

    Calling removeAll empties the underlying array and your two observables appear to have a reference to the same array.

    An easy choice is to set currentProjects to an empty array rather than calling removeAll.

    self.currentProjects([]);
    

    Now, the original array will remain as it was.

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

Sidebar

Related Questions

I have created a View Model object for use with KnockoutJS. It has a
I'm using KnockoutJS and have a main view and view model. I want a
I'm new to Knockoutjs. I have a observable array of Users. Here is my
I have a knockoutjs 2.1.0 view model that I have developed and works with
I have a view model that will always create a new entity (Score); it
I have a Knockout.js view model that looks like this: UserName:null, FirstName:null, LastName:null, Countries:{
How do we populate data in the knockoutjs view model on the first call
After looking at knockoutjs, it looks like you define your view and model directly
I'm using KnockoutJS and loading my view model via ajax. Before the load completes,
I have an MVC view model that looks like this: public class DirectorySearchModel {

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.