Book Image

KnockoutJS by Example

By : Adnan Jaswal
Book Image

KnockoutJS by Example

By: Adnan Jaswal

Overview of this book

KnockoutJS By Example is a project-based guide that introduces the key features and concepts of knockout.js. It helps you create an application skeleton and a Hello World application. You will develop a To-Do list application that aims to show the basic features of knockout.js in action, such as data binding and observables, following which you will develop a dynamic online customer registration form that captures and validates customer information. This book will further walk you through developing a customer banking portal, which demonstrates the use of knockout.js with components such as navigation bars, tabs, carousels, master details view, panels, forms, and wizards. You will also discover how to use token-based authentication and authorization to secure the customer banking portal, and move on to creating an editable products grid with CRUD operations. Finally, you will explore how to use the Google Maps API with knockout.js. KnockoutJS By Example will not only leave you with a basic understanding of knockout.js fundamentals but also take you through some of the advanced features. It will help you get a web application up and ready instantly.
Table of Contents (17 chapters)
KnockoutJS by Example
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Index

Understanding the MVVM design pattern


Knockout implements the MVVM design pattern. It is imperative to understand the basic concept behind MVVM before we dive into Knockout. This will help us grasp how two-way binding is implemented in Knockout and what are its benefits.

MVVM is a design pattern that lets you decouple your UI elements from your application logic and data. It lets you define data binding to link the UI elements to the data. The data bindings provides loose coupling and keeps the data in sync with the UI elements. The MVVM pattern provides clear separation of concerns between UI elements, application logic, and the data.

The three main components of this pattern are:

The model

The model is a domain object, which holds the data and domain-specific logic. An example of a model could be of a contact in an address book, containing contact ID, name, and phone number. The following is an example of a contact model in JavaScript:

var contact = {
    id: 1,
    name: 'John',
    phoneNumber: '00 11 000000'
};

The model should not contain any application logic such as service calls. The model can contain business-specific logic that is independent of the UI. Separating business logic from UI makes the code more maintainable and testable.

Note

The contact object in the given example is declared as an object literal, which uses Java Script Object Notation (JSON). It is important to familiarize yourself with this notation if you are not. You can find more on this topic at http://json.org/.

The view model

The view model holds the model and any application logic such as adding or removing data or making service calls to retrieve or storing data from server-side data repositories. The following is an example of a view model that holds a contact and provides method to retrieve the contact from a server-side data repository:

var contactViewModel = {
  var contact = {
    id: 1,
    name: 'John',
    phoneNumber: '00001111'
  };

  Var retrieveContact = function (){
    /* logic to retrieve contact form server side data repository */
  };

  Var updateContact = function (newPhoneNumber){
    /* logic to update the contact with new phone number */
  };
};

The view model itself does not have any concept of the HTML elements, button-click event, or how the data in the model should be displayed. It simply holds the data model and a set of actions in the form of functions that manipulate the data.

In the preceding example, contactViewModel holds the contact model. It also has two functions that are used to manipulate the contact model. The retrieveContact function, which is used to retrieve a contact from the server-side, and the updateContact function, which is used to update the contact with a new phone number.

The view

The view is what the end user sees and interacts with on the screen. The view is a representation of the data contained in the model. The view also provides a mechanism to interact with the model. In our example, the model contains a contact. The view can display the contact and provide HTML elements such as buttons to retrieve and update the contact.

In Knockout, the view is the HTML with data bindings that link the view to the view model. The following HTML displays the contact name and phone number:

The phone number for <span data-bind="text: name"></span> is <span data-bind="text: phoneNumber"></span>
<button data-bind="click: retrieveContact">Load Contact</button>

In the preceding example, the contact name and phone number are being displayed using the text binding. Click binding is used to link the Load Contact button to retrieveContact function in our view model. We will explore bindings in much more detail later on.

The following figure depicts the relationship between view, model, and view model:

In the preceding figure, the view model holds the state of the model and provides behavior to the view. The view binds with the model and this keeps the view and the model in sync. The view also binds with the view model for operations, for example, the behavior to load contacts. The view model uses the model to manipulate the data. For example, the retrieveContact function retrieves a contact and sets it in the model.