Sunday, January 20, 2013

What is Knockout ?

What is Knockout ?
  • Knockout is a JavaScript Library
  • Which helps the creation of rich, desktop-like web UIs
  • It simplifies user interactions and makes interfaces fully responsive to any data source changes
  • Knockout provides a simple two-way binding mechanism to link a data model to an UI,
  • Thus making synchronization between them a breeze

What are the Key Concepts of Knockout ?

1. Declarative Bindings
  • These allow you to connect parts of your UI to your data model in a simple and convenient way
  • With declarative bindings even if you change the DOM all bound pieces stay connected
  • You can bind data to a DOM by simply including a data-bind attribute to any DOM element
Declarative Bindings


2. Automatic UI Refresh

Automatic UI Refresh

3. Dependency Tracking
  • Every time when your model data has changed all parts associated with it automatically being updated
  • No need to worry about adding event handlers and listeners
  • All that extra work is performed internally by Knockout and observables,
  • Which notify listeners when underlying values have changed


Dependency Tracking

4. Templating
  • when your application becomes more complex and you need a way to display a rich structure of view model data,thus keeping your code simple
  • Knockout has a native, built-in template engine which you can use right away
  • But if you want, a third-party template engine, like jquery.tmpl or Underscore, also can be used
Templating


More Features of Knockout

More Features of Knockout


How to Download Knockout ?

Step 1

Download Knockout

Step 2

Download Knockout

Step 3

Download Knockout


How to work with Knockout ?

1. You have to add a reference to the "knockout-2.2.0.js" File which you downloaded

add a reference

2. Then You have to add Reference to above file inside your master file
     i.e. Inside \Views\Shared\_Layout.cshtml

<script src="@Url.Content("~/Scripts/knockout-2.2.0.js")" type="text/javascript"></script>


That's it.Your done.

Lets try with some basic capabilities of Knockout

   - ASP.Net MVC 3 and Visual Studio 2010 has been used 

CASE 1 : How to use bindings in the View (Static Data) ?
  • For that you can use data-bind attributes

View's Code

<!-- This is a View - HTML markup that defines the appearance of your UI -->
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>

Javascript Code

<script type="text/javascript">

    //This is a simple Viewmodel
    //JavaScript that defines the data and behavior of your UI
    function AppViewModel() {
        this.firstName = "Sampath";
        this.lastName = "Lokuge";
    }

    //Activates knockout.js
    ko.applyBindings(new AppViewModel());

</script>

Output


How to use bindings in the View


CASE 2 : How to Make the data Editable (Dynamic Data) ?
  • That's why Knockout has a concept of observables
  • These are properties that automatically will issue notifications whenever their value changes
i.e.
  • Not only that the underlying ViewModel data is being updated when you edit ,
  • But that all associated UI is updating in Sync with it too

View's Code

<!-- This is a View - HTML markup that defines the appearance of your UI -->
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>

<p>First name:<input data-bind="value: firstName" /></p>
<p>Last name:<input data-bind="value: lastName" /></p>

Javascript Code

<script type="text/javascript">

    //This is a simple Viewmodel
    //JavaScript that defines the data and behavior of your UI
    function AppViewModel() {
        this.firstName = ko.observable();
        this.lastName = ko.observable();
    }

    //Activates knockout.js
    ko.applyBindings(new AppViewModel());

</script>


Output

How to Make the data Editable (Dynamic Data)


CASE 3 : How to Define Computed Values ?
  • To handle this, Knockout has a concept of Computed Properties
  • These are observable (i.e., they notify on change)
  • And they are computed based on the values of other observables

View's Code

<!-- This is a View - HTML markup that defines the appearance of your UI -->
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>
<p>First name:<input data-bind="value: firstName" /></p>
<p>Last name:<input data-bind="value: lastName" /></p>

<p>Full name: <strong data-bind="text: fullName"></strong></p>


 Javascript Code

Note :  In order to simplify things you can create a variable self (i.e. var self = this; )

<script type="text/javascript">

    //This is a simple Viewmodel
    //JavaScript that defines the data and behavior of your UI
    function AppViewModel() {
        var self = this;
        self.firstName = ko.observable();
        self.lastName = ko.observable();

        self.fullName = ko.computed(function () {
            return self.firstName() + " " + self.lastName();
        });
    }

    //Activates knockout.js
    ko.applyBindings(new AppViewModel());

</script>

Output

How to Define Computed Values



CASE 4 : How to add UPPER-CASE behavior ?
  • Need to add a capitalizeLastName function to the viewmodel
  • To Read or Write an observable's value, you have to call it as a function

View's Code

<!-- This is a View - HTML markup that defines the appearance of your UI -->
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>
<p>First name:<input data-bind="value: firstName" /></p>
<p>Last name:<input data-bind="value: lastName" /></p>

<p>Full name: <strong data-bind="text: fullName"></strong></p>

<button data-bind="click: capitalizeLastName"> Go CAPS </button>


 Javascript Code

<script type="text/javascript">

   //This is a simple Viewmodel
   //JavaScript that defines the data and behavior of your UI
   function AppViewModel() {
        var self = this;
        self.firstName = ko.observable();
        self.lastName = ko.observable();

        self.fullName = ko.computed(function () {
           return self.firstName() + " " + self.lastName();
        });

        self.capitalizeLastName = function () {
            var currentVal = self.lastName();//Read the current value
            self.lastName(currentVal.toUpperCase());//Write back a modified value
        };
    }

    //Activates knockout.js
    ko.applyBindings(new AppViewModel());

</script>


Output


How to add UPPER-CASE behavior



Small discussion about Knockout on stackoverflow, Which I have involved

What has Said Creator of Knockout about This Article ?
Creator of Knockout



Conclusion
  • This is a very basic example, but it illustrates some of the key points of Knockout
  • You've got a clean, object-oriented representation of your UI's data and behaviors (your viewmodel)
  • Separately, you've got a declarative representation of how it should be displayed visibly (your view)
  • You can implement arbitrarily sophisticated behaviors just by updating the viewmodel object
  • You don't have to worry about which DOM elements need to be changed,added or removed ,
  • The framework can take care of synchronizing things for you

I hope this helps to You.Comments and feedback greatly appreciated.

If you feel it was a good article,Give me a +1.Thank You.



You Might Also Like

Sunday, January 6, 2013

How To Install Windows Azure SDK for .NET ?

Author's Note :
  • Here I am explaining the experience Which I had with Installation of Windows Azure SDK for .NET - October 2012 version (Latest at the moment ) on My Computer
  • This may be differ for other Environments (according to the software installed)

What is Windows Azure SDK for .NET ?
  • It Enables the creation, configuration, building, debugging, running, packaging and deployment of scalable web applications and services on Windows Azure

What are the Softwares Installed on My Computer ?
  • Windows 7  -  64 bit
  • VS 2010 SP1 -  32 bit
  • IIS 7.5 Express

How to Install  Azure SDK for .NETOctober 2012 ?

That's It.You're Done.

But Be Aware
  • If you don't have a Good Luck Then you're in my boat 
  • There are situations where you can have very annoying issues which I had
  • Lets see what are those issues one by one

Issue No. 1 : There was an error attaching the debugger to the IIS worker .......


error attaching the debugger to the IIS worker

Solution : For The Windows 7 with IIS Express Editions Environment

1. From the Start menu, choose Control Panel ---> Programs ---> Programs and Features


Programs and Features


2. Choose Turn Windows Features On or Off

Turn Windows Features On or Off


3. Under Microsoft .NET Framework 3.5, select Windows Communication Foundation
    HTTP   Activation

Windows Communication Foundation     HTTP   Activation


4. Under Internet Information Services --> expand World Wide Web Services --> then
    Application Development Features --> then select .NET Extensibility, ASP.NET,
    ISAPI Extensions and ISAPI Filters (keep default selected items as it is)

ISAPI Filters


5. Under Internet Information Services --> expand World Wide Web Services --> then
    Common HTTP Features --> then select Directory Browsing, HTTP Errors,
    HTTP Redirection, Static Content (keep default selected items as it is)

Static Content


6. Under Internet Information Services --> expand World Wide Web Services --> then
    Health and Diagnostics --> then select Logging Tools, Request Monitor and Tracing
    (keep default selected items as it is)

Request Monitor and Tracing

7. Under Internet Information Services, expand World Wide Web Services, then Security,
    then select Request Filtering

select Request Filtering

8. Under Internet Information Services, expand Web Management Tools, then select
    IIS Management Console

select     IIS Management Console


9. Press OK Button for Install All the selected features

Install All the selected features


Note :  If you still having a problem after doing above steps then

That's It.You could get rid of that issue by using above steps.

Issue No. 2 : There was a mismatch between the processor architecture ...........

mismatch between the processor architecture

Extra Issue:
  • When having above warning you cannot do debug on your MVC project's View files
  • In other words cannot put brake points

It's like below

cannot put brake points


Important Note : 



Solution :

1. Go to your MVC Web project ---> Select Properties

Select Properties


2. Then click Build Events Tab

3. After that go to Post-build event command line and type below Text there

               cd $(TargetDir)
               del msshrtmi.dll 

Post-build event command line


That's it.You're done.
  • Now you can put Brake Points on your MVC project's View files as you expected


Do You Need More Help ?  


Conclusion
  • After Installation of Azure SDK - October 2012 edition you may have some issues which I have mentioned above
  • So by using above solutions you could get rid of those annoying issues
  • Those issues may vary according to your software installations on your machine
  • If that is the case then you can use above mentioned links for get more details

I hope this helps to You.Comments and feedback greatly appreciated.

If you feel it was a good article,Give me a +1.Thank You.



Happy Coding

Articles You May be Like

Stay Tuned: I'll be Back with Caching in Windows Azure