DCSIMG
KnockoutJs–MVVM in HTML & Javascript - Ran Wahle's blog

Ran Wahle's blog

KnockoutJs–MVVM in HTML & Javascript

KnockoutJs – MVVM in HTML & JavascriptKnockoutJs

Previously I’ve posted about MVVM and Silverlight, It seems long ago, before Microsoft has begun focusing on HTML & Javascript as a client development platform.

One of the things I’ve cherished the most about XAML related technologies development was the ability to completely decouple behavior and UI using MVVM. I didn’t know about any possibility to implement the same pattern on HTML & Javascript based applications.

In order to do so, we can now use KnockoutJS, a Javascript library based on jQuery  that allows as to write an MVVM application with HTML and Javascript.

MVVMing with KnockoutJS

The first thing we’ll look for in MVVM is how to bind data from the View-Model. Our view-Model in this case will be a Javascript section separated from our HTML and looks like it doesn’t know anything about it (as MVVM should be). The view-model can certainly be on a separate file, however we’ll lack the intellisences doing so.

 

So, How we do it?

1. Obtain jQuery, you can do it by download it or linking to it directly. Note: You cannot use  the version supplied by Visual studio 2010 because it’s minimum version is 1.4.2 (VS supplies 1.4.1)

2. Download KnockoutJs, latest javascript file. You can find the latest version on this download page.

3. Link your HTML file to both jQuery and KnockoutJs files

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <script type="text/javascript" src="Scripts/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="Scripts/knockout-latest.js"></script>
    <title></title>
</head>
<body>
<!-- Your Page layout HTML (view) here -->
 <script>
 //Your View-Model code (javascript) here
</script>
</body>
</html>

Note: The view-model code has to be after your HTML content.
After having our page set-up, let’s have some javascript at the view model.

on our HTML content let’s make a span, and bind it’s text content to a property in the view-model:

<span>The time now is : </span>
<span data-bind="text: today"></span>

Let’s look at our view-model code. There we’ll create a view-model, add a data member called “today” (which we’ve bind our span text to) and with a simple javascript code have it updated every second

  var viewModel =
    {
        today: ko.observable(),
        updateDate: function () {
            viewModel.today(new Date());
          }
       }
    }
 
    //Init data
    viewModel.today(new Date());
    // call updateDate every second
    setInterval(viewModel.updateDate, 1000);
    //Sets the page's data context
    ko.applyBindings(viewModel);

Now, when browsing our page, we’ll see the clock at the span.

Summary:

This is, of course, a very simple example of how we can bind, but imagine the possibilities now of having an entire application with javascript and HTML, contacting the server through AJAX and not through for submission. You can now separate completely your javascript code from your UI design just as you did in WPF/Silverlight working with XAML & C#.

Of course, there’s still need for templates, handle events such as button click or text changes but this is for later posts.

kick it on DotNetKicks.com

Comments

Ran Wahle's blog said:

KnockoutJS–The Observables On my previous post , I’ve demonstrated a simple example of data binding between

# November 24, 2011 12:55 PM

Ran Wahle's blog said:

After being introduced to KnockoutJS concept and go to know the basics of data-binding with Observable and observableArray, let’s move on to how to work with templates and our collection to them. In this post I’ll explain how to bind a collection with

# December 1, 2011 4:04 PM

Ran Wahle's blog said:

On this post I’ll dive, not deeply, to the javascript on our view-model and explain some of it’s basics, the ones that causes it to act as a real view-model.

# December 1, 2011 4:18 PM

KnockoutJs–MVVM in HTML & Javascript - Ran Wahle's blog | knockout beginner | Scoop.it said:

Pingback from  KnockoutJs&ndash;MVVM in HTML &amp; Javascript - Ran Wahle's blog | knockout beginner | Scoop.it

# October 14, 2012 11:59 AM

Ran Wahle's blog said:

Recently I’ve begun testing ASP.NET.SignalR, an open source project that supports real-time web functionality in our application.SignalR logo SignalR can be added to an existing ASP.NET application and help as easily gain real-time functionality in no

# December 29, 2012 1:47 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: