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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:27:23+00:00 2026-06-13T13:27:23+00:00

I have an observable array of a complex object. The initial load is fine,

  • 0

I have an observable array of a complex object. The initial load is fine, and all the expected data looks fine. Now I am working on POSTing new items to that array. NOTE: The observable array is being loaded via ASP.NET ajax web api call.

posting a new item works fine as far as saving it to the database, but my DOM is not getting updated with the new item and I don’t know what I am missing.

Here is the entire ViewModel

    function ClientList() {
    //data
    var self = this;
    self.initialized = ko.observable(false);
    self.clients = ko.observableArray();
    self.userId = ko.observable("");
    self.name = ko.observable("");
    self.logo = ko.observable("");
    self.projects = ko.observableArray();
    self.clientAddress = ko.observableArray();


    self.addClient = function () {
        var client = {
            UserId: self.userId,
            Name: self.name,
            Logo: self.logo,
        }
        client = ko.toJSON(client);
        lucidServer.addClient(client);
        self.clients.push(client);
    }.bind(self);

    (function () {
        $.ajax({
            url: lucidServer.getClients(1),
            success: function (data) {
                ko.mapping.fromJS(data, {}, self.clients);
                self.initialized(true);
            }
        });
    })();
};

function IncompleteStoriesList() {
    //data
    var self = this;
    self.initialized = ko.observable(false);
    self.stories = ko.observableArray();
    (function () {
        $.ajax({
            url: lucidServer.getIncompleteStory(1),
            success: function (data) {
                ko.mapping.fromJS(data, {}, self.stories);
                self.initialized(true);
            }
        });
    })();
};


function ViewModel() {
    var self = this;
    self.clientList = new ClientList();
    self.storyList = new IncompleteStoriesList();
}

ko.applyBindings(new ViewModel());

Here is the particular snippet where I am doing the POST (within the ClientList() function).

self.addClient = function () {
        self.client = {
        UserId: self.userId(),
        Name: self.name(),
        Logo: self.logo(),
        }
    //make client object to send to server
    var client = ko.toJSON(self.client);
    lucidServer.addClient(client);
    //push the self.client to the observablearray of clients
    self.clients.push(self.client);
}.bind(self);

I verified it is JSON that is sitting inside the client variable, and no error messages are getting thrown, so I am confused. After I add an item and refresh the entire page, it will show up in the list.

EDIT: here is the html associated:

<form data-bind="submit: clientList.addClient">
    <div>
        <label>userId</label>
        <input type="text" data-bind="value: clientList.userId" />
    </div>
    <div>
        <label>name</label>
        <input type="text" data-bind="value: clientList.name" />
    </div>
    <div>
        <label>logo</label>
        <input type="text" data-bind="value: clientList.logo" />
    </div>
    <button type="submit">Add</button>

    </form>

    <!-- ko ifnot: clientList.initialized -->
    <span>Loading...</span>
    <!-- /ko -->
    <ul data-bind="template:{name: 'clientList', foreach:clientList.clients}">
    </ul>

And the external template looks like this:

<div id="clientListOutput">

    <li><span data-bind="text: name"></span>
        <div data-bind="template: {foreach: clientAddress}">
            <span data-bind="text: city"></span>
            <span data-bind="text: state"></span>
        </div>
        <hr />
        <ul data-bind="template: {foreach: projects}">
            <li>
                <span data-bind="text: name"></span>
                <span data-bind="text: summary"></span>
                <span data-bind="text: description"></span>
            </li>
        </ul>
    </li>
  • 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-13T13:27:23+00:00Added an answer on June 13, 2026 at 1:27 pm

    I am quite certian you have a typo in your HTML. Here is a working example using ko.observablearray

    HTML:

    <form data-bind="submit: addItem">
        prop1: <input data-bind='value: prop1, valueUpdate: "afterkeydown"' />
        prop2: <input data-bind='value: prop2, valueUpdate: "afterkeydown"' />
        <button type="submit">Add</button>
        <p>Your items:</p>
        <div data-bind="foreach: items">
            <span data-bind="text: prop1"></span> &nbsp - &nbsp
            <span data-bind="text: prop2"></span>
            <br />
        </div>
    </form>
    

    JS:

    var SimpleListModel = function() {
        this.items = ko.observableArray();
        this.prop1 = ko.observable("");
        this.prop2 = ko.observable("");
        this.addItem = function() {
            this.items.push({prop1:this.prop1, prop2: this.prop2});
        }.bind(this);  // Ensure that "this" is always this view model
    };
    
    ko.applyBindings(new SimpleListModel());
    

    http://jsfiddle.net/NjSBg/2/

    I suppose it’s also possible you forgot to apply the bindings…

    Edit

    I appologize for posting the wrong fiddle, right one up now.

    self.addClient = function () {
        var client = {
            UserId: self.userId(),
            Name: self.name(),
            Logo: self.logo()
        }
        lucidServer.addClient(ko.toJSON(client));
        self.clients.push(client);
    }.bind(self);
    

    You add the parenthesis to get the current static value of the observable

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

Sidebar

Related Questions

I have a an array of observable collections and I want to display all
I have an observable array with var items= [ {image: /images/image1.jpg }, {image: /images/image2.jpg
I have a Knockout observable array that I wish to edit from within Javascript
I am new to Knockout.js, I have created an observable array and initialized with
If I have an observable array foos = [{ name: a }, { name:
Hi (another Knockout question :-( , sorry) I have an observable array with five
We have a large nested observable array mapped though templates to create a tree
I have an observable array binding to a dropdownlist. The dropdownlist is populated on
I have an observable array that holds a list of people and a table
i have function, which has to accept two types of data - Observable collection

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.