A Responsive Blogger Website, That Take my blog to the next level.

I am a Software Developer, Learning and experementing with .Net Technology and try to put my Learning altogether here.


We have all sections related to .Net Technology, like .Net-C#, Asp.Net, MVC, jQuery, AngularJs, Sharepoint. Find out more...


Following are the some of the Advantages of using this Web site :-

  • Get Update about latest in .Net world.
  • Collection of Useful, Frequently used .Net Solutions.
  • It’s beautiful on every screen size (try resizing your browser!)
by

Part 1: Getting started with Knockout.js

In this post we will take a look on Knockout.js. We will start with basic theoretical introduction and then to some basic demo on Knockout.js
Knockout.js helps us to create dynamic JavaScript UI using the MVVM pattern. It simplifies the task of linking Data Model to the dynamic JavaScript UI.
Knockout.js is a JavaScript library and it helps us to create high responsive, very rich and interactive JavaScript UI adhering to MVVM pattern. It simplifies the task of binding data from Data Model to JavaScript UI. It is a pure JavaScript library and it works with almost all the browsers. It is an open source project under the license of MIT. It is very compact in size. Its zipped size is 13kb and it can be added over any existing web application.
Essentially Knockout.js helps to link a Data Model to UI. It tracks the changes and dynamically updates the UI. It helps to perform two ways binding in between UI and Data Model. It updates certain section of UI dynamically when data in data model is changed. It simplifies the task of binding and dynamic updating the UI.
Some of the features of Knockout.js can be outlined as below,
It detects the data changes in data model and updates respective part of the UI.
It binds data and UI declaratively. In other words declarative binding between data model and UI can be done using Knockout.js
It helps to create custom behavior. Custom behavior can be created as declarative binding.
Custom behavior created by Knockout.js can be reused with very less lines of code.
It helps to create complex dynamic UI using declarative bindings.
It helps to edit JSON data on the JavaScript UI.
Knockout.js is JavaScript MVVM framework and it helps in
Further in the post, we will explore each features of Knockout.js. You can download Knockout.js from here. You will get many versions of Knockout.js to download on the download page. For usual production purpose you can choose to download knockout-versionnumber.js file.
After downloading add reference of Knockout.js.
Next let us create a ViewModel. We are going to create ViewModel for Product. A ViewModel can be created as following. You can see that in knockout ViewModel is nothing but a JavaScript array,
Once you have created ViewModel you need to apply binding. Binding can be applied as below,
Last step is to bind values from ViewModel on the view. That can be done as following. Using data-bind attribute values from ViewModel can be bind to element on View.
A complete example of Product ViewModel data being bind with elements of View is given below,
<!--
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="Scripts/jquery-1.7.1.js"></script>
<script src="Scripts/knockout-2.2.0.js"></script>
<script type="text/javascript" >
$(document).ready(function () {
var ProductViewModel = {
productname: "pen",
productprice: 200,
producttype :"education"
};
ko.applyBindings(ProductViewModel);
});
</script>
</head>
<body>
<h2>Knockout Demo</h2>
Product Name : <span id="namespan" data-bind="text:productname"></span> <br />
Product Price : <span id ="pricespan" data-bind="text:productprice"></span><br />
Product Type : <span id="typesapn" data-bind="text:producttype"></span> <br />
</body>
</html>"
-->
And as expected you will get values from ViewModel on View elements as following
This was small introduction of Knockout.js. In further posts we will get into more details of Knockout.js and see examples of its usage in real examples. I hope you find this post useful. Thanks for reading.

0 comments :