<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Code, life and the universe</title><link>http://www.dreamfree.co.uk:80/</link><description>Code, life and the universe</description><item><title>Delivering a knockout.js punch</title><link>http://www.dreamfree.co.uk:80/delivering-a-knockout.js-punch</link><description>&lt;p&gt;The code:&amp;nbsp;&lt;a title="knockoutExample.zip" href="http://www.dreamfree.co.uk/media/default/downloads/knockoutExample.zip"&gt;knockoutExample.zip&lt;/a&gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;This post explains how knockout.js can greatly simplfy the creation of some fairly complex UI interactions.&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;I was recently working on a web page that had a file upload form and a fairly complex country selector. As I am sure you know, if you post back on the form after selecting a file to upload, the file uploader will be blanked. This by design and is to ensure security. The problem was the country selector, which was also required on the form, needed to post back. I eventually solved the problem with some fairly nasty jQuery code. Since then, however, I have discovered knockout.js&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;Knockout.js is all about 'simplifying dynamic javascript UIs by applying the Model-View-View Model (MVVM) pattern'. For my first post I thought I would show you my first faultering steps towards being a knockout.js guru (a long way to go I feel).&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;strong&gt;The spec&lt;/strong&gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;The user of the form needs to be able to specify if the file being uploaded should be available to anyone from anywhere or only available to people from particular countries. For simplicity, this screen shot of the form is just of the country selector.&lt;/p&gt;
&lt;p class="separator" style="text-align: center; clear: both; font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;a style="margin-left: 1em; margin-right: 1em;" href="http://3.bp.blogspot.com/-wK8QA4SJ92c/TiqpFtg780I/AAAAAAAAAA0/JbXSo-aIiIs/s1600/knockout1.jpg"&gt;&lt;img style="cursor: move;" src="http://3.bp.blogspot.com/-wK8QA4SJ92c/TiqpFtg780I/AAAAAAAAAA0/JbXSo-aIiIs/s400/knockout1.jpg" border="0" alt="" width="400" height="250" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;strong&gt;To the code!&lt;/strong&gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;First download the latest js files from the&amp;nbsp;&lt;a href="http://knockoutjs.com/documentation/installation.html"&gt;knockout&lt;/a&gt;&amp;nbsp;website. Its then just a simple process of&amp;nbsp;referencing&amp;nbsp;the files in your page. For simplicity here I put the js files in the same folder as my html page.&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="separator" style="text-align: -webkit-auto; clear: both; font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;lt;head&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;&amp;lt;title&amp;gt;Knockout example&amp;lt;/title&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;&amp;lt;script type='text/javascript' src='./jquery-1.6.1.min.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;&amp;lt;script type='text/javascript' src='./jquery.tmpl.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;&amp;lt;script type='text/javascript' src='./knockout-1.2.1.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;lt;/head&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;The next stage was to build up the html of the page. At the start it looked something like this:&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;lt;body&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;&amp;lt;h1&amp;gt;Country selector&amp;lt;/h1&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;ul&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;li&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;span &amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;label&amp;gt; All countries &amp;lt;input type="radio" name="countryOptionGroup" value="allCountries"/&amp;gt;/label&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/span&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/li&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;li&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;span class="filter"&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;input type="radio" name="countryOptionGroup" value="specificCountries" /&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/span&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;label&amp;gt;specific countries&amp;lt;/label&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/li&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;/ul&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;select&amp;gt;&amp;lt;/select&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;input type="button" value="Add" title="Add a country"/&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;h2&amp;gt;Selected ()&amp;lt;/h2&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;ul&amp;gt;&amp;lt;/ul&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;lt;/body&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;Given this shell we need to add the knockout magic to make it all work. In essence we build a javascript object model that describes the data and the methods we can perform and 'bind' it to the UI. Starting with the selector we would need to bind a country list to it.&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;strong&gt;The start of the View-Model&lt;/strong&gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;lt;script type='text/javascript'&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;/*&amp;lt;![CDATA[*/&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // First define a thing object called countryName with a property of 'name'&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; function country(countryName){&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return{&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; name: ko.observable(countryName)&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; };&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;// Next define an object called countrySelecter to house the main properties, data and methods&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;var countrySelecter = {&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;// Properties&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;selectedOptionValue: ko.observable("allCountries"),&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;availableCountries: ko.observableArray([new country("Afghanistan"), new country("Albania"), new country("Algeria"), new country("American Samoa"), new country("Andorra"), new country("Angola"), new country("Anguilla"), new country("Antigua and Barbuda"), new country("Argentina"), new country("Armenia"), new country("Aruba"), new country("Australia")]),&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;selectedCountry: ko.observable(),&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;selectedCountries: ko.observableArray()&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; };&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;// This line applies the bindings&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;ko.applyBindings(countrySelecter); // This makes Knockout get to work&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;/*]]&amp;gt;*/&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span style="white-space: pre;"&gt; &lt;/span&gt;&amp;lt;/script&amp;gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;strong&gt;Databinding&lt;/strong&gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;At this point we have a set of object, properties and methods which are all tied into the binding mechanism. For this example I will bind the initial country list to the 'select' we have on the page. To do this edit the 'select' html in the following way:&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;lt;select data-bind="options: availableCountries, optionsText: 'name', value: selectedCountry"&amp;gt;&amp;lt;/select&amp;gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;The magic here is in the data-bind section. When you closely at it you can see how the&amp;nbsp;mechanics&amp;nbsp;of knockout.js work. The options are bound to the avaialbleCountries list on the countrySelecter object. The selected text is bound to the 'name' property of the countryName object. Finaly the value part is bound to the selectedCountry property. All very elegant. Form that point you can build up the view-model to create the interaction required.&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;strong&gt;Adding methods&lt;/strong&gt;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;Now that we have a select populated with countries we need to act when the add button is pressed. For this example we are going to do the following things at that point.&lt;/p&gt;
&lt;p style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol style="font-family: 'Times New Roman'; font-size: medium;"&gt;
&lt;li&gt;Add the selected country to a list of selectedCountries and display them in an unordered list.&lt;/li&gt;
&lt;li&gt;We are going to display a count of selected countries. (think of the faff involved in doing that with jQuery).&lt;/li&gt;
&lt;li&gt;We are going to update the&amp;nbsp;available&amp;nbsp;countries list and remove the selected item.&lt;/li&gt;
&lt;li&gt;We are going to make the selected item a link where the user can change there mind&lt;/li&gt;
&lt;li&gt;We are going to manage the two radio buttons to ensure they are set&amp;nbsp;appropriately&lt;/li&gt;
&lt;/ol&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;So the next thing we need is a method for taking care of the 'Add' process. Here it is:&lt;/div&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; countrySelecter.addCountry = function () {&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.selectedOptionValue("specificCountries");&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.selectedCountries.push(this.selectedCountry());&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.availableCountries.remove(this.selectedCountry());&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.selectedCountries.sort();&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/div&gt;
&lt;/div&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;By using the '.' at the end of the object name I am able to attach a method to my countrySelecter object. In this method we see how to play with arrays. Notice that there is no messing&amp;nbsp;around&amp;nbsp;counting items in any lists and finding elements in the DOM to update. All that is taken care of through binding. So to add the count functionality all I needed to do was to update my HTML like this:&lt;/div&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;&amp;lt;h2&amp;gt;Selected (&amp;lt;span data-bind="text: selectedCountries().length"&amp;gt;&amp;lt;/span&amp;gt;)&amp;lt;/h2&amp;gt;&lt;/div&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;Knockout manages the bindings &amp;nbsp;for me and ensures all the&amp;nbsp;interested elements are&amp;nbsp;updated as the view-model is updated. So to ensure the count is up displayed all I need do is tell the span to bind its text property to the length of the selectedCountries array. The updates are handled for me! Doing the same thing in jQuery is clearly possible but requires a lot more effort and debugging.&lt;/div&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;&lt;strong&gt;jQuery templates and binding&lt;/strong&gt;&lt;/div&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;To show off further binding I used a jQuery template to display the selected list. To add it in replace the &amp;lt;ul&amp;gt;&amp;lt;/ul&amp;gt; in the original HTML with the following:&lt;/div&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;
&lt;div&gt;&amp;lt;ul data-bind="template: {name:'selectedCountriesTemplate', foreach: selectedCountries}"&amp;gt;&amp;lt;/ul&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;lt;script id='selectedCountriesTemplate' type='text/html'&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;li&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;a href="" data-bind="click: remove"&amp;gt; ${ name } &amp;lt;/a&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/li&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;lt;/script&amp;gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;Here we have the binding section in the &amp;lt;ul&amp;gt; and the template in the script. This illustrates how to bind to methods, in this case the 'remove' function. We also see the handy 'foreach' iterator in the binding section of the &amp;lt;ul&amp;gt;. There is loads more to learn about templates but on of the&amp;nbsp;really&amp;nbsp;smart things is that the binding mechanism only updates the bits that change leading to a highly&amp;nbsp;performant&amp;nbsp;page capable of handling large data sets. I have used another way of adding methods to view-model objects to illustrate how the remove function works. I've added it directly into the countryName object like follows:&lt;/div&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;
&lt;div&gt;function country(countryName){&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return{&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; name: ko.observable(countryName),&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; remove: function(){&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; countrySelecter.selectedCountries.remove(this);&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; countrySelecter.availableCountries.push(this);&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; countrySelecter.availableCountries.sort();&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(countrySelecter.selectedCountries().length == 0) {&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; countrySelecter.selectedOptionValue("allCountries");&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/div&gt;
&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; };&lt;/div&gt;
&lt;/div&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/div&gt;
&lt;div style="font-family: 'Times New Roman'; font-size: medium;"&gt;The knockout.js library is well worth a second look. I have only scratched the surface in the mini example but it appears to be a clean&amp;nbsp;powerful and quick way to put together some intricate UI interactions. Do checkout the knockout.js website and have a look at the samples.&amp;nbsp;&amp;nbsp;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description><pubDate>Mon, 25 Jul 2011 13:57:43 GMT</pubDate><guid isPermaLink="true">http://www.dreamfree.co.uk:80/delivering-a-knockout.js-punch</guid></item></channel></rss>
