<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">

  <title><![CDATA[Knock Me Out]]></title>
  
  <link href="http://www.knockmeout.net/" />
  <updated>2013-05-17T07:58:22-05:00</updated>
  <id>http://www.knockmeout.net/</id>
  <author>
    <name><![CDATA[Ryan Niemeyer]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/KnockMeOut" /><feedburner:info uri="knockmeout" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <title type="html"><![CDATA[Working with AMD modules in Knockout.js]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/dMnS9RZv_rQ/knockout-amd-helpers-plugin.html" />
    <updated>2013-05-01T22:35:00-05:00</updated>
    <id>http://www.knockmeout.net/2013/05/knockout-amd-helpers-plugin</id>
    <content type="html">&lt;p&gt;After being a bit resistant at first, I have happily moved to using &lt;a href="http://requirejs.org"&gt;require.js&lt;/a&gt; to manage dependencies in most of the &lt;a href="http://knockoutjs.com"&gt;Knockout.js&lt;/a&gt; applications that I have written in the last year or so. With each application, I have tried a number of patterns to make it easier to work with &lt;a href="https://github.com/amdjs/amdjs-api/wiki/AMD"&gt;AMD&lt;/a&gt; (Asynchronous Module Definition) modules. Recently, I decided to formalize some of these patterns into a lightweight plugin that makes it simple to bind against modules and pull in templates that live in external files.&lt;/p&gt;

&lt;p&gt;The result is this library: &lt;strong&gt;&lt;a href="https://github.com/rniemeyer/knockout-amd-helpers"&gt;knockout-amd-helpers&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;Why require.js and AMD modules&lt;/h2&gt;

&lt;p&gt;Breaking an application down into small, decoupled modules is a pattern that works well when done right. Creating modules in JavaScript is often accomplished by using various namespacing patterns. As an application grows though, it is easy to lose sight of the individual dependencies of each module and eventually maintaining the order that all of your scripts need to be loaded in becomes a burden as well. AMD module loaders can help with these issues.&lt;/p&gt;

&lt;p&gt;There are plenty of good resources that help explain require.js, dependency management, and AMD modules. Here are a few good ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The require.js documentation on &lt;a href="http://requirejs.org/docs/whyamd.html"&gt;AMD modules&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Our &lt;a href="http://knockoutjs.com/documentation/amd-loading.html"&gt;documentation&lt;/a&gt; on using Knockout with require.js.&lt;/li&gt;
&lt;li&gt;My co-worker &lt;a href="https://twitter.com/jcreamer898"&gt;Jonathan Creamer&lt;/a&gt;&amp;#8217;s &lt;a href="http://net.tutsplus.com/tutorials/javascript-ajax/building-large-maintainable-and-testable-knockout-js-applications/"&gt;tutorial&lt;/a&gt; on using Knockout.js in large-scale applications. He also recently did a presentation for &lt;a href="http://live.dotnetconf.net/2013-04"&gt;dotnetConf&lt;/a&gt; that can be found &lt;a href="http://www.youtube.com/watch?v=EXqyZ7k1lnw"&gt;here&lt;/a&gt; on using require.js and using it with ASP.NET MVC.&lt;/li&gt;
&lt;li&gt;While require.js is the most mentioned option in this category, there are others including the excellent &lt;a href="https://github.com/cujojs/curl"&gt;curl.js&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;A few pain points&lt;/h2&gt;

&lt;p&gt;When developing Knockout applications using require.js, there are a couple of situations that out-of-the-box still seemed less than perfect to me.&lt;/p&gt;

&lt;p&gt;1- After breaking down your application into specific modules, it is unfortunate to have to include all of your markup on a single page. There are server-side solutions to pull in partials for your templates, but that still results in a page that contains all of your markup from the start. These is also the &lt;a href="https://github.com/ifandelse/Knockout.js-External-Template-Engine"&gt;external template engine&lt;/a&gt;, which works well, but is not designed to leverage the capabilities of the AMD loader libraries.&lt;/p&gt;

&lt;p&gt;2- Dynamically requesting and binding against modules is not trivial. Normally, you would build a main &amp;#8220;App&amp;#8221; view model that contains all of the sub-modules that you want to bind against. It would be nice though to be able to dynamically pull in modules or use modules as reusable components in an app under any context.&lt;/p&gt;

&lt;h2&gt;Looking at potential solutions&lt;/h2&gt;

&lt;p&gt;Based on some of the different approaches that I have used in the past, I created the &lt;a href="https://github.com/rniemeyer/knockout-amd-helpers"&gt;knockout-amd-helpers&lt;/a&gt; plugin to hopefully help ease these issues.&lt;/p&gt;

&lt;h3&gt;An AMD compatible template engine&lt;/h3&gt;

&lt;p&gt;The first feature of the plugin is to use one of Knockout&amp;#8217;s extensibility points to replace the default template engine with one that is able to load named templates using the AMD loader&amp;#8217;s &lt;code&gt;text&lt;/code&gt; plugin.&lt;/p&gt;

&lt;p&gt;I used the template sources extensibility point that I discussed &lt;a href="http://www.knockmeout.net/2011/10/ko-13-preview-part-3-template-sources.html"&gt;here&lt;/a&gt;. When a named template is requested, it first checks to see if there is a &lt;code&gt;script&lt;/code&gt; tag with the specified id (the default engine actually will grab any element with that id) and if not uses the text plugin to require the template. This will asynchronously load the template (unless it has already been loaded or is bundled and already available on the client). The template source uses an observable that triggers the &lt;code&gt;template&lt;/code&gt; binding to update when the template is available.&lt;/p&gt;

&lt;p&gt;For example, when using a binding like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;template: { name: &amp;#39;itemView&amp;#39;, foreach: items }&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;If there is no script tag with an id of &lt;code&gt;itemView&lt;/code&gt;, it will attempt to load the template. The engine uses a default path of &lt;code&gt;templates&lt;/code&gt; and a default suffix of &lt;code&gt;.tmpl.html&lt;/code&gt;.  So, it would look for the template at &lt;code&gt;templates/itemView.tmpl.html&lt;/code&gt;.  The default path and suffix can be configured and you can also pass a more specific path in for the name (&lt;code&gt;sub/path/itemView&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;A module binding&lt;/h3&gt;

&lt;p&gt;This updated template engine now helps keep your templates as modular as your view models, but it still would be nice to easily pull modules into an application without using a top-level view model that needs to contain everything that you might want to bind against.&lt;/p&gt;

&lt;p&gt;To help with this situation, the plugin includes a &lt;code&gt;module&lt;/code&gt; binding, which offers a flexible way to dynamically pull a module into your markup. Here is a basic example:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;nav&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;module: &amp;#39;navigation&amp;#39;&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This will require a &lt;code&gt;navigation&lt;/code&gt; module (the base directory can be configured as well).  If the main element had children, then it would use them as an inline/anonymous template. However, in this case, since the element does not have children, it will use &lt;code&gt;navigation&lt;/code&gt; as the template name and follow the template engine&amp;#8217;s rules for pulling in the template.&lt;/p&gt;

&lt;p&gt;After the loading the module it will follow a few rules for deciding what data to bind against:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the module returns a function, then it will create a new instance&lt;/li&gt;
&lt;li&gt;If the module returns an object, then it will, by default, call an &lt;code&gt;initialize&lt;/code&gt; function on the object (if it exists) and either use the result of the function or the original object if there is not a return value.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;This gives you the flexibility of either constructing a new object, using an existing object, or calling a function that returns some object to bind against.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;module&lt;/code&gt; binding has a number of options that you can pass in as well. Here is an example passing all of the options:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;module: { name: &amp;#39;one&amp;#39;, data: initialData, template: &amp;#39;oneTmpl&amp;#39;,&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="s"&gt;                              initializer: &amp;#39;createItem&amp;#39;, afterRender: myCallback }&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;These options let you do things like customize the template to use, specify data to pass into the constructor or initializer, define the name of the function to call, and pass an afterRender function through to the &lt;code&gt;template&lt;/code&gt; binding.&lt;/p&gt;

&lt;p&gt;With this binding, it is possible to call &lt;code&gt;ko.applyBindings({})&lt;/code&gt; and build your application strictly using the &lt;code&gt;module&lt;/code&gt; binding. A &lt;code&gt;module&lt;/code&gt; binding can be nested inside other &lt;code&gt;module&lt;/code&gt; bindings and you can also use observables to dynamically specify your module.&lt;/p&gt;

&lt;h3&gt;Communicating between modules&lt;/h3&gt;

&lt;p&gt;When building an app using this structure, it would be less than ideal to rely on &lt;code&gt;$root&lt;/code&gt; or &lt;code&gt;$parent&lt;/code&gt; calls within the markup to communicate between modules. This can potentially couple your module to only working within a certain context. While this can certanily work, a better solution may be to use some type of messaging to communicate between the modules.&lt;/p&gt;

&lt;p&gt;I have a library called &lt;a href="https://github.com/rniemeyer/knockout-postbox/"&gt;knockout-postbox&lt;/a&gt; that adds a &lt;code&gt;ko.postbox&lt;/code&gt; object and some observable extensions that make it easy to publish values on a topic and update an observable based on a subscription to a topic.&lt;/p&gt;

&lt;p&gt;For example, in a module that defines the main content, you could have an observable like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sectionName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;subscribeTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;navigation.current&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Then, in the navigation module, publish on that topic like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedNavItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;publishOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;navigation.current&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;The postbox library supports a number of options that control how the publish/subscribe process works, but if you are looking for a stand-alone library that supports a number of additional features (channels, wildcards, envelopes), then I would recommend &lt;a href="https://github.com/postaljs/postal.js"&gt;postal.js&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;If you are using AMD modules in your Knockout application and are looking for a lightweight and simple, but flexible way to bind against templates and modules, then check out this new &lt;a href="https://github.com/rniemeyer/knockout-amd-helpers"&gt;library&lt;/a&gt;. It has been tested with both require.js and curl.js. I would be happy to help support any other AMD loaders, if there is interest. Please check out the README on the repository for additional documentation. I also plan to work on a better example, as time permits.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/dMnS9RZv_rQ" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2013/05/knockout-amd-helpers-plugin.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Joining appendTo]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/v6Rkau2RIUs/joining-appendto.html" />
    <updated>2013-03-28T09:35:00-05:00</updated>
    <id>http://www.knockmeout.net/2013/03/joining-appendto</id>
    <content type="html">&lt;p&gt;Just a quick note that I joined &lt;a href="http://appendTo.com"&gt;appendTo&lt;/a&gt; as a senior JavaScript engineer at the beginning of March. I am really excited to work with a great group of talented people and to get a chance to work on a wide variety of web development projects. I am especially eager to use some new technologies/libraries to solve real problems and can&amp;#8217;t wait to gain new perspectives on ways to improve or build on top of &lt;a href="http://knockoutjs.com"&gt;Knockout&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you are looking for help with a significant Knockout project (or any other front-end technology), then I would encourage you to &lt;a href="http://appendto.com/contact"&gt;contact&lt;/a&gt; appendTo and maybe we could have the chance to work together. For general KO questions or if you are looking for help on a project that only requires a small time investment, feel free to contact me directly.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/v6Rkau2RIUs" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2013/03/joining-appendto.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Tekpub Refactoring Knockout.js Screencast]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/_d3Juzch474/tekpub-knockoutjs-refactoring.html" />
    <updated>2013-02-21T20:35:00-06:00</updated>
    <id>http://www.knockmeout.net/2013/02/tekpub-knockout-video</id>
    <content type="html">&lt;p&gt;Recently, I had the chance to do some pair programming with &lt;a href="https://twitter.com/robconery"&gt;Rob Conery&lt;/a&gt; to refactor a &lt;a href="http://knockoutjs.com"&gt;Knockout&lt;/a&gt;-based shopping cart for a &lt;a href="http://tekpub.com"&gt;Tekpub&lt;/a&gt; full throttle &lt;a href="http://tekpub.com/products/knockout_refactor"&gt;video&lt;/a&gt;. Rob has had a love/hate relationship with Knockout over the years and I have had several discussions with him in the past trying to work through some of his concerns.&lt;/p&gt;

&lt;p&gt;He recently asked me to take a look at a Knockout-based shopping cart that he had written for another video. The code was working just fine, but as I dug into it, I started jotting down a &lt;em&gt;LOT&lt;/em&gt; of notes. I ended up formatting them in markdown and sent them Rob&amp;#8217;s way. He thought that it would be a good idea to record a screencast to really dig into those notes.&lt;/p&gt;

&lt;p&gt;Here is a trailer for the &lt;a href="http://tekpub.com/products/knockout_refactor"&gt;screencast&lt;/a&gt;:&lt;/p&gt;

&lt;iframe src="http://player.vimeo.com/video/60131454" width="500" height="313" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen&gt;&lt;/iframe&gt;


&lt;p&gt;&lt;a href="http://vimeo.com/60131454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I personally have had a Tekpub subscription for around two years and definitely recommend the service. They are really putting out some great content lately and have a well-done Knockout &lt;a href="http://tekpub.com/productions/knockout"&gt;series&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; here is a &lt;a href="https://gist.github.com/rniemeyer/a715efc49acb8169a3e2"&gt;link&lt;/a&gt; to the notes that I sent Rob.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/_d3Juzch474" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2013/02/tekpub-knockoutjs-refactoring.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[A Simple Editor Pattern for Knockout.js]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/lM_ZFCyg_IY/simple-editor-pattern-knockout-js.html" />
    <updated>2013-01-28T20:35:00-06:00</updated>
    <id>http://www.knockmeout.net/2013/01/simple-editor-pattern-knockoutjs</id>
    <content type="html">&lt;p&gt;Implementing an editor that allows users to accept or cancel their changes is a common task in &lt;a href="http://knockoutjs.com"&gt;Knockout.js&lt;/a&gt;. Previously, I &lt;a href="http://www.knockmeout.net/2011/03/guard-your-model-accept-or-cancel-edits.html"&gt;suggested&lt;/a&gt; the idea of a &lt;code&gt;protectedObservable&lt;/code&gt; that is an extended observable with the ability to commit and reset the value being edited. Since Knockout 2.0, I would probably now implement that functionality using either &lt;a href="http://knockoutjs.com/documentation/extenders.html"&gt;extenders&lt;/a&gt; or by augmenting the &lt;code&gt;.fn&lt;/code&gt; &lt;a href="http://knockoutjs.com/documentation/fn.html"&gt;object&lt;/a&gt; of the core types. However, I now use a different pattern.&lt;/p&gt;

&lt;p&gt;I described this pattern in the Twin Cities Code Camp presentation &lt;a href="http://www.knockmeout.net/2012/10/twincitiescodecamp-2012-session.html"&gt;here&lt;/a&gt;. This technique allows you to easily copy, commit, and revert changes to an entire model. There are two rules that make this pattern work:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The creation of observables and computeds needs to be separate from actually populating them with values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The format of the data that you put in should be the same as the format that you are able to get out. Typically this means that calling &lt;code&gt;ko.toJS&lt;/code&gt; on your model should result in an object that you could send back through the function described in step 1 to re-populate the values.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;Separate creation and initialization&lt;/h2&gt;

&lt;p&gt;The idea is that we can apply fresh data to our object at anytime, so our constructor function should just create our structure, while a separate method handles setting the values.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;new item&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Our &lt;code&gt;update&lt;/code&gt; function (call it whatever you like &lt;code&gt;init&lt;/code&gt;, &lt;code&gt;initialize&lt;/code&gt;, &lt;code&gt;hydrate&lt;/code&gt;, etc.), needs to handle populating the values of all of the observables given a plain JavaScript object. It can also handle supplying default values.&lt;/p&gt;

&lt;h2&gt;Data in matches data out&lt;/h2&gt;

&lt;p&gt;To make this pattern work, we need to be able to put a plain JavaScript object in and get the equivalent object out. Ideally, simply calling &lt;code&gt;ko.toJS&lt;/code&gt; on our model, will give us the plain object that we need. It is okay, if there are some additional computeds/properties on the object, but it needs to be appropriate for sending through the &lt;code&gt;update&lt;/code&gt; function again.&lt;/p&gt;

&lt;p&gt;A technique that I use frequently to &amp;#8220;hide&amp;#8221; data that I won&amp;#8217;t want when I turn the model into a plain JavaScript object or to JSON is to use a &amp;#8220;sub-observable&amp;#8221;. So, if we had a computed observable to show a formatted version of the price that we don&amp;#8217;t want in our output, we could specify it like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;formatted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getFormattedPrice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Since observables are functions and functions are objects that can have their own properties, it is perfectly valid to create a &lt;code&gt;formatted&lt;/code&gt; computed off of the &lt;code&gt;price&lt;/code&gt; observable. In the markup, we can bind against &lt;code&gt;price.formatted&lt;/code&gt;. However, when calling &lt;code&gt;ko.toJS&lt;/code&gt; or &lt;code&gt;ko.toJSON&lt;/code&gt;, the &lt;code&gt;formatted&lt;/code&gt; sub-observable will disappear, as we will simply be left with a &lt;code&gt;price&lt;/code&gt; property and value.&lt;/p&gt;

&lt;h2&gt;Reverting changes&lt;/h2&gt;

&lt;p&gt;Now that we have these pieces in place, it is easy to create an editor that allows reverting changes. In the event that a user chooses to cancel their editing, all we need to do is send the original data back through our &lt;code&gt;update&lt;/code&gt; function and we will be back to where we started.&lt;/p&gt;

&lt;p&gt;We will need to track the original data, so that it is available to repopulate the model. A good place to cache this data is in the &lt;code&gt;update&lt;/code&gt; function itself. It is useful to make sure that the data is &amp;#8220;hidden&amp;#8221; as well, so multiple edits don&amp;#8217;t result in recursive versions of the old data being kept around. If we were not using the prototype, then we could just use a local variable to store the data, but since we are placing our shared functions on the prototype in these samples, we need to find another way to hide this data. A simple solution is to create an empty &lt;code&gt;cache&lt;/code&gt; function and store our data as property off of it. This will prevent calls to &lt;code&gt;ko.toJS&lt;/code&gt; or &lt;code&gt;ko.toJSON&lt;/code&gt; from capturing this data.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;span class='line-number'&gt;14&lt;/span&gt;
&lt;span class='line-number'&gt;15&lt;/span&gt;
&lt;span class='line-number'&gt;16&lt;/span&gt;
&lt;span class='line-number'&gt;17&lt;/span&gt;
&lt;span class='line-number'&gt;18&lt;/span&gt;
&lt;span class='line-number'&gt;19&lt;/span&gt;
&lt;span class='line-number'&gt;20&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;      &lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;new item&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="c1"&gt;//save off the latest data for later use&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;latestData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;      &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;      &lt;span class="nx"&gt;revert&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;latestData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;In this scenario, we will let edits persist to our observables and in the event that a user chooses to cancel, we will refresh our model with the original data.&lt;/p&gt;

&lt;h2&gt;Committing changes&lt;/h2&gt;

&lt;p&gt;When a user accepts the data, we need to make sure that we update the cached data with the current state of the model. For example, we could simply add a &lt;code&gt;commit&lt;/code&gt; or &lt;code&gt;accept&lt;/code&gt; function to our prototype that does:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;commit&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;latestData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toJS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;We now have the latest data cached, so the next time that a user cancels it will be reverted back to this updated state.&lt;/p&gt;

&lt;h2&gt;Copying/cloning&lt;/h2&gt;

&lt;p&gt;Sometimes in an editor, we would not want the currently edited observables to affect the rest of the UI until the user chooses to accept the changes. In this case, we can use these same methods to create a copy of the item for editing and then apply it back to the original. Here is how our overall view model might look:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;span class='line-number'&gt;14&lt;/span&gt;
&lt;span class='line-number'&gt;15&lt;/span&gt;
&lt;span class='line-number'&gt;16&lt;/span&gt;
&lt;span class='line-number'&gt;17&lt;/span&gt;
&lt;span class='line-number'&gt;18&lt;/span&gt;
&lt;span class='line-number'&gt;19&lt;/span&gt;
&lt;span class='line-number'&gt;20&lt;/span&gt;
&lt;span class='line-number'&gt;21&lt;/span&gt;
&lt;span class='line-number'&gt;22&lt;/span&gt;
&lt;span class='line-number'&gt;23&lt;/span&gt;
&lt;span class='line-number'&gt;24&lt;/span&gt;
&lt;span class='line-number'&gt;25&lt;/span&gt;
&lt;span class='line-number'&gt;26&lt;/span&gt;
&lt;span class='line-number'&gt;27&lt;/span&gt;
&lt;span class='line-number'&gt;28&lt;/span&gt;
&lt;span class='line-number'&gt;29&lt;/span&gt;
&lt;span class='line-number'&gt;30&lt;/span&gt;
&lt;span class='line-number'&gt;31&lt;/span&gt;
&lt;span class='line-number'&gt;32&lt;/span&gt;
&lt;span class='line-number'&gt;33&lt;/span&gt;
&lt;span class='line-number'&gt;34&lt;/span&gt;
&lt;span class='line-number'&gt;35&lt;/span&gt;
&lt;span class='line-number'&gt;36&lt;/span&gt;
&lt;span class='line-number'&gt;37&lt;/span&gt;
&lt;span class='line-number'&gt;38&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;ViewModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="c1"&gt;//turn the raw items into Item objects&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observableArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arrayMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="c1"&gt;//hold the real currently selected item&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="c1"&gt;//make edits to a copy&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;itemForEditing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="c1"&gt;//select and item and make a copy of it for editing&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;selectItem&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;itemForEditing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toJS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;acceptItem&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;selected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedItem&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;                &lt;span class="nx"&gt;edited&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toJS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;itemForEditing&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;//clean copy of edited&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="c1"&gt;//apply updates from the edited item to the selected item&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="nx"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;edited&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="c1"&gt;//clear selected item&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;itemForEditing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="c1"&gt;//just throw away the edited item and clear the selected observables&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;revertItem&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;itemForEditing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;So, we make edits to a copy of the original and on an accept we apply those edits back to the original. We can use &lt;code&gt;ko.toJS&lt;/code&gt; to get a plain JavaScript object and feed it into our &lt;code&gt;update&lt;/code&gt; function to apply the changes. In this scenario, the &lt;code&gt;revertItem&lt;/code&gt; function can simply throw away the copied item and clear our the selected value.&lt;/p&gt;

&lt;iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/K3gJT/embedded/result,js,html,css/light/"&gt;&lt;/iframe&gt;


&lt;p&gt;&lt;a href="http://jsfiddle.net/rniemeyer/K3gJT/"&gt;Link to sample on jsFiddle.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you were going to reuse this technique often, then you could even create an extension to an observableArray that adds the appropriate observables and functions off of the observableArray itself like in this &lt;a href="http://jsfiddle.net/rniemeyer/MQVr3/"&gt;sample&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Updating from server&lt;/h2&gt;

&lt;p&gt;Our &lt;code&gt;update&lt;/code&gt; function is also a handy way to apply updates from the server to our existing view model. In our case, we would likely need to add an &lt;code&gt;id&lt;/code&gt; to the &lt;code&gt;Item&lt;/code&gt; objects, so that we can identify which object needs updating. This works very well with something like &lt;a href="http://signalr.net"&gt;SignalR&lt;/a&gt; or &lt;a href="http://socket.io/"&gt;socket.io&lt;/a&gt; to keep various clients in sync with each other. This is an easier pattern than trying to swap items in an array or update specific properties one by one.&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;Editors with accept/cancel options are a common scenario in a Knockout.js application. By separating the creation of observables/computeds from poplating their values, we can commit, reset, and update models by feeding a version of the data through our &lt;code&gt;update&lt;/code&gt; function. When you think in terms of the plain JS object that you can get out of or put into a model, it makes many of these scenarios easy to handle.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/lM_ZFCyg_IY" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2013/01/simple-editor-pattern-knockout-js.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Revisting Event Delegation in Knockout.js]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/aIQCmhhpNPw/revisit-event-delegation-in-knockout-js.html" />
    <updated>2012-11-19T20:35:00-06:00</updated>
    <id>http://www.knockmeout.net/2012/11/revisiting-event-delegation-in-knockoutjs</id>
    <content type="html">&lt;p&gt;Previously, I &lt;a href="http://www.knockmeout.net/2011/04/event-delegation-in-knockoutjs.html"&gt;discussed&lt;/a&gt; a technique to do event delegation in &lt;a href="http://knockoutjs.com"&gt;Knockout.js&lt;/a&gt;. That post was created before Knockout 2.0, which added the ability to use &lt;code&gt;ko.dataFor&lt;/code&gt; and &lt;code&gt;ko.contextFor&lt;/code&gt; to retrieve the data context related to a specific element (as described &lt;a href="http://knockoutjs.com/documentation/unobtrusive-event-handling.html"&gt;here&lt;/a&gt;).  With these new tools, there are now much better ways to add a single handler on a parent element that can respond to events triggered on its child elements.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: based on the techniques in this post, I created a plugin called &lt;code&gt;knockout-delegatedEvents&lt;/code&gt; located here: &lt;a href="https://github.com/rniemeyer/knockout-delegatedEvents"&gt;https://github.com/rniemeyer/knockout-delegatedEvents&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Why event delegation in Knockout.js?&lt;/h2&gt;

&lt;p&gt;Currently when you use the &lt;code&gt;click&lt;/code&gt; or &lt;code&gt;event&lt;/code&gt; binding in Knockout, an event handler is attached directly to that element. Generally this is fine and causes no real problems. However, if you face a scenario where you have a large number of elements that require these bindings, then there can be a performance impact to creating and attaching these handlers to each element, especially in older browsers. For example, you may be creating a grid where each cell needs various event handlers or a hierarchical editor where you are potentially attaching handlers to interact with the parent items as well as each child (and each child may have children).&lt;/p&gt;

&lt;p&gt;In this case, there are advantages to using event delegation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you only have to attach one (or a few) event handlers rather than one on each element. Events bubble up to the higher-level handler and can understand the original element that triggered it.&lt;/li&gt;
&lt;li&gt;dynamically added content does not need new event handlers added to them (largely not an issue with KO bindings and templating)&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Normal event delegation with KO 2.0+&lt;/h2&gt;

&lt;p&gt;With Knockout 2.0+, the common way to use event delegation is to avoid attaching event handlers in the bindings themselves and add another layer of code to attach a single handler to a parent element using something like jQuery&amp;#8217;s &lt;a href="http://api.jquery.com/on/"&gt;on&lt;/a&gt; (previously &lt;a href="http://api.jquery.com/live/"&gt;live&lt;/a&gt;/&lt;a href="http://api.jquery.com/delegate/"&gt;delegate&lt;/a&gt;). So, rather than doing:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;foreach: items&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;#&amp;quot;&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;click: $root.selectItem, text: name&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;You would instead do:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;items&amp;quot;&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;foreach: items&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;#&amp;quot;&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;text: name&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;and attach an event handler elsewhere like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;#items&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;a&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;contextFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//this is the element that was clicked&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Attaching a single handler like this works quite well. However, there are a few issues that I still have with this technique:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you have another layer of code to manage besides your view model. This is the type of code that Knockout generally helps you get away from.&lt;/li&gt;
&lt;li&gt;this code is tightly coupled (fairly) with this view&amp;#8217;s markup&lt;/li&gt;
&lt;li&gt;in our example, if the entire &lt;code&gt;ul&lt;/code&gt; is part of a template that gets swapped, then we would need to hook up our handler again or choose to add our handler at a higher level (possibly up to the &lt;code&gt;body&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;this particular code also adds a dependency on jQuery that is not really necessary&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;In my perfect Knockout application, you have your view and your view model with the only code outside of that being a call to &lt;code&gt;ko.applyBindings&lt;/code&gt;. To accomplish this, we would need to be able to wire it up declaratively in the view. I have been exploring a few ways to make this happen.&lt;/p&gt;

&lt;h2&gt;Declaratively adding a handler&lt;/h2&gt;

&lt;p&gt;The first step is attaching a handler on the parent or root element that will respond when events bubble up to it. This is pretty easy to accomplish with a custom binding. When the event is handled, we can determine the original element and use &lt;code&gt;ko.dataFor&lt;/code&gt; or &lt;code&gt;ko.contextFor&lt;/code&gt; to get the original data context and then execute some method.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;//add a handler on a parent element that reponds to events from the children&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bindingHandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;delegatedHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;valueAccessor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="c1"&gt;//array of events&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unwrapObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;valueAccessor&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arrayForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;registerEventHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;createDelegatedHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;To create the handler, we take the target element and get its context:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;span class='line-number'&gt;14&lt;/span&gt;
&lt;span class='line-number'&gt;15&lt;/span&gt;
&lt;span class='line-number'&gt;16&lt;/span&gt;
&lt;span class='line-number'&gt;17&lt;/span&gt;
&lt;span class='line-number'&gt;18&lt;/span&gt;
&lt;span class='line-number'&gt;19&lt;/span&gt;
&lt;span class='line-number'&gt;20&lt;/span&gt;
&lt;span class='line-number'&gt;21&lt;/span&gt;
&lt;span class='line-number'&gt;22&lt;/span&gt;
&lt;span class='line-number'&gt;23&lt;/span&gt;
&lt;span class='line-number'&gt;24&lt;/span&gt;
&lt;span class='line-number'&gt;25&lt;/span&gt;
&lt;span class='line-number'&gt;26&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;//create a handler for a specific event like &amp;quot;click&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;createDelegatedHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="c1"&gt;//return a handler&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="nx"&gt;action&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;findAnAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//??? how do we know what method to call&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="c1"&gt;//execute the action&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="c1"&gt;//call it like KO normally would with the data being both the value of `this` and the first arg&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="c1"&gt;//prevent the default action, unless the function returns true&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;                    &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;                &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;                &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;                    &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;returnValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;                &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;When used on an element, the binding would take in an array of events like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;delegatedHandler: [&amp;#39;click&amp;#39;, &amp;#39;mouseover&amp;#39;, &amp;#39;mousedown&amp;#39;]&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;...
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;With this binding in place, we can handle the event, find the element that triggered the event, and determine the data context of the element. However, we still need to know what method to call. I looked at a few alternatives for this piece.&lt;/p&gt;

&lt;h2&gt;Option #1 - using a binding&lt;/h2&gt;

&lt;p&gt;On the child elements, we could use a binding that stores a reference to the function to call. The binding can use Knockout&amp;#8217;s &lt;code&gt;ko.utils.domData&lt;/code&gt; functions to store and retrieve data on the element, as Knockout automatically cleans up this data whenever it removes elements from the document. For example, we could create a &lt;code&gt;delegatedClick&lt;/code&gt; binding to make this association.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;//associate a handler with an element that will be executed by the delegatedHandler&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bindingHandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;delegatedClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;valueAccessor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;valueAccessor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;domData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;ko_delegated_click&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Now, on a child element, you would associate a function like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;#&amp;quot;&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;text: name, delegatedClick: $parent.selectItem&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;We could create additional bindings for other events that we are interested in and could even create a helper function to generate these bindings given an array of events that we need to handle.&lt;/p&gt;

&lt;h3&gt;Pros&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Can simply replace &lt;code&gt;click&lt;/code&gt; bindings with &lt;code&gt;delegatedClick&lt;/code&gt; (and other events) without any other changes&lt;/li&gt;
&lt;li&gt;Can bind against specific functions and can execute code for cases where you need to call a function with a parameter to create a handler (&lt;code&gt;delegatedClick: $parent.createHandler('some_modifier')&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;Cons&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The binding on the child elements is lightweight, but we still have to pay the overhead of parsing and executing a binding.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Option #2 - use a &lt;code&gt;data-&lt;/code&gt; attribute with method name&lt;/h2&gt;

&lt;p&gt;The first solution works well, but I still do not like having to pay the price of executing bindings on the child elements. For an alternative solution, I looked at a different approach where the child elements are tagged with a &lt;code&gt;data-eventName&lt;/code&gt; attribute that contains the method name.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;#&amp;quot;&lt;/span&gt; &lt;span class="na"&gt;data-click=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;selectItem&amp;quot;&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;text: name&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;With this technique we would no longer need the child bindings. In the event handling code, we would now need to locate a method that matches our &lt;code&gt;data-eventName&lt;/code&gt; attribute value.  This time we can use &lt;code&gt;ko.contextFor&lt;/code&gt; to get back the entire &lt;a href="http://knockoutjs.com/documentation/binding-context.html"&gt;binding context&lt;/a&gt;. It is very common to call functions off of &lt;code&gt;$parent&lt;/code&gt; or &lt;code&gt;$root&lt;/code&gt; as well as off of the current data itself, so the &lt;code&gt;$parents&lt;/code&gt; array allows us to walk up the scope chain to locate an appropriate method.&lt;/p&gt;

&lt;h3&gt;Pros&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;No overhead of executing a binding on the child elements&lt;/li&gt;
&lt;li&gt;Can execute the function with the correct owner, so we do not need to use &lt;code&gt;.bind&lt;/code&gt; or &lt;code&gt;var self = this;&lt;/code&gt; to ensure that the context is correct. This is a really nice benefit, we have to search for the method in each scope, so we know the appropriate owner of the method to use as the context.&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;Cons&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;could run into issues with clashing names (child has an &lt;code&gt;add&lt;/code&gt; method, but you wanted to call parent&amp;#8217;s &lt;code&gt;add&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;cannot execute code to create a handler, as the attribute value would be a string that needs to match a method name&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Option #3 - associate method and name in root/global object&lt;/h2&gt;

&lt;p&gt;Another alternative might be to use the &lt;code&gt;data-&lt;/code&gt; attribute, but use the string to key into an object that holds the associations between names and actions. Perhaps something like &lt;code&gt;ko.actions&lt;/code&gt; or &lt;code&gt;ko.commands&lt;/code&gt;. This object could hold just function references or maybe a function and owner. For arrays of objects, we would not be able to indicate an appropriate owner, so we would have to fall back to making sure that the function is properly bound on each instance. For example, we could even give the function an alias of &lt;code&gt;select&lt;/code&gt; like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectItem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;h3&gt;Pros&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;No overhead of executing a binding on the child elements&lt;/li&gt;
&lt;li&gt;No issues with ambiguity in names, as names can be aliased&lt;/li&gt;
&lt;li&gt;Functions outside of a view model could be included&lt;/li&gt;
&lt;li&gt;Could dynamically change the definition of the action&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;Cons&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;have to manage adding (and maybe removing) functions to this object&lt;/li&gt;
&lt;li&gt;different types of objects with the same method names would have to be aliased with unique names in this object&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In cases where you need to attach a large number of event handlers, using event delegation can be a nice win. Attaching these handlers declaratively provides an alternative to wiring this up outside of your view/viewmodel. While considering each of the techniques to determine the function to execute, I was leaning towards #2, as it does not require the child bindings and as a bonus gets the context right when it calls the handler. Instead though, I decided to create a plugin that can use all of these techniques interchangeably. The plugin lives at: &lt;a href="http://github.com/rniemeyer/knockout-delegatedEvents"&gt;http://github.com/rniemeyer/knockout-delegatedEvents&lt;/a&gt;. Please let me know if you have feedback or suggestions for the plugin.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/aIQCmhhpNPw" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/11/revisit-event-delegation-in-knockout-js.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Knockout.js 2.2 is out now!]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/VkcFhAzBbBA/knockout-2-dot-2-is-out.html" />
    <updated>2012-10-29T20:35:00-05:00</updated>
    <id>http://www.knockmeout.net/2012/10/knockout-2-dot-2-is-out</id>
    <content type="html">&lt;p&gt;Just a quick note that &lt;a href="http://knockoutjs.com"&gt;Knockout&lt;/a&gt; 2.2 is now available. You can read more about the details of this release in the &lt;a href="https://groups.google.com/d/topic/knockoutjs/kP3IaZ6tqus/discussion"&gt;announcement&lt;/a&gt; or Steve Sanderson&amp;#8217;s blog &lt;a href="http://blog.stevensanderson.com/2012/10/29/knockout-2-2-0-released/"&gt;post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here are a few of my favorite changes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;css&lt;/code&gt; binding can either toggle specific classes on and off like (&lt;code&gt;css: { error: hasError, required: isRequired }&lt;/code&gt;) or now add one or more classes dynamically like &lt;code&gt;css: someClassOrClasses&lt;/code&gt;. This is handy when the class names are dynamic. Previously, you could do something similar with the &lt;code&gt;attr&lt;/code&gt; binding against the class attribute, but it would overwrite the class attribute completely. When using the &lt;code&gt;css&lt;/code&gt; binding in this way, it will now preserve existing classes and will properly remove any class or classes that were previously added by the binding when the value changes.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;ifnot&lt;/code&gt; bindings now only re-render their contents when the bound value changes between truthy and falsy. This helps to alleviate the performance issue described in the first half of this &lt;a href="http://www.knockmeout.net/2012/03/knockoutjs-performance-gotcha-1ifwith.html"&gt;post&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;There is a new &lt;code&gt;peek&lt;/code&gt; function available on &lt;code&gt;observables&lt;/code&gt; and &lt;code&gt;computed observables&lt;/code&gt;. This allows you to retrieve its value without creating a dependency. This definitely can be useful when you want tight control over when bindings and other subscriptions are triggered, but I would use caution as it does circumvent the normal dependency tracking mechanism. If you find that you need this functionality, then you probably have a good reason why you don&amp;#8217;t want a dependency and would understand what does and doesn&amp;#8217;t trigger updates.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;foreach&lt;/code&gt; binding and the &lt;code&gt;foreach&lt;/code&gt; option of the &lt;code&gt;template&lt;/code&gt; binding now try to understand if items have been moved and will relocate the content rather than tearing it down and re-rendering it at its new position. This comes with some new callbacks (&lt;code&gt;beforeMove&lt;/code&gt; and &lt;code&gt;afterMove&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;ko&lt;/code&gt; variable is now available from within bindings, even if &lt;code&gt;ko&lt;/code&gt; is not a global variable (common in AMD scenarios).&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;You can download the 2.2 files from &lt;a href="https://github.com/SteveSanderson/knockout/downloads"&gt;here&lt;/a&gt;. There were no intentional breaking changes made in 2.2 (unless someone was relying on an existing bug), so if you have any issues with your application not behaving as it did in 2.1, please log an issue &lt;a href="https://github.com/SteveSanderson/knockout/issues"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/VkcFhAzBbBA" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/10/knockout-2-dot-2-is-out.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Getting the Most Out of Knockout.js Session From Twin Cities Code Camp]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/k5gpejGdjzU/twincitiescodecamp-2012-session.html" />
    <updated>2012-10-09T20:35:00-05:00</updated>
    <id>http://www.knockmeout.net/2012/10/twin-cities-code-camp-knockout-talk</id>
    <content type="html">&lt;p&gt;&lt;img class="right" src="http://www.knockmeout.net/images/posts/tccc.jpg" width="435" height="326" title="Photo by @JudahGabriel" &gt;&lt;/p&gt;

&lt;p&gt;Last weekend, I was invited to talk about &lt;a href="http://knockout.js"&gt;Knockout.js&lt;/a&gt; at &lt;a href="http://twincitiescodecamp.com"&gt;Twin Cities Code Camp&lt;/a&gt;. It was a very enjoyable day, as I saw several great presentations and had a chance to talk to many Knockout users.&lt;/p&gt;

&lt;p&gt;I was told that there had been several sessions in the past that touched on Knockout, so for this talk I decided to focus on some tips, tricks, and techniques that can be used to get the most out of Knockout. The topics covered included:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Brief introduction to Knockout&lt;/li&gt;
&lt;li&gt;Getting data in and out&lt;/li&gt;
&lt;li&gt;Extending Knockout&amp;#8217;s structures&lt;/li&gt;
&lt;li&gt;Custom Bindings&lt;/li&gt;
&lt;li&gt;Performance tips&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;There was a small amount of overlap with the &lt;a href="http://www.knockmeout.net/2012/08/thatconference-2012-session.html"&gt;session&lt;/a&gt; that I did at &lt;a href="http://thatConference.com"&gt;ThatConference&lt;/a&gt; in August, but all of the samples were new for this talk. The project that I used to give the presentation is also described in the ThatConference &lt;a href="http://www.knockmeout.net/2012/08/thatconference-2012-session.html"&gt;post&lt;/a&gt; and a reference project lives &lt;a href="https://github.com/rniemeyer/SamplePresentation"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here is a screencast of the presentation:&lt;/p&gt;

&lt;iframe src="http://player.vimeo.com/video/51103092" width="500" height="335" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen&gt;&lt;/iframe&gt;


&lt;p&gt; &lt;p&gt;&lt;a href="http://vimeo.com/51103092"&gt;Getting the Most Out of Knockout.js - 10/06/2012 - Twin Cities Code Camp&lt;/a&gt;&lt;/p&gt;&lt;/p&gt;

&lt;p&gt;A few clarifications from the presentation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At the end of the &amp;#8220;getting data out&amp;#8221; part, I had typed &lt;code&gt;var meta&lt;/code&gt; instead of &lt;code&gt;this.meta&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;maxLength&lt;/code&gt; extender, I used a writeable computed observable. In the &lt;code&gt;write&lt;/code&gt; function, I should have written the new value to our observable (&lt;code&gt;original(newValue);&lt;/code&gt;), in addition to checking its length.&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;enterKey&lt;/code&gt; custom binding, you probably would want to return the result of the actual function call, as when you return &lt;code&gt;true&lt;/code&gt; from a handler called by the &lt;code&gt;event&lt;/code&gt; binding, it will allow the default action to proceed, which can be useful at times.&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;modal&lt;/code&gt; binding where we add a handler for the &lt;code&gt;hidden&lt;/code&gt; event, you may want to check if what you are dealing with can be written to as an observable (is an observable or writeable computed observable) by calling &lt;code&gt;ko.isWriteableObservable&lt;/code&gt;. So, we might want to call &lt;code&gt;ko.isWriteableObservable(data)&lt;/code&gt; before doing &lt;code&gt;data(null)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;modal&lt;/code&gt; binding, the wrappers to the &lt;code&gt;with&lt;/code&gt; binding, should really be written with &lt;code&gt;with&lt;/code&gt; quoted like &lt;code&gt;return ko.bindingHandlers['with'].init.apply(this, arguments);&lt;/code&gt; and &lt;code&gt;return ko.bindingHandlers['with'].update.apply(this, arguments);&lt;/code&gt;. In IE &amp;lt; 9 the use of &lt;code&gt;with&lt;/code&gt; as a property name will cause an error, as it is a JavaScript keyword.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Participating in the code camp was definitely worth the effort of traveling up to the Twin Cities. I would be happy to hear any type of feedback on the presentation. Photo by &lt;a href="https://twitter.com/JudahGabriel/"&gt;@JudahGabriel&lt;/a&gt;.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/k5gpejGdjzU" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/10/twincitiescodecamp-2012-session.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Extending Knockout.js Session From ThatConference]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/p7iTmunGeyw/thatconference-2012-session.html" />
    <updated>2012-08-15T20:35:00-05:00</updated>
    <id>http://www.knockmeout.net/2012/08/that-conference-knockout-talk</id>
    <content type="html">&lt;p&gt;&lt;img class="right" src="http://www.knockmeout.net/images/posts/speaking.jpg" width="240" height="320" title="ThatConference" &gt;&lt;/p&gt;

&lt;p&gt;I had a wonderful time at &lt;a href="http://thatConference.com"&gt;ThatConference&lt;/a&gt; this week. It was really special to be in a place with so many people that are passionate about the same types of things that I am.
I also had the pleasure of giving a talk about Knockout. The session mainly focused on the various extensibility points in Knockout. After reviewing some Knockout basics, I discussed extending observables, creating custom bindings, and the idea of binding providers.&lt;/p&gt;

&lt;p&gt;For the presentation, I knew that I wanted to show a lot of code, but I was not too excited about continually flipping between a slide show, an IDE, and a browser.  So, I decided to create a slideshow from scratch with Knockout that let me combine all three pieces into a single page app.  It was quite a bit of work, but it was a fun experience overall.&lt;/p&gt;

&lt;p&gt;Here is a screencast of the presentation:&lt;/p&gt;

&lt;iframe src="http://player.vimeo.com/video/47567244" width="500" height="313" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen&gt;&lt;/iframe&gt;


&lt;p&gt; &lt;p&gt;&lt;a href="http://vimeo.com/47567244"&gt;Extending Knockout.js - 08/14/2012 - ThatConference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For creating the presentation app, I used these libraries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://knockoutjs.com"&gt;Knockout&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://requirejs.org"&gt;require.js&lt;/a&gt; &amp;amp; the &lt;a href="https://github.com/requirejs/text"&gt;text plugin&lt;/a&gt; - for managing dependencies and dynamically loading view models, templates, and code samples.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://codemirror.net/"&gt;CodeMirror&lt;/a&gt; - for the code editor&lt;/li&gt;
&lt;li&gt;&lt;a href="http://jQuery.com"&gt;jQuery&lt;/a&gt; - just for some animations&lt;/li&gt;
&lt;li&gt;&lt;a href="http://jsplumb.org/jquery/demo.html"&gt;jqPlumb&lt;/a&gt; - for drawing lines between two elements&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Here is a repository with a heavily stripped down version of the app that I created for this presentation &lt;a href="https://github.com/rniemeyer/SamplePresentation"&gt;https://github.com/rniemeyer/SamplePresentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The binding provider sample implementation is also available here &lt;a href="https://github.com/rniemeyer/knockout-classBindingProvider"&gt;https://github.com/rniemeyer/knockout-classBindingProvider&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I would love to hear any feedback on the presentation. Also, I was thinking about the possibility of recording some short (&amp;lt; 10 minutes) screencasts on various Knockout topics. Would there be any interest in something like that?&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/p7iTmunGeyw" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/08/thatconference-2012-session.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Knockout.js Performance Gotcha #3 - all bindings fire together]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/A2bOWpVWuWA/knockoutjs-performance-gotcha-3-all-bindings.html" />
    <updated>2012-06-22T23:55:00-05:00</updated>
    <id>http://www.knockmeout.net/2012/06/performance-gotcha-3-bindings-fired-together</id>
    <content type="html">&lt;h2&gt;How bindings are processed&lt;/h2&gt;

&lt;p&gt;When using multiple bindings on a single element, it is important to understand how Knockout triggers updates to bindings to avoid potential performance issues. For example, a common binding might look like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;options: choices, value: selectedValue, visible: showChoices&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;How are the bindings on this element processed? When Knockout determines that an element has bindings, a computed observable is created to aid in tracking dependencies. Inside the context of this computed observable, Knockout parses the &lt;code&gt;data-bind&lt;/code&gt; attribute&amp;#8217;s value to determine which bindings to run and the arguments to pass. As the &lt;code&gt;init&lt;/code&gt; and &lt;code&gt;update&lt;/code&gt; functions for each binding are executed, the computed observable takes care of accumulating dependencies on any observables that have their value accessed.&lt;/p&gt;

&lt;p&gt;Here is a simplified flow chart of the binding execution:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.knockmeout.net/images/posts/bindings.png"&gt;&lt;/p&gt;

&lt;p&gt;There are a couple of important points to understand here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;init&lt;/code&gt; function for each binding is only executed once.  However, currently this does happen inside the computed observable that was created to track this element&amp;#8217;s bindings.  This means that you can trigger the binding to run again (only it&amp;#8217;s &lt;code&gt;update&lt;/code&gt; function) based on a dependency created during initialization.  Since, the &lt;code&gt;init&lt;/code&gt; function won&amp;#8217;t run again, this dependency will likely be dropped on the second execution (unless it was also accessed in the &lt;code&gt;update&lt;/code&gt; function).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is currently only &lt;strong&gt;one&lt;/strong&gt; computed observable used to track &lt;em&gt;all&lt;/em&gt; of an element&amp;#8217;s bindings.  This means that the &lt;code&gt;update&lt;/code&gt; function for &lt;em&gt;all&lt;/em&gt; of an element&amp;#8217;s bindings will run again when any of the dependencies are updated.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;This is definitely something to consider when writing custom bindings where your &lt;code&gt;update&lt;/code&gt; function does a significant amount of work. Whenever bindings are triggered for the element, your &lt;code&gt;update&lt;/code&gt; function will run, even if none of its observables were triggered. If possible, it is a good idea to check if you need to do work, before actually executing your &lt;code&gt;update&lt;/code&gt; logic.&lt;/p&gt;

&lt;h2&gt;Common problems with the default bindings&lt;/h2&gt;

&lt;p&gt;1- A common scenario where this can cause an issue is when using the &lt;code&gt;template&lt;/code&gt; binding in conjunction with other bindings. For example, you may attach a &lt;code&gt;visible&lt;/code&gt; binding along with a &lt;code&gt;template&lt;/code&gt; binding like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;visible: showPlaylist, template: { name: &amp;#39;playlistTmpl&amp;#39;, data: playlist }&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt; In this case, if &lt;code&gt;showPlaylist&lt;/code&gt; is frequently updated, it will cause the &lt;code&gt;template&lt;/code&gt; binding to re-render the template again. In some cases, this may not cause a concern (it would just behave like the &lt;code&gt;if&lt;/code&gt; binding). However, in a scenario where the template has significant markup and the &lt;code&gt;visible&lt;/code&gt; binding&amp;#8217;s condition is frequently triggered this can cause an unnecessary performance hit. Note that when using the &lt;code&gt;foreach&lt;/code&gt; option or binding, logic is executed to determine if any items were added or removed, so it will cause less of a performance hit in that case.&lt;/p&gt;

&lt;p&gt;2- Another place where this can come into play with the default bindings is when using the &lt;code&gt;options&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; bindings together.  The &lt;code&gt;update&lt;/code&gt; function of the &lt;code&gt;options&lt;/code&gt; binding rebuilds the list of option tags for the select element. Whenever the &lt;code&gt;value&lt;/code&gt; is updated, it will trigger all of the bindings on the element to execute.  So, instead of just setting the appropriate value, it will rebuild all of the options and then set the value.  If you have a situation where you have a large number of options, then this can cause a performance issue.&lt;/p&gt;

&lt;h2&gt;Ways to address this concern&lt;/h2&gt;

&lt;p&gt;1- In some cases, it makes sense to put bindings on separate elements or on a container element.  For example, you may be able to move a frequently triggered &lt;code&gt;visible&lt;/code&gt; binding to a parent element rather
have it coupled with other bindings like the &lt;code&gt;template&lt;/code&gt; binding.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;visible: showPlaylist&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;template: { name: &amp;#39;playlistTmpl&amp;#39;, data: playlist }&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Here is a sample showing a &lt;code&gt;visible&lt;/code&gt; and &lt;code&gt;template&lt;/code&gt; binding on the same and different elements:&lt;/p&gt;

&lt;iframe style="width: 100%; height: 400px" src="http://jsfiddle.net/jUsup/embedded/result,js,html,css/light/"&gt;&lt;/iframe&gt;


&lt;p&gt;2- In the case of the &lt;code&gt;options&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; bindings, you can choose to build your option elements separately. It would be nice to just use a containerless &lt;code&gt;foreach&lt;/code&gt; statement inside of a &lt;code&gt;select&lt;/code&gt; element, but Internet Explorer will remove comments that it finds between &lt;code&gt;select&lt;/code&gt; and &lt;code&gt;option&lt;/code&gt; elements. An alternative would be to use Michael Best&amp;#8217;s &lt;a href="https://github.com/mbest/knockout-repeat"&gt;repeat&lt;/a&gt; binding on the &lt;code&gt;option&lt;/code&gt; element like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;value: selectedOption&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;repeat: {foreach: items, bind: &amp;#39;attr: { value: $item().id }, text: $item().name&amp;#39;}&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;3- A more advanced way to handle these issues, is to create your own computed observable in the &lt;code&gt;init&lt;/code&gt; function to handle updates yourself. Any observables accessed in your computed observable, will not be a dependency of the overall computed observable used to track all of the element&amp;#8217;s bindings.&lt;/p&gt;

&lt;p&gt;This is a technique that I tend to use by default in any bindings that I write. It is also useful when you want a single binding to accept multiple observable options and you want to respond separately to each one changing (as opposed to using the &lt;code&gt;update&lt;/code&gt; function to repond when any observable changes).  You can even wrap the existing bindings in this way to create an &lt;code&gt;isolatedOptions&lt;/code&gt; binding.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bindingHandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isolatedOptions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;valueAccessor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="nx"&gt;read&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;               &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unwrapObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;valueAccessor&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;               &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bindingHandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="nx"&gt;disposeWhenNodeIsRemoved&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;A few notes on this technique:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The idea is that we want to trap our dependencies in our own isolated computed observable.&lt;/li&gt;
&lt;li&gt;We call the update function of the &lt;code&gt;options&lt;/code&gt; binding using &lt;code&gt;.apply&lt;/code&gt; with the original arguments that were passed ot the binding.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;disposeWhenNodeIsRemoved&lt;/code&gt; option ensures that this computed observable will be destroyed if Knockout removes our element, like in a templating scenario.&lt;/li&gt;
&lt;li&gt;There is one minor issue with using this technique currently: observables that are accessed in the actual binding string are included in the overall computed observable during parsing rather than when you create you call &lt;code&gt;valueAccessor&lt;/code&gt;. This means that if your binding contains an expression where you access the observable&amp;#8217;s value (&lt;code&gt;text: 'Hello ' + name()&lt;/code&gt;) , then it will be tracked in the overall computed observable. This is likely to change in the near future.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Here is a sample that shows using #2 and #3 with &lt;code&gt;options&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; bindings: &lt;a href="http://jsfiddle.net/rniemeyer/QjVNX/"&gt;http://jsfiddle.net/rniemeyer/QjVNX/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Future&lt;/h2&gt;

&lt;p&gt;There has been some thought and work put into running each binding in the context of its own computed observable. &lt;a href="https://github.com/mbest"&gt;Michael Best&lt;/a&gt; has this working properly in his Knockout &lt;a href="https://github.com/mbest/knockout"&gt;fork&lt;/a&gt;. As these changes can be considered breaking, they will likely be carefully implemented over time using an opt-in approach, perhaps until a major version allows us to make some potentially breaking changes.  For now, it is wise to keep in mind how your bindings are triggered and how that affects other bindings on the same element.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/A2bOWpVWuWA" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/06/knockoutjs-performance-gotcha-3-all-bindings.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Using KO's native PubSub for decoupled synchronization]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/W0io-PfH9x8/using-ko-native-pubsub.html" />
    <updated>2012-05-29T23:55:00-05:00</updated>
    <id>http://www.knockmeout.net/2012/05/using-kos-native-pub-slash-sub-for-decoupled-synchronization</id>
    <content type="html">&lt;p&gt;In the previous &lt;a href="http://www.knockmeout.net/2012/05/quick-tip-skip-binding.html"&gt;post&lt;/a&gt;, I suggested some ideas for binding against multiple view models in &lt;a href="http://knockoutjs.com"&gt;Knockout.js&lt;/a&gt;. When working with more than one view model, a common task is deciding how to communicate between the separate components. Creating direct references between them often doesn&amp;#8217;t feel right and can lead you down a path where each view model has lost its independence and cannot be used effectively without the other objects.&lt;/p&gt;

&lt;p&gt;To handle this situation, we can keep our components loosely coupled by using a messaging system where each view model or component does not need to have direct references to its counterparts. There are several benefits to using this technique:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Components remain independent. They can be developed, tested, and used in isolation.&lt;/li&gt;
&lt;li&gt;Knockout view models and non-KO components can communicate using a common interface without direct knowledge of each other.&lt;/li&gt;
&lt;li&gt;Components can be safely refactored. Properties can be renamed, moved, and adjusted without worrying about breaking compatibility.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Knockout&amp;#8217;s native pub/sub&lt;/h2&gt;

&lt;p&gt;There are already many libraries and options for providing this type of message bus. One option might be triggering custom events or using great pub/sub libraries like &lt;a href="http://amplifyjs.com/api/pubsub/"&gt;amplify.js&lt;/a&gt; or &lt;a href="https://github.com/ifandelse/postal.js"&gt;postal.js&lt;/a&gt;. While these libraries provide robust capabilities, Knockout itself already has all of the tools to do basic pub/sub communication built-in. This is part of the &lt;a href="http://knockoutjs.com/documentation/fn.html"&gt;ko.subscribable&lt;/a&gt; type, which has its capabilities added to &lt;code&gt;observables&lt;/code&gt;, &lt;code&gt;computed observables&lt;/code&gt;, and &lt;code&gt;observableArrays&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Normally, you would not construct a &lt;code&gt;ko.subscribable&lt;/code&gt; directly, but it is easy enough to do so:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;postbox&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;In Knockout 2.0, support was added for &lt;a href="http://www.knockmeout.net/2011/11/knockout-13-release-candidate-new.html"&gt;topic-based subscriptions&lt;/a&gt; to aid in sending out &lt;em&gt;beforeChange&lt;/em&gt; notifications. To create a topic-based subscription against our &lt;code&gt;ko.subscribable&lt;/code&gt;, we would simply do:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;postbox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;//with some actual code&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;postbox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;latestTopic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;mytopic&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;To send out a notification on this topic we would do:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;postbox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;notifySubscribers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;mytopic&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Now, we can do basic pub/sub messaging using &lt;code&gt;subscribe&lt;/code&gt; and &lt;code&gt;notifySubscribers&lt;/code&gt; against our mediator, the &lt;code&gt;postbox&lt;/code&gt; object. However, whenever I explore integrating a new technique with Knockout, I try to consider how to make it feel as easy and natural as possible.&lt;/p&gt;

&lt;h2&gt;Extending Observables&lt;/h2&gt;

&lt;p&gt;A typical scenario for this type of functionality would be that we want to synchronize an observable between view models. This might be a one-way or even a two-way conversation. To make this easy, we can look at &lt;a href="http://knockoutjs.com/documentation/fn.html"&gt;extending&lt;/a&gt; the &lt;code&gt;ko.subscribable&lt;/code&gt; type, which would affect &lt;code&gt;observables&lt;/code&gt;, &lt;code&gt;observableArrays&lt;/code&gt;, and &lt;code&gt;computed observables&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Suppose that we want to setup an observable to automatically publish on a topic whenever it changes. We would want to set up a manual subscription against that observable and then use &lt;code&gt;notifySubscribers&lt;/code&gt; on our &lt;code&gt;postbox&lt;/code&gt;.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;publishOn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;postbox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;notifySubscribers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//support chaining&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;//usage of publishOn&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myObservable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;myValue&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;publishOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;myTopic&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Now, whenever the observable&amp;#8217;s value changes, it will publish a message on the topic.&lt;/p&gt;

&lt;p&gt;On the other side, we might want to make it easy for an observable to update itself from messages on a topic. We can use this same concept:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribeTo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;postbox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//support chaining&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;//usage&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observableFromAnotherVM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;subscribeTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;myTopic&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Notice that we can just pass &lt;code&gt;this&lt;/code&gt; (the observable) in as the callback to the &lt;code&gt;subscribe&lt;/code&gt; function. We know that an observable is a function and that it will have its value set when you pass an argument into the function.  So, there is no need to write something like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;//no need to add an extra anonymous function like:&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;postbox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Now, our observables can exchange information without direct references to each other. They do need to agree on a topic, but do not have any knowledge about the internals of the other view model or component. We can even mock or simulate the other components in testing scenarios by firing messages.&lt;/p&gt;

&lt;h2&gt;Potential gotcha: publishing objects&lt;/h2&gt;

&lt;p&gt;If the new value being published is an object, then we need to be careful, as both sides will have a reference to the same object. If code from multiple view models makes changes to that object, then we are likely no longer as decoupled as we would like. If the object is simply configuration/options passed as an object literal that are not modified, then this seems okay. Otherwise, it is preferable to stick to primitives in the values being published. Another alternative is to use something like &lt;code&gt;ko.toJS&lt;/code&gt; to create a deep-copy of the object that is being published, so neither side has the same reference.&lt;/p&gt;

&lt;h2&gt;A new library: knockout-postbox&lt;/h2&gt;

&lt;p&gt;I created a small &lt;a href="https://github.com/rniemeyer/knockout-postbox"&gt;library&lt;/a&gt; called &lt;strong&gt;knockout-postbox&lt;/strong&gt; to handle this type of communication. It uses the techniques described above and adds a bit of additional functionality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;creates a &lt;code&gt;ko.postbox&lt;/code&gt; object with clean &lt;code&gt;subscribe&lt;/code&gt; and &lt;code&gt;publish&lt;/code&gt; methods that take a topic as the first argument.&lt;/li&gt;
&lt;li&gt;adds a &lt;code&gt;subscribeTo&lt;/code&gt; function to all subscribables (observables, observableArrays, and computed observables). The &lt;code&gt;subscribeTo&lt;/code&gt; function also allows you to initialize your observable from the latest published value and allows you to pass a transform function that runs on the incoming values.&lt;/li&gt;
&lt;li&gt;adds an &lt;code&gt;unsubcribeFrom&lt;/code&gt; function to all subscribables that removes subscriptions created by &lt;code&gt;subscribeTo&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;adds a &lt;code&gt;publishOn&lt;/code&gt; function to all subscribables that automatically publishes out new values. The &lt;code&gt;publishOn&lt;/code&gt; function also allows you to control whether you want to immediately publish the initial value, and lets you supply an override function (&lt;code&gt;equalityComparer&lt;/code&gt;) that allows you to compare the old and new values to determine if the new value should really be published.&lt;/li&gt;
&lt;li&gt;adds a &lt;code&gt;stopPublishingOn&lt;/code&gt; function to all subscribables that removes the subscription that handles the &lt;code&gt;publishOn&lt;/code&gt; messages.&lt;/li&gt;
&lt;li&gt;adds a &lt;code&gt;syncWith&lt;/code&gt; function that does both a &lt;code&gt;subscribeTo&lt;/code&gt; and a &lt;code&gt;publishOn&lt;/code&gt; on the same topic for two-way synchronization.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Project link: &lt;a href="https://github.com/rniemeyer/knockout-postbox"&gt;https://github.com/rniemeyer/knockout-postbox&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Basic sample:&lt;/em&gt;&lt;/p&gt;

&lt;iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/mg3hj/embedded/result,js,html,css/light/"&gt;&lt;/iframe&gt;


&lt;p&gt;&lt;a href="http://jsfiddle.net/rniemeyer/mg3hj/"&gt;Link to sample on jsFiddle.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Next post: using this concept to integrate with client-side routing in a way that is natural to Knockout&lt;/em&gt;&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/W0io-PfH9x8" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/05/using-ko-native-pubsub.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Quick Tip: Telling Knockout to skip binding part of a page]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/ATufIF2wPNo/quick-tip-skip-binding.html" />
    <updated>2012-05-16T20:57:00-05:00</updated>
    <id>http://www.knockmeout.net/2012/05/quick-tip-skip-binding</id>
    <content type="html">&lt;p&gt;Recently, I worked with several people on questions related to binding multiple view models in a single page. One common approach is to bind a view model to a particular root element using a call like &lt;code&gt;ko.applyBindings(vm, containerNode);&lt;/code&gt;. However, a limitation with this approach is that when binding multiple view models, none of the container elements can overlap. This means that you could not bind one view model nested inside of another.&lt;/p&gt;

&lt;p&gt;One way to address this issue is to create a top level view model that contains your &amp;#8220;sub&amp;#8221; view models and then call &lt;code&gt;ko.applyBindings&lt;/code&gt; on the entire page with the overall view model:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;span class='line-number'&gt;14&lt;/span&gt;
&lt;span class='line-number'&gt;15&lt;/span&gt;
&lt;span class='line-number'&gt;16&lt;/span&gt;
&lt;span class='line-number'&gt;17&lt;/span&gt;
&lt;span class='line-number'&gt;18&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;profileModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Bob&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Smith&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;shellModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Administration&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;sections&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;profile&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;settings&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;notifications&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;selectedSection&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;//the overall view model&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;viewModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;shell&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;shellModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;profile&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;profileModel&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;applyBindings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Now in the view, you can use the &lt;code&gt;with&lt;/code&gt; binding along with &lt;code&gt;$root&lt;/code&gt; to bind a nested view model:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;with: shell&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;text: header&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;with: $root.profile&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        ...
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    ...
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This technique is nice, because you only have to make a single &lt;code&gt;ko.applyBindings&lt;/code&gt; call and you can use &lt;code&gt;$root&lt;/code&gt; or &lt;code&gt;$parent&lt;/code&gt;/&lt;code&gt;$parents&lt;/code&gt; to access data at any time from another view model. However, based on a desire to maintain modular code and to control how and when elements are bound, it is often not convenient or practical to build a top level view model.&lt;/p&gt;

&lt;p&gt;With Knockout 2.0, there is a simple alternative that can provide for greater flexibility. Bindings are now able to return a flag called &lt;code&gt;controlsDescendantBindings&lt;/code&gt; in their &lt;code&gt;init&lt;/code&gt; function to indicate that the current binding loop should not try to bind this element&amp;#8217;s children. This flag is used by the &lt;code&gt;template&lt;/code&gt; and control-flow bindings (&lt;a href="http://www.knockmeout.net/2011/09/ko-13-preview-part-1-native-template.html"&gt;wrappers&lt;/a&gt; to the &lt;code&gt;template&lt;/code&gt; binding), as they will handle binding their own children with an appropriate data context.&lt;/p&gt;

&lt;p&gt;For our scenario, we can take advantage of this flag and simply tell Knockout to leave a certain section alone by using a simple custom binding:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bindingHandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stopBinding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;controlsDescendantBindings&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Now, we can bind our &amp;#8220;shell&amp;#8221; model to the entire page and bind our &amp;#8220;profile&amp;#8221; model to the specific container:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;profileModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Bob&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Smith&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;shellModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Administration&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;sections&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;profile&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;settings&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;notifications&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;selectedSection&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;applyBindings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shellModel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;applyBindings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;profileModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;profile&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;In our view, we can now use the simple &lt;code&gt;stopBinding&lt;/code&gt; custom binding around our inner container element:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;text: header&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;stopBinding: true&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;profile&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;value: first&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;value: last&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    ...
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Adding the extra div to hold our &lt;code&gt;stopBinding&lt;/code&gt; binding may not cause our app any problems, but if it does then in KO 2.1 we can now create containerless custom bindings by adding our binding to &lt;code&gt;ko.virtualElements.allowedBindings&lt;/code&gt;.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bindingHandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stopBinding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;controlsDescendantBindings&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;virtualElements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allowedBindings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stopBinding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;and finally we can clean up our view to look like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;text: header&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="c"&gt;&amp;lt;!-- ko stopBinding: true --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;profile&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;value: first&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;value: last&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="c"&gt;&amp;lt;!-- /ko --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;With this simple binding, we can now compose pages with multiple view models without the worry of conflicting/overlapping bindings.&lt;/p&gt;

&lt;p&gt;Here is a live sample:&lt;/p&gt;

&lt;iframe style="width: 100%; height: 250px" src="http://jsfiddle.net/fjj7q/embedded/result,js,html,css/light/"&gt;&lt;/iframe&gt;


&lt;p&gt;&lt;a href="http://jsfiddle.net/rniemeyer/fjj7q/"&gt;Link to full sample on jsFiddle.net&lt;/a&gt;&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/ATufIF2wPNo" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/05/quick-tip-skip-binding.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Knockout 2.1 is out]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/DDpkiqTkTqM/knockout-2-dot-1-is-out.html" />
    <updated>2012-05-08T03:57:00-05:00</updated>
    <id>http://www.knockmeout.net/2012/05/knockout-2-dot-1-is-out</id>
    <content type="html">&lt;p&gt;&lt;a href="http://knockoutjs.com"&gt;Knockout.JS&lt;/a&gt; &lt;strong&gt;2.1&lt;/strong&gt; is now available! Minified and debug versions available &lt;a href="https://github.com/SteveSanderson/knockout/downloads"&gt;here&lt;/a&gt;.  This release focused mainly on performance improvements and bug fixes after the 2.0 release.&lt;/p&gt;

&lt;h3&gt;Here are some highlights:&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;CommonJS and &lt;a href="http://knockoutjs.com/documentation/amd-loading.html"&gt;AMD support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Great performance improvements to templating - use cloned DOM nodes when possible rather than parsing strings into elements.&lt;/li&gt;
&lt;li&gt;Support for &lt;a href="http://knockoutjs.com/documentation/custom-bindings-for-virtual-elements.html"&gt;custom container-less bindings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Ability to &lt;a href="http://knockoutjs.com/documentation/custom-bindings-controlling-descendant-bindings.html"&gt;extend the binding context&lt;/a&gt; in a custom binding&lt;/li&gt;
&lt;li&gt;Computed observables no longer can recursively evaluate themselves (&lt;em&gt;I think that has happened to all of us a few times!&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$index&lt;/code&gt; is available in &lt;code&gt;foreach&lt;/code&gt; scenarios from the &lt;a href="http://knockoutjs.com/documentation/binding-context.html"&gt;binding context&lt;/a&gt;. There is no longer a need for &lt;a href="http://stackoverflow.com/questions/8744374/how-to-access-the-index-of-an-item-in-knockout-js-template/8744771#8744771"&gt;workarounds&lt;/a&gt; like storing an index as a property of your data items. Note that &lt;code&gt;$index&lt;/code&gt; is an observable, so it needs to be reference using &lt;code&gt;$index()&lt;/code&gt; in expressions.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$parentContext&lt;/code&gt; is available from the &lt;a href="http://knockoutjs.com/documentation/binding-context.html"&gt;binding context&lt;/a&gt; as well. While, &lt;code&gt;$parent&lt;/code&gt; contains the actual data from one scope level up, &lt;code&gt;$parentContext&lt;/code&gt; contains the actual binding context (&lt;code&gt;$data&lt;/code&gt;, &lt;code&gt;$parent&lt;/code&gt;, &lt;code&gt;$parents&lt;/code&gt;, &lt;code&gt;$root&lt;/code&gt;, etc.) of the parent.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ko.isComputed&lt;/code&gt; is a helper function that was added to determine if a value is a computed observable or not.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ko.toJSON&lt;/code&gt; now passes its arguments on to &lt;code&gt;JSON.stringify&lt;/code&gt; after calling &lt;code&gt;ko.toJS&lt;/code&gt;.  This makes creating a handy &amp;#8220;debug&amp;#8221; section even easier &lt;code&gt;&amp;lt;pre data-bind="text: ko.toJSON($root, null, 2)"&amp;gt;&amp;lt;/pre&amp;gt;&lt;/code&gt; and this can be used to include only certain properties in the converted JSON like &lt;code&gt;ko.toJSON(person, ['firstName', 'lastName'])&lt;/code&gt;. This seems even more flexible in many cases than the approach that I mentioned &lt;a href="http://www.knockmeout.net/2011/04/controlling-how-object-is-converted-to.html"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Better support for using a single instance of KO between documents - you can now pass an instance of KO between documents and have bindings work properly across the documents. Previously the computed observables associated with bindings would dispose themselves when outside of the original document as they believed they were no longer part of the DOM.&lt;/li&gt;
&lt;li&gt;Numerous other bug and performance fixes&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;For this release &lt;a href="https://github.com/mbest"&gt;Michael Best&lt;/a&gt; joined the core team and drove the majority of the changes with &lt;a href="http://blog.stevensanderson.com/"&gt;Steve&lt;/a&gt;. Michael has numerous other exciting performance enhancements in his &lt;a href="https://github.com/mbest/knockout"&gt;fork&lt;/a&gt; that will be evaluated for possible inclusion (in a non-breaking fashion) in the core. He has really helped tremendously in moving the project forward.&lt;/p&gt;

&lt;p&gt;Please report any issues found &lt;a href="https://github.com/SteveSanderson/knockout/issues"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/DDpkiqTkTqM" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/05/knockout-2-dot-1-is-out.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Giving the blog a facelift]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/nJoSTKr3NwM/giving-the-blog-a-facelift.html" />
    <updated>2012-05-07T11:58:00-05:00</updated>
    <id>http://www.knockmeout.net/2012/05/giving-the-blog-a-facelift</id>
    <content type="html">&lt;p&gt;I just finished the process of moving this blog from Blogger to an &lt;a href="http://octopress.org/"&gt;Octopress&lt;/a&gt; site that uses &lt;a href="https://github.com/mojombo/jekyll"&gt;Jekyll&lt;/a&gt; to generate a static blog. I am excited to have better control over the look and feel of the blog and to move to a git-based workflow using &lt;a href="http://daringfireball.net/projects/markdown/"&gt;markdown&lt;/a&gt; files for posts.&lt;/p&gt;

&lt;p&gt;Here are a few of the steps that I took to make this move:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Converted Blogger site to use &lt;a href="http://disqus.com"&gt;disqus&lt;/a&gt; for comments&lt;/li&gt;
&lt;li&gt;Imported old comments using the import tool from disqus&lt;/li&gt;
&lt;li&gt;Imported blog posts into Octopress using the script &lt;a href="http://coolaj86.info/articles/migrate-from-blogger-to-jekyll.html"&gt;here&lt;/a&gt; to at least get some basic structure and permalinks.&lt;/li&gt;
&lt;li&gt;At that point, I scrapped all of the HTML for each post and recreated each in markdown. It was a painful and time consuming process, but the posts will hopefully now look consistent and will be easier to edit/update in the future.&lt;/li&gt;
&lt;li&gt;Made updates/change to the layout and look of the default template.&lt;/li&gt;
&lt;li&gt;Created an app in &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt;, pushed the site to it, and added the custom domain.&lt;/li&gt;
&lt;li&gt;Pointed the domain to Heroku.&lt;/li&gt;
&lt;li&gt;Updated the Feedburner feed to use the new RSS.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I was never too excited about the look, feel, and process of updating the old site. I was mostly focused on getting content out there and a Blogger blog was an easy way to get started. I am excited to continue tweaking the site and using git and markdown for the posts. Hopefully, I will find it easier to make more frequent posts and to update older posts as Knockout changes.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/nJoSTKr3NwM" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/05/giving-the-blog-a-facelift.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Knockout.js Performance Gotcha #2 - manipulating observableArrays]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/znRwma_Pzn0/knockoutjs-performance-gotcha.html" />
    <updated>2012-04-07T23:24:00-05:00</updated>
    <id>http://www.knockmeout.net/2012/04/knockoutjs-performance-gotcha-2</id>
    <content type="html">&lt;p&gt;Most &lt;a href="http://knockoutjs.com"&gt;Knockout.js&lt;/a&gt; applications tend to deal with collections of objects that are stored in one or more observableArrays. When dealing with observableArrays, it is helpful to understand exactly how they work to make sure that you don&amp;#8217;t hit a performance issue.&lt;/p&gt;

&lt;p&gt;An observableArray is simply an observable with some additional methods added to deal with common array manipulation tasks.  These functions get a reference to the underlying array, perform an action on it, then notify subscribers that the array has changed (in KO 2.0, there is now an additional notification prior to the change as well).&lt;/p&gt;

&lt;p&gt;For example, doing a &lt;code&gt;push&lt;/code&gt; on an observableArray essentially does this:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observableArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;underlyingArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueWillMutate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;underlyingArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;underlyingArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueHasMutated&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;h2&gt;Pushing items to an observableArray&lt;/h2&gt;

&lt;p&gt;Let’s consider a common scenario where we have a collection of objects along with a computed observable to track a filtered array of those objects.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;ViewModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observableArray&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Task One&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;high&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Task Two&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;normal&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Task Three&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;high&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;highPriorityItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arrayFilter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;high&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Now, say we want to add additional data from the server to the observableArray of items.  It is easy enough to loop through the new data and push each mapped item to our observableArray.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addNewDataBad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Consider what happens though each time that we call &lt;code&gt;.push()&lt;/code&gt;.  The item is added to our underlying array and any subscribers are notified of the change.  Each time that we push, our &lt;code&gt;highPriorityItems&lt;/code&gt; filter code will run again.  Additionally, if we are binding our UI to the items observableArray, then the template binding has to do work each time to determine that only the one new item was added.&lt;/p&gt;

&lt;p&gt;A better pattern is to get a reference to our underlying array, push to it, then call &lt;code&gt;.valueHasMutated()&lt;/code&gt;.  Now, our subscribers will only receive one notification indicating that the array has changed.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addNewDataGood&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;underlyingArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;underlyingArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueHasMutated&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This can be simplified down to:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addNewData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;newItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arrayMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="c1"&gt;//take advantage of push accepting variable arguments&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newItems&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Here is a jsFiddle that tracks the number of re-evaluations for each method: &lt;a href="http://jsfiddle.net/rniemeyer/NCpxm/"&gt;http://jsfiddle.net/rniemeyer/NCpxm/&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Clearing or replacing the contents of an observableArray&lt;/h2&gt;

&lt;p&gt;Another common scenario is when you want to completely replace the contents of an observableArray. This may be to empty the array or to set it to a new set of items.  There is no need to loop through the array and operate on it.  The most efficient operation is to simply set it to a new value:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;//clear the observableArray&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;//replace the contents of the observableArray&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;otherItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Minimizing the amount of notifications to subscribers from observableArrays is an easy way to avoid a potential performance issue.  Typically, this can be done by performing your operations on the underling array and then triggering notifications.  In other cases, replacing the observableArray’s value with a completely new value is a good choice that results in a single notification.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/znRwma_Pzn0" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/04/knockoutjs-performance-gotcha.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Knockout.js Performance Gotcha #1 - if/with bindings are not always cheap]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/lnidR4W-nJo/knockoutjs-performance-gotcha-1ifwith.html" />
    <updated>2012-03-29T23:56:00-05:00</updated>
    <id>http://www.knockmeout.net/2012/03/knockoutjs-performance-gotcha-1-if-with</id>
    <content type="html">&lt;p&gt;&lt;em&gt;update: &lt;a href="http://www.knockmeout.net/2012/10/knockout-2-dot-2-is-out.html"&gt;Knockout 2.2&lt;/a&gt; has addressed the first issue in this post. Now the &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;ifnot&lt;/code&gt; binding will not re-render their contents unless the value changes between truthy and falsy.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A recent StackOverflow &lt;a href="http://stackoverflow.com/questions/9927213/performance-tuning-a-knockout-application-guidelines-for-improving-response-ti"&gt;question&lt;/a&gt; prompted me to start a series of short posts with some basic performance tips/gotchas for &lt;a href="http://knockoutjs.com"&gt;Knockout.js&lt;/a&gt;. In smaller applications, many of these items would go unnoticed, but as a project becomes more complex, it is important to understand the amount of work that Knockout is doing to manage your UI.&lt;/p&gt;

&lt;p&gt;The control flow bindings &lt;a href="http://www.knockmeout.net/2011/09/ko-13-preview-part-1-native-template.html"&gt;introduced&lt;/a&gt; in Knockout 2.0 are really just simple wrappers to the template binding. They use the children of the element as their “template” (what we call anonymous templates) and will re-render the contents whenever triggered.  If you are not careful, the &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;with&lt;/code&gt; bindings may be causing re-renders much more often than you might realize.&lt;/p&gt;

&lt;h2&gt;Problem: &lt;em&gt;if&lt;/em&gt; binding causing frequent re-renders&lt;/h2&gt;

&lt;p&gt;A common scenario where you might use the &lt;a href="http://knockoutjs.com/documentation/if-binding.html"&gt;if&lt;/a&gt; binding is when you want to display a section only when an observableArray actually contains items.  Something like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="c"&gt;&amp;lt;!--&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Items&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;/h2&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;foreach: items&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;text: name&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;/li&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;/ul&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="c"&gt;&amp;lt;!--&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;ko&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;The problem in this case, is that the &lt;code&gt;if&lt;/code&gt; binding depends on the &lt;code&gt;items&lt;/code&gt; observableArray and re-evaluates every time that it is updated.  Each time that it is triggered and has a truthy value, it will actually re-render its “template” again, as you can see &lt;a href="http://jsfiddle.net/rniemeyer/A2Rt5/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Solutions&lt;/h2&gt;

&lt;p&gt;One choice to avoid these re-renders would be to instead use the &lt;code&gt;visible&lt;/code&gt; binding on a container element around our section or on the individual elements, as shown &lt;a href="http://jsfiddle.net/rniemeyer/nWkZq/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If we prefer to use the &lt;code&gt;if&lt;/code&gt; binding in this case, then we need to make sure that it is only triggered when the number of items in our array moves between 0 and 1.  One might think that a computed observable holding the length would be a good choice.  However, one important thing to note is that currently &lt;strong&gt;computed observables will always notify subscribers when re-evaluated, even if their value ends up being the same&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;However, this is not true for observables. Notifications are suppressed when you write a value that is identical to the previous value (this behavior can be overridden).  So, one option is to subscribe to changes to our observableArray and populate an observable based on the length.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observableArray&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Now, we can bind against &lt;code&gt;hasItems&lt;/code&gt; and it will only notify subscribers when changing between true and false based on the length of the array.  You can &lt;a href="http://jsfiddle.net/rniemeyer/vEzaL/"&gt;here&lt;/a&gt; see that each item is no longer re-rendered on every add.&lt;/p&gt;

&lt;p&gt;You could even extend observableArrays to add a function that would attach this &lt;code&gt;hasItems&lt;/code&gt; property as a sub-observable and create the subscription to keep it updated.  This helps to keep the view model a bit cleaner and allows you to easily reuse the code.&lt;/p&gt;

&lt;p&gt;It might look like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;span class='line-number'&gt;14&lt;/span&gt;
&lt;span class='line-number'&gt;15&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observableArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;trackHasItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="c1"&gt;//create a sub-observable&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="c1"&gt;//update it when the observableArray is updated&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="c1"&gt;//trigger change to initialize the value&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueHasMutated&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="c1"&gt;//support chaining by returning the array&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;&lt;a href="http://jsfiddle.net/rniemeyer/ShwP9/"&gt;Here&lt;/a&gt; is a sample of how to use this function.&lt;/p&gt;

&lt;h2&gt;Problem: with binding causes entire area to re-render&lt;/h2&gt;

&lt;p&gt;The &lt;a href="with%20binding"&gt;with&lt;/a&gt; binding is very handy for binding against nested objects/models and helps to keep your data-bind attributes from becoming too verbose.  It is important, however, to understand that the with binding will re-render the entire section whenever its bound value changes.&lt;/p&gt;

&lt;p&gt;For example, we might want to render a selected item like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;with: selectedItem&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;Selected&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;value: name&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;value: price&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;The children of the main &lt;code&gt;div&lt;/code&gt; become our template.  Whenever &lt;code&gt;selectedItem&lt;/code&gt; changes, the contents are re-rendered completely.   So, rather than just binding the inputs against the new data, the entire section is replaced with new elements, as shown &lt;a href="http://jsfiddle.net/rniemeyer/5ABwC/"&gt;here&lt;/a&gt;.  In this case, it won’t cause any issues, but if your markup is complex and/or has events attached outside of Knockout, then you can run into issues when the elements are removed and replaced by the original template.&lt;/p&gt;

&lt;h2&gt;Solutions&lt;/h2&gt;

&lt;p&gt;If performance does end up becoming an issue, then we could consider just binding directly to the observables on the selected item.  If the selected item might be null, then you would need to make sure that the binding does not fail looking for properties on a null object, as show in this &lt;a href="http://jsfiddle.net/rniemeyer/94QgT/"&gt;sample&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another choice would be to use the &lt;code&gt;with&lt;/code&gt; binding in smaller, targeted areas.  We can make use of the containerless control-flow bindings to wrap more specific sets of elements, as shown &lt;a href="http://jsfiddle.net/rniemeyer/kYync/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, for cases where you want to bind against a nested object that won’t change (non-observable), take a look at the simple &lt;a href="https://github.com/mbest/knockout/issues/9"&gt;withlight&lt;/a&gt; binding developed by &lt;a href="https://github.com/mbest"&gt;Michael Best&lt;/a&gt;.  This will switch the context to your nested object on initialization, but is not designed to bind against an observable that will be changed.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;The control-flow bindings are certainly handy, but when performance matters, it helps to understand a little bit about how they are implemented.  In cases where you want to avoid re-rendering entire sections of your UI, it takes a little extra work to ensure that this is not happening too frequently.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/lnidR4W-nJo" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/03/knockoutjs-performance-gotcha-1ifwith.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Knockout.js bindings for Kendo UI]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/4Tp3Vxdim20/knockoutjs-bindings-for-kendo-ui.html" />
    <updated>2012-03-25T23:50:00-05:00</updated>
    <id>http://www.knockmeout.net/2012/03/knockoutjs-bindings-for-kendo-ui</id>
    <content type="html">&lt;p&gt;Recently, I spent much of my late hour programming time working on a project to create a set of &lt;a href="http://knockoutjs.com"&gt;Knockout.js&lt;/a&gt; bindings for the &lt;a href="http://kendoui.com/"&gt;Kendo UI&lt;/a&gt; web widgets.  After being involved in several discussions about how Kendo UI could integrate with Knockout, I decided to jump in and try to provide a place where this integration could officially exist and grow.  As a result, there is now a &lt;a href="http://rniemeyer.github.com/knockout-kendo/"&gt;project on Github&lt;/a&gt; for these bindings.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://rniemeyer.github.com/knockout-kendo/img/ko-logo.png" width="55" height="55" title="Knockout logo" &gt; + &lt;img src="http://rniemeyer.github.com/knockout-kendo/img/kendo-logo.png" title="Kendo UI logo" &gt;&lt;/p&gt;

&lt;h2&gt;Kendo UI – a great framework&lt;/h2&gt;

&lt;p&gt;If you are not familiar with Kendo UI, then take a look at the &lt;a href="http://demos.kendoui.com/web/overview/index.html"&gt;demos&lt;/a&gt; and &lt;a href="http://www.kendoui.com/documentation/introduction.aspx"&gt;documentation&lt;/a&gt;.  Also, take note of the &lt;a href="http://www.kendoui.com/get-kendo-ui.aspx"&gt;licensing&lt;/a&gt; requirements.  I am not affiliated with &lt;a href="http://www.telerik.com/"&gt;Telerik&lt;/a&gt; in any way, but am quite impressed with the framework.  In my experience, it is an excellent product with good people working on it.&lt;/p&gt;

&lt;p&gt;When developing these bindings, the first challenge that I found was how to simultaneously write, test, and document 19 custom bindings.  After writing a couple of the bindings “by hand”, I decided to take a different route and create a factory that could generate the bindings.  The idea is that you pass in some configuration and it can produce a proper binding for you.  This would allow for the sharing of common code, easier maintenance/enhancements, and the ability to generate some standard tests for each widget from the configuration.&lt;/p&gt;

&lt;h2&gt;A pattern for 3rd party widget bindings&lt;/h2&gt;

&lt;p&gt;To create the bindings, I followed a pattern that I like to use for custom bindings against 3rd party widget that involves these steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepare the options needed to initialize the widget&lt;/li&gt;
&lt;li&gt;Subscribe to any events triggered by the widget that require an update to the model&lt;/li&gt;
&lt;li&gt;Initialize the widget&lt;/li&gt;
&lt;li&gt;Register to destroy the widget when KO removes this element
*Use either the update function or individual computed observables created in the init function to update the widget based on changes to the model.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;This covers creating the widget and syncing changes between the model and the widget.  However, for these bindings I wanted to support a couple of additional features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The ability to define global default options for a widget.  The binding will merge any options passed in the data-bind attribute with these global options.  This helps to keep the mark up clean by not forcing you to include all of the configuration in the binding string.&lt;/li&gt;
&lt;li&gt;The ability to access the actual widget from the view model.  If you pass a “widget” option to the binding, then it will fill that observable with a reference to the actual widget.  This allows you to call methods of the widget from within view model methods, if necessary.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Next Steps&lt;/h2&gt;

&lt;p&gt;With these particular bindings, I hope to look at bringing tighter integration with templating and data sources.  Many of the widgets (Grid, ListView) need this type of support to fit naturally in a Knockout application.  I also hope to see what kind of feedback comes from real-world usage to decide the next steps.&lt;/p&gt;

&lt;p&gt;Additionally, if there is enough demand, it would be interesting to apply this factory concept to other widgets like jQuery UI, YUI , and/or the Bootstrap plugins.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;One final note: Kendo UI recently released their own &lt;a href="http://demos.kendoui.com/web/mvvm/index.html"&gt;native MVVM implementation&lt;/a&gt;.  It has a Knockout flavor to some of the syntax and integrates well with the widgets.  It is definitely an option to consider, if you are “all in” on Kendo UI.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://rniemeyer.github.com/knockout-kendo/index.html"&gt;Knockout-Kendo.js project link&lt;/a&gt;&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/4Tp3Vxdim20" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/03/knockoutjs-bindings-for-kendo-ui.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Revisiting dragging, dropping, and sorting observableArrays]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/ifNaQFw6BO8/revisiting-dragging-dropping-and.html" />
    <updated>2012-02-04T13:54:00-06:00</updated>
    <id>http://www.knockmeout.net/2012/02/revisit-drag-drop</id>
    <content type="html">&lt;p&gt;Earlier I &lt;a href="http://www.knockmeout.net/2011/05/dragging-dropping-and-sorting-with.html"&gt;explored&lt;/a&gt; a set of &lt;a href="http://knockoutjs.com"&gt;Knockout.js&lt;/a&gt; bindings that would allow dragging and dropping items in an observableArray.  These bindings could either be used to sort items in a single array or to move items between arrays.  With Knockout 2.0, I wanted to take a fresh look at how this can be accomplished.  The resulting project is &lt;a href="https://github.com/rniemeyer/knockout-sortable"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Areas for improvement&lt;/h2&gt;

&lt;p&gt;The original syntax that I was using for binding to a single array looked like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;container&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;template: {name: &amp;#39;taskTmpl&amp;#39;, foreach: tasks},&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="s"&gt;    sortableList: tasks&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;The syntax for binding multiple arrays, required additional data to be passed to the chlidren.  The parent looked like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;container&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;template: {name: &amp;#39;taskTmpl&amp;#39;,&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="s"&gt;    foreach: highPriorityTasks,&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="s"&gt;    templateOptions: { parentList: highPriorityTasks} },&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="s"&gt;    sortableList: highPriorityTasks&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;The child elements required some meta-data to be attached via a binding:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;sortableItem: { item: $data, parentList: $item.parentList }&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This syntax is far too verbose and forces us to use too many bindings to make this work.  My goal is to use a single binding that will work for both a single array and moving items between arrays.  Ultimately, I wanted to simplify it down to something like this:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;sortable: tasks&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;text: name&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;or if using named templates like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;sortable: { tmpl: &amp;#39;taskTmpl&amp;#39;, list: tasks }&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;h2&gt;What does our single binding need to accomplish?&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;We are not specifying the template or foreach binding, so our binding will have to wrap this functionality by calling the template binding with the appropriate options.&lt;/li&gt;
&lt;li&gt;We are not specifying any bindings on the children, so we need to find a way to attach meta-data to the children, so we can retrieve the data and original parent from a dropped element.  Since we are calling the template binding, we can use its  afterRender option to add the meta-data.&lt;/li&gt;
&lt;li&gt;The element require a certain class to be hooked up as a target.  We should handle adding this class in the binding rather than requiring the developer to add it (or forget to add it).&lt;/li&gt;
&lt;li&gt;We still need to initialize the container by calling jQuery UI’s .sortable and specify how to handle an item being dropped.&lt;/li&gt;
&lt;li&gt;We should also call the sortable widgets destroy method when Knockout removes the element (perhaps by the template binding).&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;How about some extra features?&lt;/h2&gt;

&lt;p&gt;With these features in place, we can clean up the syntax required on the element and still accomplish the same functionality as before.  However, there are a few additional enhancements that I felt would be useful in the binding:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can configure the class that is used on the element to enable it as a drop target (the connectWith class).&lt;/li&gt;
&lt;li&gt;You can specify a function, observable, or static value to determine whether the element is a target for dropping.  This will toggle the class based on the supplied value and react if it changes.&lt;/li&gt;
&lt;li&gt;The binding accepts a beforeMove function that allows you to perform an action prior to the item being moved from its original location to its new destination.  This function can also cancel the move.&lt;/li&gt;
&lt;li&gt;The binding also accepts an afterMove function to perform an action after the item has been moved to its new index it the destination array.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;Each of these options can be specified in the binding like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='html'&gt;&lt;span class='line'&gt;&lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;data-bind=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;sortable: {data: items, afterMove: saveData, allowDrop: isBucketFull}&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Additionally, each feature can be configured with a global default.  This keeps the markup/bindings clean when the options will apply to all or most of the bindings.  This would look like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bindingHandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sortable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connectClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;myContainer&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bindingHandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sortable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allowDrop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isBucketFull&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bindingHandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sortable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;afterMove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;saveData&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;h2&gt;Samples&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Single Todo list:&lt;/em&gt;&lt;/p&gt;

&lt;iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/hw9B2/embedded/result,js,html,css/light/"&gt;&lt;/iframe&gt;


&lt;p&gt;&lt;a href="http://jsfiddle.net/rniemeyer/hw9B2/"&gt;Link to sample on jsFiddle.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Connected Todo lists:&lt;/em&gt;&lt;/p&gt;

&lt;iframe style="width: 100%; height: 450px" src="http://jsfiddle.net/Jr2rE/embedded/result,js,html,css/light/"&gt;&lt;/iframe&gt;


&lt;p&gt;&lt;a href="http://jsfiddle.net/rniemeyer/Jr2rE/"&gt;Link to sample on jsFiddle.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Seating chart:&lt;/em&gt;&lt;/p&gt;

&lt;iframe style="width: 100%; height: 500px" src="http://jsfiddle.net/UdXr4/embedded/result,js,html,css/light/"&gt;&lt;/iframe&gt;


&lt;p&gt;&lt;a href="http://jsfiddle.net/rniemeyer/UdXr4/"&gt;Link to sample on jsFiddle.net&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;knockout-sortable Project&lt;/h2&gt;

&lt;p&gt;The binding is up on github here: &lt;a href="https://github.com/rniemeyer/knockout-sortable"&gt;https://github.com/rniemeyer/knockout-sortable&lt;/a&gt;.  I am definitely looking for feedback and suggestions for additional functionality, as well as working to improve the docs, specs, examples.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/ifNaQFw6BO8" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2012/02/revisiting-dragging-dropping-and.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Knockout 2.0 is out!]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/QOGF1PxPEwI/knockout-20-is-out.html" />
    <updated>2011-12-21T08:25:00-06:00</updated>
    <id>http://www.knockmeout.net/2011/12/knockout-20-is-out</id>
    <content type="html">&lt;p&gt;KnockoutJS 2.0 is available now!  This version was formally known as 1.3 and has been in the works for quite a while.  It includes substantial changes that are mainly focused on allowing developers to write cleaner and simpler code while providing additional extensibility points to that allow better control of how Knockout is used. &lt;a href="Knockoutjs.com"&gt;&lt;/a&gt; has been thoroughly updated for this release with changes to docs, examples, and tutorials along with some additional advanced topics.&lt;/p&gt;

&lt;p&gt;A few links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get the 2.0 build on github &lt;a href="https://github.com/SteveSanderson/knockout/downloads"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Check out Steve’s post &lt;a href="http://blog.stevensanderson.com/2011/12/21/knockout-2-0-0-released/"&gt;summarizing&lt;/a&gt; the changes.&lt;/li&gt;
&lt;li&gt;Look at the &lt;a href="http://learn.knockoutjs.com/"&gt;tutorials&lt;/a&gt;, &lt;a href="http://knockoutjs.com/documentation/introduction.html"&gt;docs&lt;/a&gt;, and &lt;a href="http://knockoutjs.com/examples/"&gt;examples&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;See more in-depth coverage of some of the extensibility points in posts from this site:

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.knockmeout.net/2011/09/ko-13-preview-part-1-native-template.html"&gt;Native template engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.knockmeout.net/2011/09/ko-13-preview-part-2-custom-binding.html"&gt;Custom binding providers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.knockmeout.net/2011/10/ko-13-preview-part-3-template-sources.html"&gt;Template sources&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.knockmeout.net/2011/11/knockout-13-release-candidate-new.html"&gt;Subscription notifications before/after change&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;KnockoutJS 2.0 is an exciting release.  The native templating and control-flow bindings allow an easier and streamlined way to change scope and repeat content.  It is no longer required to reference an external engine like the jQuery Templates plugin to do templating in Knockout.  Even jQuery is not a strict dependency.   The additional extensibility points allows developers to explore using Knockout in different ways, such as applying bindings in code rather than declaratively.&lt;/p&gt;

&lt;p&gt;I would expect a 2.0.x release in the near future that will include some further performance tweaks that are being explored, but overall 2.0 feels very solid.  Go try it now and please leave feedback &lt;a href="https://groups.google.com/forum/#!forum/knockoutjs"&gt;here&lt;/a&gt; or &lt;a href="https://github.com/SteveSanderson/knockout/issues?state=open"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/QOGF1PxPEwI" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2011/12/knockout-20-is-out.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Knockout 1.3 Release Candidate + a new feature]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/s2L-Rnegjt4/knockout-13-release-candidate-new.html" />
    <updated>2011-11-19T22:24:00-06:00</updated>
    <id>http://www.knockmeout.net/2011/11/knockout-13-release-candidate-new</id>
    <content type="html">&lt;p&gt;&lt;em&gt;Update: Knockout 1.3 was eventually renamed to version 2.0&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Just a quick note: the &lt;a href="http://knockoutjs.com"&gt;Knockout.js&lt;/a&gt; 1.3 release candidate is now available for testing.  Barring any last-minute fixes, this should be the final version.  Files are available from: &lt;a href="https://github.com/SteveSanderson/knockout/tree/master/build/output"&gt;https://github.com/SteveSanderson/knockout/tree/master/build/output&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Some links to 1.3 information:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blog.stevensanderson.com/2011/08/31/knockout-1-3-0-beta-available/"&gt;Steve’s overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.knockmeout.net/2011/09/ko-13-preview-part-1-native-template.html"&gt;Native template engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.knockmeout.net/2011/09/ko-13-preview-part-2-custom-binding.html"&gt;Binding providers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.knockmeout.net/2011/10/ko-13-preview-part-3-template-sources.html"&gt;Template sources&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Currently, I am working with Steve on documentation updates to &amp;lt;Knockoutjs.com&gt; before a final release.  Hopefully, it will be ready soon!&lt;/p&gt;

&lt;h2&gt;Controlling when to receive subscription notifications&lt;/h2&gt;

&lt;p&gt;There were was one feature added recently, which hasn’t been covered, that I wanted to briefly highlight.  Knockout 1.3 now allows you to decide when you want to be notified before or after a value has changed when creating a manual subscription.  The default behavior, as before, is that the subscription runs after the value has changed and passes the new value as the first argument.&lt;/p&gt;

&lt;p&gt;When creating a manual subscription, you can now opt to have your code run before the value has changed and receive the previous value as the first parameter.  To be notified before the value changes, the subscribe API now accepts a third parameter to specify an event name.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;beforeChange&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;If the event name is not passed, then it will default to &lt;code&gt;change&lt;/code&gt;, which is consistent with the current behavior of manual subscriptions.&lt;/p&gt;

&lt;p&gt;This can be useful to take action before a value has changed or to track changes to values.  Previously, you could have accomplished this with a writeable dependentObservable in front of the actual observable, but this allows a more direct way to accomplish this type of action.&lt;/p&gt;

&lt;p&gt;While &lt;code&gt;change&lt;/code&gt; and &lt;code&gt;beforeChange&lt;/code&gt; are the only events that Knockout uses itself, we are able to subscribe to custom events.  We would couple this with the use of the &lt;code&gt;notifySubscribers&lt;/code&gt; method on an observable to trigger messages for those events.  This could be useful in certain scenarios where your application wants to delegate responsibilities to other components.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;notifySubscribers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;valueRemoved&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="c1"&gt;//react to the value being removed&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;valueRemoved&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;As shown above, we can create an overall subscription to an observable that looks at its state and decides when to notify subscribers of any appropriate events.  These could be chained together and allow us to create components that expose specific events to subscribers.  Seems like a potentially useful feature to consider when creating a reusable library with Knockout.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/s2L-Rnegjt4" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2011/11/knockout-13-release-candidate-new.html</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[KO 1.3 preview part 3: template sources]]></title>
    <link href="http://feedproxy.google.com/~r/KnockMeOut/~3/GJsjKT1YCrw/ko-13-preview-part-3-template-sources.html" />
    <updated>2011-10-21T23:07:00-05:00</updated>
    <id>http://www.knockmeout.net/2011/10/ko-13-preview-part-3-template-sources</id>
    <content type="html">&lt;p&gt;&lt;em&gt;Update: Knockout 1.3 was eventually renamed to version 2.0&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://knockoutjs.com"&gt;Knockout&lt;/a&gt; 1.3 contains a number of enhancements that allow developers to extend the default capabilities of the library. Besides custom binding providers, you can now also create objects that define how to get and set a template’s contents based on the template name.  These objects are called templateSources.&lt;/p&gt;

&lt;h2&gt;Knockout’s default templateSources&lt;/h2&gt;

&lt;p&gt;Knockout includes two templateSources that are used by default. The first templateSource understands how to retrieve a template from a DOM element using the template name as the id of the element.  Typically, these are the templates that live in script tags.  The other deals with anonymous templates, where the children of the element are used as the template.  Anonymous templates are used when a name is not provided to the template binding and is most commonly used with the new control-flow bindings.&lt;/p&gt;

&lt;h2&gt;What does a templateSource look like?&lt;/h2&gt;

&lt;p&gt;A templateSource needs to provide a &lt;code&gt;text&lt;/code&gt; and a &lt;code&gt;data&lt;/code&gt; function.  Similar to an observable, the &lt;code&gt;text&lt;/code&gt; function when given no arguments should return the text of the template.  When passed a single parameter, the function should write that value to wherever the template is being stored.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;data&lt;/code&gt; function is used to associate additional metadata related to the template and should accept &lt;em&gt;key&lt;/em&gt; and &lt;em&gt;value&lt;/em&gt; parameters.  If only passed the key parameter, it should return the value.  If passed both arguments, then it should use the key to write the value to an appropriate location.  This meta-data is currently used for things like indicating that a template has been rewritten and to set/retrieve a precompiled version of a jQuery template.&lt;/p&gt;

&lt;h2&gt;A quick note about rewriting templates&lt;/h2&gt;

&lt;p&gt;When using a third-party engine, Knockout needs to take special care to ensure that the proper scope is available when applying bindings.  You might want to use variables that are exposed by the template engine in your data-bind attributes or you may be looping over arrays using the template engine’s syntax and adding bindings on elements inside the loop.  These are cases where Knockout can rewrite the template to allow bindings to execute under the proper context, as long as the template engine knows how to create a block to execute arbitrary JavaScript.  Note that the new native template engine does not require this rewriting.&lt;/p&gt;

&lt;h2&gt;Writing a quick custom templateSource&lt;/h2&gt;

&lt;p&gt;Suppose that we want to keep our templates in strings.  Maybe we want to load them from the server or even generate them on-the-fly.  For starters, we can maintain an object that stores our template text by key like:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;viewTemplate&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;lt;li data-bind=&amp;#39;text: name&amp;#39;&amp;gt;&amp;lt;/li&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;editTemplate&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;lt;li&amp;gt;&amp;lt;input data-bind=&amp;#39;value: name&amp;#39; /&amp;gt;&amp;lt;/li&amp;gt;&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;To create our templateSource, let’s first write a constructor function that takes in the name of our template and the object that holds our templates.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templateSources&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringTemplate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templateName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Next our templateSource needs to provide a &lt;code&gt;text&lt;/code&gt; method.  When passed no parameters, we need to return our template from the template object.  When passed a value (in the case of a rewritten template), we need to set the value on the template object.  This method can be as simple as:&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templateName&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;     &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;     &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templateName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Now we need a &lt;code&gt;data&lt;/code&gt; method for storing any meta-data about the template.  We could have our template object store &lt;em&gt;text&lt;/em&gt; and &lt;em&gt;data&lt;/em&gt; properties for each key, but to keep the template definitions simple, I am going to tuck the meta-data into a &lt;code&gt;_data&lt;/code&gt; key on the template object.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_data&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templateName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templateName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templateName&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templateName&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This method just ensures that the proper objects have been created and then does a get or set using the key.&lt;/p&gt;

&lt;p&gt;There is one last step that we need to perform to take advantage of our new type of templateSource.  We need to override the &lt;code&gt;makeTemplateSource&lt;/code&gt; function of a template engine.  To be flexible, we can create a function that takes in an existing template engine and the location of our string templates.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;createStringTemplateEngine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;templateEngine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;templateEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;makeTemplateSource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templateSources&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringTemplate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;templateEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Finally, we need to tell Knockout to use an appropriate template engine.&lt;/p&gt;

&lt;figure class='code'&gt; &lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='js'&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setTemplateEngine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;createStringTemplateEngine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ko&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nativeTemplateEngine&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templates&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Here is a sample using this technique:&lt;/p&gt;

&lt;iframe style="width: 100%; height: 250px" src="http://jsfiddle.net/3598P/embedded/result,js,html,css/light/"&gt;&lt;/iframe&gt;


&lt;p&gt;&lt;a href="http://jsfiddle.net/rniemeyer/3598P/"&gt;Link to sample on jsFiddle.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You could even extend this concept to create a template source that loads its contents from an external source.  Here is a sample that uses an observable to store the template contents, which allows the use of a &lt;em&gt;loading&lt;/em&gt; template while the real template is being retrieved:&lt;/p&gt;

&lt;iframe style="width: 100%; height: 250px" src="http://jsfiddle.net/MKFj6/embedded/result,js,html,css/light/"&gt;&lt;/iframe&gt;


&lt;p&gt;&lt;a href="http://jsfiddle.net/rniemeyer/MKFj6/"&gt;Link to sample on jsFiddle.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Knockout 1.3 definitely has some great new extensibility features.  While the default templateSources should be sufficient for most applications, it is nice to know that there is some flexibility built in to deal with templates in other ways.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/KnockMeOut/~4/GJsjKT1YCrw" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://www.knockmeout.net/2011/10/ko-13-preview-part-3-template-sources.html</feedburner:origLink></entry>
  
</feed>
