Special addressing forms

In the Knockout.js library, there are some specific keywords you will use. Let's review their Knockout MVC analogue:

  • $parent, $parents, $root, and $parentContext: These properties allow you to refer to different data binding contexts. You don't need think about the scopes hierarchy and peculiarities of addressing between them in Knockout MVC—now every context has a name! Consider the following code snippet:
    Razor: @using (var items = ko.Foreach(m => m.Items)) { using (var subItems = items.Foreach(m => m.SubItems)) { @subItems.Html.Span(m => ko.Model.Key + " " + items.Model.Caption + " " + m.Name)<br /> } } Html: <!-- ko foreach: Items --> <!-- ko foreach: SubItems --> <span data-bind="text : $parents[1].Key()+' '+$parent.Caption()+' ...

Get Getting Started with Knockout.js for .NET Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.