Tuesday, 4 March 2014

KnockoutJS

Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model. Any time you have sections of UI that update dynamically (e.g., changing depending on the user’s actions or when an external data source changes), Knockout can help you implement it more simply and maintainable.

Features of KnockoutJS:
  1. Pure JavaScript: Knockout.js connects the underlying data model to HTML elements by adding a single HTML attribute
  2. Extensible: Knockout.js makes it easy to add your own bindings, giving you complete control over how your data is transformed into HTML
  3. Utility Functions: Array filters, JSON Parser and most useful feature is generic way to map data from server to an HTML view, and these utilities make it possible to turn large amounts of data into a dynamic user interface with just a few lines of code.
KnockoutJS Blocks:
  1. Model: You can save and load your model data via AJAX Call, It’s up to you to communicate between your model storage and Knockout.js.
  2. View: This block is used for HTML/CSS page displayed to the user. After connecting the view to the ViewModel, it will automatically display new, deleted, and updated items when the ViewModel changes.
  3. ViewModel: A pure-JavaScript object representing the cart, including a list of items and save/load methods for interacting with the model. After connecting your HTML view with the ViewModel, your application only needs to worry about manipulating this object (Knockout.js will take care of the view). 
How we Implement KnockoutJS:
  1. Copy the KnockoutJS script to root directory. You can use https://github.com/knockout/knockout/downloads link to get KnockoutJS latest build for development.
  2. Include the KnockoutJS Script and CSS files in the head tag of your HTML page.
    <link href="Style.css" rel="stylesheet" type="text/css" /><script src="knockout-2.1.0.js" type="text/javascript"></script>

Example of KnockoutJS:

1. Basic Example:

<html lang='en'>

<head>

    <title></title>
    <script src="knockout-2.1.0.js" type="text/javascript"></script>
    <script type='text/javascript'>
        var personViewModel =
        {
            firstName: "Vishal",
            lastName: "Kudale"
        };
        ko.applyBindings(personViewModel);
    </script>
</head>
<body>
    <p>
        <span data-bind='text: firstName'></span>'s Cart</p>
</body>
</html>


Output:











2.  List Example by KnockoutJS

<html lang='en'>

<head>

    <title></title>
    <script src="knockout-2.1.0.js" type="text/javascript"></script>
    <script type='text/javascript'>
        var SimpleListModel = function (items) {
            this.items = ko.observableArray(items);
            this.itemToAdd = ko.observable("");
            this.addItem = function () {
                if (this.itemToAdd() != "") {
                    this.items.push(this.itemToAdd()); // Adds the item. Writing to the "items" observableArray causes any associated UI to update.
                    this.itemToAdd(""); // Clears the text box, because it's bound to the "itemToAdd" observable
                }
            } .bind(this);  // Ensure that "this" is always this view model
        };

        ko.applyBindings(new SimpleListModel(["Pune", "Mumbai"]));
    </script>
</head>
<body>
    <form data-bind="submit: addItem">
    New item:
    <input data-bind='value: itemToAdd, valueUpdate: "afterkeydown"' />
    <button type="submit" data-bind="enable: itemToAdd().length > 0">
        Add</button>
    <p>
        Your items:</p>
    <select multiple="multiple" width="50" data-bind="options: items">
    </select>
    </form>
</body>
</html>

Output:




























About Author:
Vishal Kudale  is enthusiast .net developer who works as associate consultant with Systems Plus Pvt. Ltd. He in free time reads and write on various web technologies. He can be contacted at: vishal.kudale@spluspl.com

8 comments:

  1. Really a nice example explaing dynamic, light weighted nature and intereactivity based on varies event supported by knockout.js

    ReplyDelete
  2. really a nice helpful example

    ReplyDelete
  3. really helpful example for JS beginners....

    ReplyDelete
  4. It is nice js example. simply and maintainable.

    ReplyDelete
  5. Very well written, and examples are easy to understand.

    ReplyDelete
  6. Good one... Well explained !!!

    ReplyDelete
  7. It's very helpful to JS beginners..well documented....great job!!!

    ReplyDelete
  8. THANK YOU FOR THE INFORMATION
    PLEASE VISIT US
    erp software companies






    ReplyDelete