Here the agenda is to run through several things, kind of a nutshell first. Since this is a Knockout.js introduction we will talk about why we should use Knockout a little bit and then I get into the core fundamentals of Knockout. What is observable, how you do computed and what is an observableArray. Then an overview of KO (KO is the second name of Knockout) so KO with MVVM or Knockout with MVVM.

So look at all these kinds of things and then we have a look at installation and a simple demo as we go through.

What Knockout.js is

Knockout is an application library rich in JavaScript by Steven Sanderson originally who is now with Microsoft when he wrote it he was not with Microsoft.



It uses the MVVM pattern which is the Model View View Model pattern and provides rich client-side interactivity, Don't worry about the browser support, your browser can support Knockout If you are using IE 6 or up.

And now your question must be, what are the benefits of Knockout?


Now understand the main three core pieces of Knockout that help you to understand what you gain from Knockout.

First pieces is Observable



You need to tell Knockout about the updating of your view model part and for that you need to declare your model properties as Observable. As in the following example:

  1. var myViewModel = {
  2. FirstName: ko.observable(“FirstName”),
  3. LastName: Ko.observable(“LastName”)
  4. };

Since we have an interface called I-notify property change that can be applied to classes in C# or VB they allow us to communicate between the properties of those classes and to the UI. What that means is it means when use type types a value in a text box, it will automatically save that value into the backing model, the view model and when the model is updated it automatically syncs to the UI.

Second pieces are Computed


Now for the Computed observables, they are functions that are dependent on one or more other observables and will automatically update whenever any of these dependencies change.

Like you have one observable for firstName, and another for lastName, and you want to display the full name.

The last core pieces of Knockout is observableArray as in the following:



ObservableArray is used when you want to detect and respond to changes of a collection of things, it is mainly used in that scenario in which you are displaying or editing multiple values and need repeated sections of the UI to appear and disappear as items are added and removed.

So how do you do Knockout.js? It's simple, in just the following 3 steps:


Let's understand the MVVM pattern. It will help you to understand how Knockout.js data binding is done between your JavaScript and HTML.



The Model is responsible for holding application data and the view is responsible for presenting the model data to the user, finally the ViewModel is responsible for establishing the communication with the View and Model.

Knockout.js is just simplifies the MVVM data-binding process by using observables to dynamically change viewmodel properties upon changes to the view.

Now let's setup development environment for Knockout.js and then we will see a simple demo

Now to setup development environment in Visual Studio

Complete Code

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>My first Ko Application</title>
  5. <script src="Scripts/knockout-3.0.0.js"></script>
  6. <script src="Scripts/jquery-2.0.3.js"></script>
  7. </head>
  8. <body>
  9. <p>First Name :<strong data-bind="text:initialName"></strong></p>
  10. <p>Last Name :<strong data-bind="text:lastName"></strong></p>
  11. <input data-bind="value:initialName" />
  12. <input data-bind="value:lastName" />
  13. <script type="text/ecmascript">
  14. function koapp() {
  15. this.initialName = ko.observable();
  16. this.lastName = ko.observable();
  17. };
  18. ko.applyBindings(new koapp());
  19. </script>
  20. </body>
  21. </html>

Now let us proceed to run the application.



When you change the value in any of the input boxes, the label value will be immediately changed. So we have seen that two-way binding and automatic UI refresh is very simple to implement using Knockoutjs.


Thank the you.