Before reading this article you first need to read my previous article:
This article explains how to use the Observable Array in Knockout.js. Knockout.js defines an important role when we want to detect and respond to changes on one object, we uses the observable. An observable is useful in various scenarios where we are displaying or editing multiple values and require repeated sections of the UI to appear and disappear as items are inserted and deleted. The main advantage of KO is that it updates our UI automatically when the view model changes. To apply these features of KO we need to declare our model properties as observable, because there is special JavaScript that defines the important role in notifying subscribers about changes and can automatically detect dependencies. KO is the most important feature useful for the Automatic refresh of UI. It can be done when the view will be notified on the changes of ViewModel.
Step 1
Start Visual Studio.
Figure 1: Visual Studio
Step 2
Now for creating a website, click on File, go to New and click on Web Site.
Figure 2: Create Website
Step 3
Now we need to add the knockout.js library to our website. To do that we will manage the NuGet Package and for that we will right-click on the website and click on the NuGet Packages.
Figure 3: Manage NuGet
Step 4
Now the next step to search and install the Knockout.js and jQuery library in the website as I have shown in my previous article. After adding the Knockout.js library we need to add the Web Form to the Website. By right-clicking add a Web Form to the website.
Figure 4: Add Web Form
After adding the Web Form on the website, we will write its name as in the following:
Figure 5: Name of Form
Step 5
Now we will write the code for the observable array.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Understanding Observables in Knockout.js</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <p>First Name :<span data-bind="text: firstName" /></p>
- <p>Middle Name :<span data-bind="text: MiddleName" /></p>
- <p>Last Name:<span data-bind="text: LastName"></span></p>
-
- </div>
- <hr />
- <div>
- <p>First Name<asp:TextBox ID="txname" runat="server" data-bind="value:firstName"></asp:TextBox></p>
- <p>Middle Name<asp:TextBox ID="txtlstnme" runat="server" data-bind="value:MiddleName"></asp:TextBox></p>
- <p>Last Name<asp:TextBox ID="txtlastname" runat="server" data-bind="value:LastName"></asp:TextBox></p>
- </div>
- <hr />
- <p>Full Name :<span data-bind="text: fullName"> </span></p>
- </form>
- <script src="Scripts/knockout-3.3.0.js"></script>
- <script type="text/javascript">
- var vm =
- {
- firstName: ko.observable("yatendra"),
- MiddleName: ko.observable("Kumar"),
- LastName: ko.observable("Sharma")
- };
- vm.fullName = ko.dependentObservable(function () {
- return vm.firstName() + " " + vm.MiddleName() + " " + vm.LastName();
- });
- ko.applyBindings(vm);
- </script>
- </body>
- </html>
In this code I have used the observable to detect and respond to changes on one object. So the observable is helpful when we want changes on the multiple values and need repeated sections of the UI to appear and disappear as items are added and removed.
Step 6
After writing the code, now we will execute the website using the F5 key. After execution the following window appears on the screen.
Figure 5: Execution Window
Step 7
Now if we change in the first name in the text box then the upper part of the window changes automatically.
So it shows that the observable is working perfectly when we change in one part then the changes happens simultaneously in the other connecting parts.
Summary
This article explained how the observable works in the Knockout.js and how to use the Observable Knockout.js in the Visual Studio 2013.
I hope this article will be helpful for the beginners if they want to use observables in Knockout.js.