O'Reilly logo

Getting Started with Knockout.js for .NET Developers by Andrey Akinshin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Nested contexts

Nested contexts allow developers to wrap a part of a view in a special construction, which offers some additional behavior.

It is possible to create the following types of nested contexts:

  • Foreach: This nested context walks through all collection elements and provides easy access to HTML elements:
    Razor: @using (var items = ko.Foreach(m => m.Items))
           {
             <tr>
               <td @items.Bind.Text(items.GetIndex())>
               </td>
               <td @items.Bind.Text(m => m)>
               </td>
             </tr>
           }
    Html:  <!-- ko foreach: Items -->
             <tr>
               <td data-bind="text : $index()">
               </td>
               <td data-bind="text : $data">
               </td>
             </tr>
           <!-- /ko -->
  • With: This allows easy referring to a subentity of the main model:
    Razor: @using (var subModel = ko.With(m => m.SubModel)) { using (var subSubModel = subModel.With(m ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required