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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:43:19+00:00 2026-06-01T09:43:19+00:00

I have a parent-child view model object structure set up and need to update

  • 0

I have a parent-child view model object structure set up and need to update an observable on the parent from the child. I’ve basically come up with two patterns for doing so:

1] Pass a reference of the parent property to the child and update the property from within the child:

var ParentViewModel = function(){
    var self = this;
    this.selectedItem = ko.observable();
    this.child = ko.observable(new ChildViewModel(self.selectedItem));
}

var ChildViewModel = function(parentSelectedItem){
    var self = this;
    this.id = ko.observable();
    this.parentSelectedItem = parentSelectedItem;
    this.select = function(){
        self.parentSelectedItem(self);
    }
}

2] Create the child’s select method on the parent and reference the parent observable locally:

var ParentViewModel = function(){
    var self = this;
    this.selectedItem = ko.observable();

    var child = new ChildViewModel();
    child.select = function(){
        self.selectedItem(child);
    }
    this.child = ko.observable(child);
}

var ChildViewModel = function(){
    this.id = ko.observable();
}

Neither of these patterns send me head over heels. The first one pushes the entire property reference into children view models, the second defines a child’s function outside of the scope of the child.

Does anyone have any other pattern suggestions as to how this operation could be achieved in javascript in a clean and testable manner? Or am I more or less stuck with just these two options?

  • 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-01T09:43:20+00:00Added an answer on June 1, 2026 at 9:43 am

    The most common pattern to do this in Knockout is to put a “selectChild” method on your parent that takes in a child. In most cases, the actual child does not need to know that it is being selected.

    Then in your binding, you can bind to $root.selectChild or $parent.selectChild. The first argument passed to a handler bound to the click/event binding is the actual data (in KO 2.0), so your method can live on the parent and receive the child as the first arg.

    var Item = function(id, name) {
        this.id = id;
        this.name = ko.observable(name);    
    };
    
    var ViewModel = function() {
        var self = this;
        this.items = ko.observableArray([
            new Item(1, "one"),
            new Item(2, "two"),
            new Item(3, "three")
        ]);  
    
        this.selectedItem = ko.observable();
    
        this.selectItem = function(item) {
            self.selectedItem(item);
        };     
    };
    

    In this case, your binding would look like:

    <ul data-bind="foreach: items">
        <li>
            <a href="#" data-bind="text: name, click: $root.selectItem"></a>
        </li>
    </ul>
    

    Here it is in jsFiddle: http://jsfiddle.net/rniemeyer/anRsA/

    You can even simplify it further. Observables are functions and the first argument that you pass to them is used to set their value, so you can even choose to not include the selectItem method and simply bind against $root.selectedItem directly (would look like: http://jsfiddle.net/rniemeyer/anRsA/1/). I usually use a separate method to be explicit, to give it a proper name (action), and in case there is extra processing that needs to be done before or after setting the item.

    Prior to KO 2.0 (where $root and $parent were introduced along with the change to pass the data as the first arg to click and event handlers), I used to use the first method that you suggested quite a bit. One thing that you can do there is actually not create the child property (this.parentSelectedItem) and just reference parentSelectedItem (that was passed as an argument) directly in the select method, as it will be available in the function because of the closure that is created.

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

Sidebar

Related Questions

I have a case where the child view sends notification to its parent view.
I have parent/child relationship set up via Node Reference. A Child record can have
I have a Parent/Child object/mapping as follows: class Parent { int Id; string name;
I have a object model like this public class Parent{ public int Id; public
I have the following entity classes, which are mapped from virtually identical view model
i have a parent child table such that class Service( models.Model ): id =
When you have parent-child tables and you wish to use a DetailsView to edit
I use Entity Framework 4 and I have parent - child relation with Cascade
I have a parent-child relationship setup that is fairly basic. The end result is
Using parent child relationship where a parent can have children while each child has

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.