Book Image

Getting Started with Knockout.js for .NET Developers

By : Andrey Ankshin
Book Image

Getting Started with Knockout.js for .NET Developers

By: Andrey Ankshin

Overview of this book

Table of Contents (14 chapters)
Getting Started with Knockout.js for .NET Developers
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Binding context


Binding contexts are a hierarchical structure of special objects that help you make logical scopes for the control flow bindings. Each such binding (such as foreach, if, with, and so on) generates its own binding context in the hierarchy. You can think of contexts as a scoped wrapper of a ViewModel. For example, the root binding context refers to your main ViewModel. You can work with contexts with the help of special properties that are listed as follows. The best way to understand the binding context concept is to look at some examples. Let's do it.

  • $parent: This refers to the ViewModel of the parent binding context. For example, say you have the foreach binding to the book collection, but want to use the name of the library inside foreach. The parent context helps you as follows:

    <div data-bind="foreach: Books">
      The book "<span data-bind="text: Name"></span>" from the "<span data-bind="text: $parent.LibraryName"></span>" library.
    </div...