title: Progressive Enhancement with KnockoutJS
body: <p>Let's say we have data as follows</p>
  
  <pre><code>var data = {
    facets: [{
      name : "some name",
      values: [{
        value: "some value" 
      }]
    }]
  };
  </code></pre>
  
  <p>We can easily represent this as a view model bound to a knockout template as follows:</p>
  
  <pre><code>&lt;ul data-bind="foreach: facets"&gt;    
    &lt;li&gt;      
      &lt;span data-bind="text: name"&gt;&lt;/span&gt;
      &lt;ul data-bind="foreach: values"&gt;            
        &lt;li data-bind="text: value"&gt;&lt;/li&gt;     
      &lt;/ul&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>The question is, how can we achieve the same result when using progressive enhancement? That is by having the template render on the server side initially and then binding the knockout template and the view model to that rendering.</p>
  
  <p>A simple server side template would look something like this:</p>
  
  <pre><code>&lt;ul&gt;    
    &lt;li&gt;      
      &lt;span&gt;some name&lt;/span&gt;
      &lt;ul&gt;            
        &lt;li&gt;some value&lt;/li&gt;       
      &lt;/ul&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>I have explored a few different possibilities:</p>
  
  <ul>
  <li><p>One is create one knockout template and one server side template, and generate the Knockout view model dynamically by parsing the DOM for the server side template. In this way, only the Knockout template would be visible when JavaScript is enabled, and only the server side template would be visible if JavaScript is disabled. They could be styled in a way to make them look identical. </p></li>
  <li><p>Another approach is to apply bindings for each item in the facets array seperately to the existing DOM element for that facet. However this is still only one level deep and does not work for the nested elements.</p></li>
  </ul>
  
  <p>Neither of these approaches seem rather clean. Another solution might be to write a custom binding that handles the whole rendering and reuses existing elements if possible. </p>
  
  <p>Any other ideas?</p>