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

  • Home
  • SEARCH
  • 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 9132361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:17:58+00:00 2026-06-17T08:17:58+00:00

I have a problem with the $parent in javascript knockouts. If i have model:

  • 0

I have a problem with the $parent in javascript knockouts.

If i have model:

        var Person = function () {

        var self = this;

        self.person_Id = ko.observable(); 
        self.firstName = ko.observable(); 
        self.lastName = ko.observable(); 
        self.age = ko.observable(); 
        self.role = ko.observable(); 

        self.init = function (data) {

            self.person_Id(data.person_Id);
            self.firstName(data.firstName);
            self.lastName(data.lastName);
            self.age(data.age);
            self.role(data.role);
        };

    };

and the collection

        var PersonCollection = function () {

        var self = this;

        self.ItemToEditOrToAdd = ko.observable(new Person());  

        self.persons = ko.observableArray();

        self.AddItem = function (item) {

            self.persons.push(item);


        };

    };

and the html

                <div id="new" data-bind="with: PersonCollectionInstance.ItemToEditOrToAdd">

                <p>Firstname: <input type="text" data-bind="value: firstName"/></p>
                <p>Lastname: <input type="text" data-bind="value: lastName"/></p>
                <p>Age: <input type="text" data-bind="value: age"/></p>
                <p>Role: <input type="text" data-bind="value: role"/></p>
                <button data-bind="click: $parent.PersonCollectionInstance.AddItem">Add new</button>

            </div>

If you look at the html, the natural way to access AddItem is to write

<button data-bind="click: $parent.AddItem">Add new</button>

This doesnt work, because the $parent binding gets me to my AppVM that is my all mighty view model, instead of going back to my PersonCollectionInstance.

Thats why i need to do it like this:

 <button data-bind="click: $parent.PersonCollectionInstance.AddItem">Add new</button>

Can anyone explain to me why $parent gets me back to my all mighty viewmodel instead of the PersonCollectionInstance where my AddItem is?

  • 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-17T08:17:59+00:00Added an answer on June 17, 2026 at 8:17 am

    You need to write data-bind="click: $parent.PersonCollectionInstance.AddItem" because you have defined your with like this:

    data-bind="with: PersonCollectionInstance.ItemToEditOrToAdd"`
    

    So you have stepped down two levels in your with binding because you have accessed a nested property. However the $parent is still pointing to your main view model because knockout isn`t able track your nested property and it doesn’t create the nested contexts for you.

    So can achieve the desired outcome with using two with:

    <div id="new" data-bind="with: PersonCollectionInstance">
      <!-- ko with: ItemToEditOrToAdd -->
        <p>Firstname: <input type="text" data-bind="value: firstName"/></p>
        <p>Lastname: <input type="text" data-bind="value: lastName"/></p>
        <p>Age: <input type="text" data-bind="value: age"/></p>
        <p>Role: <input type="text" data-bind="value: role"/></p>
        <button data-bind="click: $parent.AddItem">Add new</button>
      <!-- /ko -->
    </div>
    

    And if you anyway using two with you can put your button “outside”:

    <div id="new" data-bind="with: PersonCollectionInstance">
      <!-- ko with: ItemToEditOrToAdd -->
        <p>Firstname: <input type="text" data-bind="value: firstName"/></p>
        <p>Lastname: <input type="text" data-bind="value: lastName"/></p>
        <p>Age: <input type="text" data-bind="value: age"/></p>
        <p>Role: <input type="text" data-bind="value: role"/></p>       
      <!-- /ko -->
      <button data-bind="click: AddItem">Add new</button>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem calling a JavaScript function in an iframe from the parent page.
I have a problem with jQuery click() function. I use this function to programatically
I have problem to release div in parent window. I need to do this:
I have defined two classes in javascript as follows. function ParentClass(){ this.one = function(){
I am often stuck on this problem where I have a parent element with
I have a problem with the use of the javascript removeChild function. Here's my
I have problem with my query on C, I’m using the oci8 driver. This
I have a rather interesting problem. I have a parent page that will create
I have this javascript code that creates a slider: http://jsfiddle.net/samccone/ZMkkd/ Now, i want to
I have the following code that I am playing with: <script type=text/javascript> var Dash

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.