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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:27:59+00:00 2026-06-04T16:27:59+00:00

I have this very simple test view <button data-bind=click: add>Add</button> <table data-bind=foreach: items> <tr>

  • 0

I have this very simple test view

<button data-bind="click: add">Add</button>
<table data-bind="foreach: items">
    <tr>
        <td data-bind="text: name"></td>
        <td><button data-bind="click: $root.remove">Remove</button></td>
    <tr>
</table>

Fiddle: http://jsfiddle.net/6PP5m/

The problem is that the context of this in the remove method is the child view model that fired the click event

I havent found a solution that I like, you can add a self variable in the contructor and use that instead of “this”, but its more clean code and OO to use the “this” keyword

Fiddle: http://jsfiddle.net/Qn2CM/

You can also create a proxy function delegate but its still not very clean code

Fiddle: http://jsfiddle.net/gYhMr/

What I would like is to tell ko somehow to set the context of this to the correct correct scope ($root in this case) is it possible?

  • 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-04T16:28:01+00:00Added an answer on June 4, 2026 at 4:28 pm

    The click/event bindings run in the context of the current data as you have found.

    There are a few options to ensure that your function runs with the correct context:

    • set the correct this to a variable like self and use it in your handler (as you have mentioned)
    • use something like $.proxy or .bind in your view model to ensure that this is correct (as you have mentioned)
    • bind the function in-line like data-bind="click: $root.remove.bind($root)"
    • use an (ugly) anonymous function like data-bind="click: function() { $root.remove($data); }"
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume we have this very simple 'test' table. There is: 5 bob, 2
I have this html page which is very simple, it contains a text box
I'm stuck! I have this very simple test code and I can't get it
I have this very simple test code: s=helloCustomer is_contain_customer = s=='helloCustomer' || s.include? 'Customer'
I have this very simple example that I am using to learn structs in
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =
I have this very simple C++ class: class Tree { public: Node *head; };
I'm sure this is very simple. I have gotten colorbox to work before, but
This is a very simple question yet I have little knowledge in Linux and
i have a very simple question, actually this is not the question, this is

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.