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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:07:16+00:00 2026-06-14T17:07:16+00:00

I’m new to knockout.js and am trying to bind to the following object representing

  • 0

I’m new to knockout.js and am trying to bind to the following object representing a user:

{
    "$id": "1",
    "$values": [
        {
            "$id": "2",
            "Locations": {
                "$id": "3",
                "$values": []
            },
            "Photos": {
                "$id": "4",
                "$values": []
            },
            "UserId": 1,
            "Name": "Test User"
        }
    ]
}

A user can zero or more locations, and zero or more photos.

The view model:

function UsersViewModel() {
    var self = this;
    self.users = ko.observableArray();

    var baseUri = 'http://localhost:46241/api/users';

    $.getJSON(baseUri, function (data) {
        self.users = data;
    });
}

$(document).ready(function () {
    ko.applyBindings(new UsersViewModel());
})

The HTML contains the following binding:

<ul id="update-users" data-bind="foreach: users"> 
     <li> 
            <div><div class="item">User ID</div>
                <input type="text" data-bind="value: $data.UserId" />
            </div>                 
            <div><div class="item">Name</div>
                <input type="text" data-bind="value: $data.Name" />
            </div>                  
     </li> 
</ul>

Am I flat out doing this incorrectly? Or are user’s object references to Locations and Photos possibly messing up the binding?

  • 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-14T17:07:17+00:00Added an answer on June 14, 2026 at 5:07 pm

    Am I flat out doing this incorrectly?

    Yes. You are overwriting your observableArray in the JSON callback, thus destroying it:

    $.getJSON(baseUri, function (data) {
        self.users = data;
    });
    

    In general, knockout observables are assigned to like this:

    $.getJSON(baseUri, function (data) {
        self.users(data.$values);
    });
    

    Note that in your case, data.$values seems to contain the actual array, not data itself.

    This would work for your case, but it’s not using knockout to its full potential.

    The mapping plugin has been created for exactly this case. It recursively traverses a complex object and turns all its properties into observables and all contained arrays into observable arrays. This gives you fine-grained control over the state of the object, allowing you to monitor every change.

    $.getJSON(baseUri, function (data) {
        ko.mapping.fromJS(data.$values, {}, self.users);
    });
    

    It also allows partial updates: Assume you retrieve a list of users from the server and your view model already has half of them. The mapping plugin is capable of adding only those to your view model that are missing and making appropriate changes to the existing ones. Read the “Advanced usage” section in the docs if you want to know how.


    Assuming you get an array of user objects from the server and use the mapping plugin, then your binding would look like this:

    <ul id="update-users" data-bind="foreach: users"> 
         <li> 
                <div><div class="item">User ID</div>
                    <input type="text" data-bind="value: UserId" />
                </div>                 
                <div><div class="item">Name</div>
                    <input type="text" data-bind="value: Name" />
                </div>
                <ul class="photos" data-bind="foreach: Photos.$values">
                  <!-- ... -->
                <ul>
         </li> 
    </ul>
    

    On a general note, @Greg Smith’s comment is correct. Try to lose the $ in the object keys, they might clash with special variables in knockout at some point. You could safely replace them with underscores, for example.

    On a more general note, I’d try to strip out the whole $id/$values notion in your JSON if I were you, it does not seem to serve any purpose:

    [
        {
            "Locations": [],
            "Photos": [],
            "UserId": 1,
            "Name": "Test User"
        }
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.