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

Lazy loading


In some cases, you have a big amount of data, and the page loading process needs time. A good practice is loading the full page markup first and then loading big data with an additional request to the server. Knockout MVC allows you do it in an easy way.

Imagine that your library contains a large number of books. Let's consider an example with lazy loading of the book list.

The Model will be as follows:

public class BookModel
{
  public string Title { get; set; }
  public string Author { get; set; }
}
public class LibraryModel
{
  public bool IsLoaded { get; set; }
  public List<BookModel> Books { get; set; }

  public void LoadData()
  {
    Books = new List<BookModel>();
    Books.Add(new BookModel { Title = "Oliver Twist", Author = "Charles Dickens" });
    Books.Add(new BookModel { Title = "Winnie-the-Pooh", Author = "A. A. Milne" });
    Books.Add(new BookModel { Title = "The Hobbit", Author = "J. R. R. Tolkien" });
    Thread.Sleep(2000); // Emulation of loading...