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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:28:52+00:00 2026-05-25T20:28:52+00:00

Using the knockout mapping plugin ( http://knockoutjs.com/documentation/plugins-mapping.html ) can you map a deeply hierachical

  • 0

Using the knockout mapping plugin ( http://knockoutjs.com/documentation/plugins-mapping.html ) can you map a deeply hierachical object?

If I have an object with multiple levels:

var data = {
    name: 'Graham',
    children: [
        {
            name: 'Son of Graham',
            children: [
                {
                    name: 'Son of Son of Graham',
                    children: [
                        {
                            ... and on and on....
                        }
                    ]

                }
            ]
        }
    ]
}

How do I map it to my custom classes in javascript:

var mapping = {
    !! your genius solution goes here !!

    !! need to create a myCustomPerson object for Graham which has a child myCustomerPerson object 
    !! containing "Son of Graham" and that child object contains a child myCustomerPerson 
    !! object containing "Son of Son of Graham" and on and on....

}

var grahamModel = ko.mapping.fromJS(data, mapping);

function myCustomPerson(name, children)
{
     this.Name = ko.observable(name);
     this.Children = ko.observableArray(children);
}

Can the mapping plugin recursively map this data into an hierachy of my custom objects?

  • 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-05-25T20:28:52+00:00Added an answer on May 25, 2026 at 8:28 pm

    Something like this (Live copy on js fiddle):

    CSS:

    .left {
        float: left;
    }
    
    .clear {
        clear: both;
    }​
    

    HTML:

    <p>Current:&nbsp;
        <a href="#" data-bind="visible: (stack.length > 0), text: selectedNode().name, click: selectParentNode"></a>
        <span data-bind="visible: (stack.length <= 0), text: selectedNode().name"></span>
    </p>
    <p class="left">Children:&nbsp;</p>
    <ul class="left" data-bind="template: {name: 'childList', foreach: selectedNode().children}"></ul>
    
    <script type="text/html" id="childList">
        <li data-bind="click: function(){nodeViewModel.selectChildNode($data)}">
            <a href="#">A${name}</a>
        </li>
    </script>
    
    <br /><br />
    <ul class="clear" data-bind="template: {name: 'backBtn'}"></ul>
    
    <script type="text/html" id="backBtn">
        <a href="#" data-bind="visible: $data.selectedNode().back, click: function() { nodeViewModel.selectBackNode($data.selectedNode().back) }">Back</a>
    </script>​
    

    JavaScript:

    var node = function(config, parent) {
        this.parent = parent;
        var _this = this;
    
        var mappingOptions = {
            children: {
                create: function(args) {
                    return new node(args.data, _this);
                }
            }
        };
    
        ko.mapping.fromJS(config, mappingOptions, this);
    };
    
    var myModel = {
        node: {
            name: "Root",
            children: [
                {
                name: "Child 1",
                back: 1,
                children: [
                    {
                    name: "Child 1_1",
                    back: 1,
                    children: [
                        {
                        name: "Child 1_1_1",
                        back: 4,
                        children: [
                            ]},
                    {
                        name: "Child 1_1_2",
                        back: 2,
                        children: [
                            ]},
                    {
                        name: "Child 1_1_3",
                        back: 1,
                        children: [
                            ]}
                        ]}
                ]},
            {
                name: "Child 2",
                back: 1,
                children: [
                    {
                    name: "Child 2_1",
                    back: 1,
                    children: [
                        ]},
                {
                    name: "Child 2_2",
                    back: 1,
                    children: [
                        ]}
                ]}
            ]
        }
    };
    
    var viewModel = {
    
        nodeData: new node(myModel.node, undefined),
    
        selectedNode: ko.observable(myModel.node),
    
        stack: [],
    
        selectBackNode: function(numBack) {
    
            if (this.stack.length >= numBack) {
                for (var i = 0; i < numBack - 1; i++) {
                    this.stack.pop();
                }
            }
            else {
                for (var i = 0; i < this.stack.length; i++) {
                    this.stack.pop();
                }
            }
    
            this.selectNode( this.stack.pop() );
        },
    
        selectParentNode: function() {
            if (this.stack.length > 0) {
                this.selectNode( this.stack.pop() );
            }
        },
    
        selectChildNode: function(node) {
            this.stack.push(this.selectedNode());
            this.selectNode(node);
        },
    
        selectNode: function(node) {
            this.selectedNode(node);
        }
    
    };
    
    window.nodeViewModel = viewModel;
    ko.applyBindings(viewModel);​
    

    This sample just maps an infinitely nested set of JSON data, and I can say from actually using this exact code in application that is works great.

    Some of the extra functions like

    selectBackNode and selectParentNode

    allow you to move back up the tree.

    While navigating the example the parent label becomes a link to allow for going up one level, and some of the leaf nodes have a back button that allows them to move back up the tree by a given number of levels.

    –EDIT–

    If your leaf nodes don’t have a children array you might get a problem where additional data is introduced that doesn’t exist in the model.

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

Sidebar

Related Questions

I've recently been using Steve Sanderson's knockout js library http://knockoutjs.com/ in my client side
We are using knockout and the knockout mapping plugin to facilitate databinding in our
I am using Knockout and the Knockout Mapping plugin. My MVC3 Action returns a
Using knockout.js, is it possible to bind to a property of a child object
I am binding a list of objects to a select using knockout. Object Class
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Using jQuery, what's the best way to automatically initialize a plugin on all current
Using the Cocoa Framework, how can I add bullets and numbering to text?

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.