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

Working with KnockoutContext


The main object when forming a View is an instance of the KnockoutContext class. It is necessary to create it at the beginning of the view, using the following line:

var ko = Html.CreateKnockoutContext();

At the end of the view, it is necessary to apply the formed data binding. You can do it in the following way:

@ko.Apply(Model)

If you have a lot of data that needs some time to load, it makes sense to use lazy analogue (in this case, a page will load first, and then an additional AJAX query will load all the data):

@ko.LazyApply(Model, "GetDataAction", "ControllerName")

So, a common view template looks like the following snippet of code:

@using PerpetuumSoft.Knockout
@model <!-- You model -->
@{
  var ko = Html.CreateKnockoutContext();
}
<!-- Your page -->

@ko.Apply(Model)