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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:13:43+00:00 2026-06-17T11:13:43+00:00

Here is a jsFiddle which demonstrates my problem: http://jsfiddle.net/dDEd5/4/ In summary, I have a

  • 0

Here is a jsFiddle which demonstrates my problem: http://jsfiddle.net/dDEd5/4/

In summary, I have a simple ViewModel:

ViewModel = function () {}
ViewModel.prototype = {
    child: function () {},
    children: new Array(3),

    outermethod: function () {
        this.innerMethod();
    },

    innerMethod: function () {
        alert("ok!");
    },

    outerProperty: function () {
        return this.innerProperty();
    },

    innerProperty: function() {
        return "Property is OK";
    }
}

I am attempting to bind this ViewModel using a ‘click’ binding. The problem is that when my binding uses the $parent context, the value of ‘this’ within my ViewModel fails to resolve to the ViewModel.

For example, this binding works fine:

<div>
    <span data-bind="text: outerProperty()"></span>
    <button data-bind="click: outermethod">This Works</button>
</div>

However, when I use another binding context and attempt to call my ViewModel using $parent, things break down. In the following two example, the property resolves fine; however, the buttons both error out:

<div>
    <!-- ko with: child -->
    <span data-bind="text: $parent.outerProperty()"></span>
    <button data-bind="click: $parent.outermethod">This Doesn't</button>
    <!-- /ko -->
</div>

and

<div>
    <!-- ko foreach: children -->
    <span data-bind="text: $parent.outerProperty()"></span> 
        <button data-bind="click: $parent.outermethod">These Don't Either</button>
    <!-- /ko -->
</div>

I have done my due diligence trying to understand how Execution Contexts work in javascript and why these examples fail; however, I am at at loss on this.

  • 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-17T11:13:44+00:00Added an answer on June 17, 2026 at 11:13 am

    When Knockout executes a handler it uses the current data bound at that level as the context. So, when using something like $parent or $root, this can cause issues.

    There are a few ways to handle it:

    -you can bind it to the proper context in the binding itself like:

    `click: $parent.outermethod.bind($parent)`
    

    This returns a new function that ensures $parent will be this.

    -you can bind it in your view model. Since you are placing the function on the prototype it is a little more challenging.

    One technique (not using prototype) is to use a variable to track the correct value of this and reference it in your function like:

    var ViewModel = function() {
      var self = this;
    
      this.outermethod = function() {
         self.innerMethod();
      };
    
    };
    

    Using the prototype, you can still put the implementation on the prototype and then create a bound version on the actual instance like:

    var ViewModel = function() {
       this.outermethod = this.outermethod.bind(this);
    };
    

    So, this will create a new function on the instance that call the prototype’s implementation of the function with the correct context.

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

Sidebar

Related Questions

I have the following problem: http://jsfiddle.net/DerNa/12/ Which is demonstrated more clearly the jump here:
I have a layout setup which can be view here: http://jsfiddle.net/Pn3ts/ It all works
All of this is code is here: http://jsfiddle.net/yrgK8/ I have a news section which
I have a jsfiddle that demonstrates the problem at: http://jsfiddle.net/michaelajohnsonwa/sDchM/7/ The generated dropdown elements
I have the following Table example here: http://jsfiddle.net/Lu9y3/ Which is based on the Telerik
I've got a problem which you can look at here http://jsfiddle.net/dng2P/4/ If you click
I have a fiddle here: http://jsfiddle.net/ybZvv/43/ The problem I have is with the text
Here is a jsfiddle which works: http://jsfiddle.net/n52ES/ It is how I have it set
First of all, here is a JSFiddle which has the problem I'm getting. http://jsfiddle.net/UG66K/9/
I have a problem with a float which overflows ( JSFiddle here ). HTML

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.