Alex Rivera | Logout

knockout, nested foreach

Asked 2012-07-30T12:10:37.617
17

Let's I have Humans with Cats with Kittens

class Master
{  
   String masterName;

   Cat[] cats;  
}
class Cat 
{
   String catName;

   Kitten[] kittens;
}

class Kitten 
{
   String kittenName;
}  

Now I want show all my Kittens with Cats with Masters in html. I use

                    <!-- ko foreach: humans -->
                    <!-- ko foreach: cats -->
                    <!-- ko foreach: kittens -->
<p data-bind="$data.kittenName"></p>
<p data-bind="$parent.catName"></p>
<p data-bind="???????"></p>   <!-- How get master's name? -->
                    <!-- /ko -->
                    <!-- /ko -->
                    <!-- /ko -->
Edit
Report

1 Answer

13

You can use, $root to get to the base object - which in your case will be at the level of Master.

<!-- ko foreach: humans -->
    <!-- ko foreach: cats -->
        <!-- ko foreach: kittens -->
            <p data-bind="$data.kittenName"></p>
            <p data-bind="$parent.catName"></p>
            <p data-bind="text:console.log($root, $parent, $data)"></p>   <!-- How get master's name? -->
        <!-- /ko -->
    <!-- /ko -->
<!-- /ko -->
answered 2012-07-30T12:15:57.010

Your Answer