<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.codeville.net/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Steve Sanderson’s blog</title>
	
	<link>http://blog.stevensanderson.com</link>
	<description>As seen on YouTube™</description>
	<lastBuildDate>Wed, 09 May 2012 05:27:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.codeville.net/SteveCodeville" /><feedburner:info uri="stevecodeville" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Knockout 2.1.0 released</title>
		<link>http://feeds.codeville.net/~r/SteveCodeville/~3/XsGk1VKxMmo/</link>
		<comments>http://blog.stevensanderson.com/2012/05/09/knockout-2-1-0-released/#comments</comments>
		<pubDate>Wed, 09 May 2012 04:59:07 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Knockout]]></category>

		<guid isPermaLink="false">http://blog.stevensanderson.com/?p=452</guid>
		<description><![CDATA[Last night we released version 2.1.0 of knockout.js. This brings a whole bundle of added functionality and performance improvements. For a more detailed rundown of what’s new, see Ryan Niemeyer’s blog post. With this release we welcome new core team [...]]]></description>
			<content:encoded><![CDATA[<p>Last night we released version 2.1.0 of <a href="http://knockoutjs.com/">knockout.js</a>. This brings a whole bundle of added functionality and performance improvements. For a more detailed rundown of what’s new, see <a href="http://www.knockmeout.net/2012/05/knockout-2-dot-1-is-out.html">Ryan Niemeyer’s blog post</a>.</p>
<p>With this release we welcome new core team member <a href="https://github.com/mbest">Michael Best</a>, who implemented a lot of the 2.1.0 goodness. One of my favourite new features is the <strong>$index</strong> context variable. Here’s a short usage example:</p>
<p><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/HhXGH/embedded/result,html,js/" frameborder="0" allowfullscreen="allowfullscreen"></iframe></p>
<p>The cool thing is that <strong>$index</strong> is an observable and is updated automatically as you add or remove items in any collection bound to a <strong>foreach</strong> (and so your UI will update to match). Impressively, Michael was able to add this new functionality while simultaneously <em>improving </em>the performance of <strong>foreach</strong> whether or not you are using <strong>$index</strong>!</p>
<p>We also benefited from pull requests, issue reports, and feature suggestions from many others in the community. Special thanks to <a href="https://github.com/neonstalwart">neonstalwart</a> and <a href="https://github.com/mtscout6">mtscout6</a> for not only implementing AMD module loader support and putting up with a lot of questions from me, but even including <a href="http://knockoutjs.com/documentation/amd-loading.html">full documentation pages</a> in their pull request – flawless OSS etiquette!</p>
<p>Again, if you want to know more about what’s new in KO 2.1.0, head on over to <a href="http://www.knockmeout.net/2012/05/knockout-2-dot-1-is-out.html">Ryan’s post</a> for a full rundown.</p>
<div class="feedflare">
<a href="http://feeds.codeville.net/~ff/SteveCodeville?a=XsGk1VKxMmo:M2aYYhhRtfQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=XsGk1VKxMmo:M2aYYhhRtfQ:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=XsGk1VKxMmo:M2aYYhhRtfQ:G79ilh31hkQ"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=G79ilh31hkQ" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=XsGk1VKxMmo:M2aYYhhRtfQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=XsGk1VKxMmo:M2aYYhhRtfQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=XsGk1VKxMmo:M2aYYhhRtfQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SteveCodeville/~4/XsGk1VKxMmo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.stevensanderson.com/2012/05/09/knockout-2-1-0-released/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://blog.stevensanderson.com/2012/05/09/knockout-2-1-0-released/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=knockout-2-1-0-released</feedburner:origLink></item>
		<item>
		<title>Single Page Application packages and samples</title>
		<link>http://feeds.codeville.net/~r/SteveCodeville/~3/oeKSd9jWyI4/</link>
		<comments>http://blog.stevensanderson.com/2012/03/06/single-page-application-packages-and-samples/#comments</comments>
		<pubDate>Tue, 06 Mar 2012 18:44:55 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[SPA]]></category>

		<guid isPermaLink="false">http://blog.stevensanderson.com/?p=447</guid>
		<description><![CDATA[This post is for you if you saw my ASP.NET Single Page Applications presentation at recent conferences and would like to try out the code for yourself. Getting the post-beta tools update, and running the scaffolder First, make sure you [...]]]></description>
			<content:encoded><![CDATA[<p>This post is for you if you saw my <a href="http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2159">ASP.NET Single Page Applications presentation</a> at recent conferences and would like to try out the code for yourself. </p>
<h4>Getting the post-beta tools update, and running the scaffolder</h4>
<p>First, make sure you have <a href="http://www.asp.net/mvc/mvc4">ASP.NET MVC 4 Beta</a>. Then if you want to scaffold up a simple data-editing SPA,</p>
<ul>
<li>Create a new MVC 4 project. Choose the <strong>Single Page Application</strong> subtemplate.</li>
<li>Once you’ve created the new project, pop open NuGet package console (View &gt; Other Windows &gt; Package Manager Console), and install the latest scaffolder templates as folllows:</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="text">Install-Package SinglePageApplication.CSharp</pre></div></div>

<blockquote><p>Or, if you&#8217;re using Visual Basic instead of C#, install the VB package instead:</p>
</blockquote>

<div class="wp_syntax"><div class="code"><pre class="text">Install-Package SinglePageApplication.VisualBasic</pre></div></div>

<blockquote>
<p><font color="#000000"><font color="#555555"><strong><font color="#ff0000">Don’t skip this step</font></strong>, otherwise you’ll be using the old beta SPA templates and the result will be much less interesting.</font></font></p>
<p>Note that Visual Studio may give a false compiler error (“<em>The type or namespace name &#8216;MvcTextTemplateHost&#8217; could not be found</em>”) – you can either just ignore it, or restart Visual Studio to make it go away. This is an unfortunate limitation of the way we’re packaging this post-beta update, and won’t affect the final tooling.</p>
</blockquote>
<ul>
<li>Now you can follow the on-screen instructions in <strong>/Models/TodoItem.cs</strong> to scaffold a simple data-editing SPA. Use the Add Controller dialog as explained in the comments on that file, remembering to build the solution first, and then you can run the app and browse to <a href="http://localhost:whateverport/Tasks">http://localhost:whateverport/Tasks</a>.</li>
</ul>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2012/03/image.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://blog.stevensanderson.com/wp-content/uploads/2012/03/image_thumb.png" width="640" height="319"></a></p>
<p>If you want to learn what the point of all this is, see <a title="http://www.asp.net/single-page-application" href="http://www.asp.net/single-page-application">http://www.asp.net/single-page-application</a> or watch <a href="http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2159">this presentation</a>.</p>
<h4>Getting the DeliveryTracker sample</h4>
<p>If you want to see a slightly bigger and more fun app built with the same libraries and architecture, you can <a href="https://github.com/SteveSanderson/DeliveryTracker">get hold of the DeliveryTracker app that I built during the SPA presentation</a>. If you want to see the mobile UI, you’ll want to either use a real mobile to view it, or fake your useragent (instructions for <a href="http://my.opera.com/wikipedian/blog/2011/05/18/change-user-agent-string-in-ie9">IE</a> or <a href="http://googlesystem.blogspot.com/2011/12/changing-user-agent-new-google-chrome.html">Chrome</a> or <a href="http://superuser.com/questions/98798/how-do-i-change-firefoxs-user-agent-via-aboutconfig">Firefox</a>).</p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2012/03/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://blog.stevensanderson.com/wp-content/uploads/2012/03/image_thumb1.png" width="410" height="618"></a></p>
<p>Note that the source on GitHub doesn’t include the offline support, as that’s still very early in development &#8211; the design and functionality is still likely to change significantly.</p>
<div class="feedflare">
<a href="http://feeds.codeville.net/~ff/SteveCodeville?a=oeKSd9jWyI4:rXjlA0TPDUQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=oeKSd9jWyI4:rXjlA0TPDUQ:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=oeKSd9jWyI4:rXjlA0TPDUQ:G79ilh31hkQ"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=G79ilh31hkQ" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=oeKSd9jWyI4:rXjlA0TPDUQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=oeKSd9jWyI4:rXjlA0TPDUQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=oeKSd9jWyI4:rXjlA0TPDUQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SteveCodeville/~4/oeKSd9jWyI4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.stevensanderson.com/2012/03/06/single-page-application-packages-and-samples/feed/</wfw:commentRss>
		<slash:comments>59</slash:comments>
		<feedburner:origLink>http://blog.stevensanderson.com/2012/03/06/single-page-application-packages-and-samples/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=single-page-application-packages-and-samples</feedburner:origLink></item>
		<item>
		<title>Knockout 2.0.0 released</title>
		<link>http://feeds.codeville.net/~r/SteveCodeville/~3/MHecUaelnVs/</link>
		<comments>http://blog.stevensanderson.com/2011/12/21/knockout-2-0-0-released/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 12:40:15 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Knockout]]></category>

		<guid isPermaLink="false">http://blog.stevensanderson.com/?p=431</guid>
		<description><![CDATA[Here it is at last! Knockout 2.0.0 contains a huge set of improvements since the 1.2.x line. If you’re using KO already, see below for some of the highlights. New to all this? Now’s a great time to check it [...]]]></description>
			<content:encoded><![CDATA[<p>Here it is at last! Knockout 2.0.0 contains a huge set of improvements since the 1.2.x line. If you’re using KO already, see below for some of the highlights. </p>
<p>New to all this? Now’s a great time to check it out. Knockout is an MVVM library for JavaScript – it makes rich dynamic web UIs easier and cleaner to build. The best place to start learning is with the <a href="http://learn.knockoutjs.com" target="_blank">interactive tutorials</a>.</p>
<p><strong>What just happened?</strong></p>
<ul>
<li>The finished 2.0.0 build is <a href="https://github.com/SteveSanderson/knockout/downloads" target="_blank">now on GitHub</a> </li>
<li>All of the <a href="http://knockoutjs.com" target="_blank">documentation and live examples</a> are updated to reflect the new version </li>
<li>All of the <a href="http://learn.knockoutjs.com" target="_blank">interactive tutorials</a> are updated too. </li>
</ul>
<p>Credits go to the many community members who contributed pull requests, bug reports, new documentation, and support during this process. These people include <a href="http://github.com/aaronpowell">aaronpowell</a>, <a href="http://github.com/amonat">amonat</a>, <a href="http://github.com/antis">antis</a>, <a href="http://github.com/aredridel">aredridel</a>, <a href="http://github.com/artiomchi">artiomchi</a>, <a href="http://github.com/barkmadley">barkmadley</a>, <a href="http://github.com/b-dur">b-dur</a>, <a href="http://github.com/bmac">bmac</a>, <a href="http://github.com/cburgdorf">cburgdorf</a>, <a href="http://github.com/develop-me">develop-me</a>, <a href="http://github.com/DomenicDenicola">DomenicDenicola</a>, <a href="http://github.com/doodlemoonch">doodlemoonch</a>, <a href="http://github.com/dre2901">dre2901</a>, <a href="http://github.com/drewlesueur">drewlesueur</a>, <a href="http://github.com/ducka">ducka</a>, <a href="http://github.com/gigi81">gigi81</a>, <a href="http://github.com/GLuKKi">GLuKKi</a>, <a href="http://github.com/hunterloftis">hunterloftis</a>, <a href="http://github.com/ifandelse">ifandelse</a>, <a href="http://github.com/joeldart">joeldart</a>, <a href="http://github.com/kmalakoff">kmalakoff</a>, <a href="http://github.com/lcbarcellos">lcbarcellos</a>, <a href="http://github.com/mbest">mbest</a>, <a href="http://github.com/mtscout6">mtscout6</a>, <a href="http://github.com/neonstalwart">neonstalwart</a>, <a href="http://github.com/owenssam">owenssam</a>, <a href="http://github.com/Quaternion">Quaternion</a>, <a href="http://github.com/rniemeyer">rniemeyer</a>, <a href="http://github.com/RoyJacobs">RoyJacobs</a>, <a href="http://github.com/schinckel">schinckel</a>, <a href="http://github.com/seanami">seanami</a>, <a href="http://github.com/SimonBartlett">SimonBartlett</a>, <a href="http://github.com/studgeek">studgeek</a>, <a href="http://github.com/tehsenaus">tehsenaus</a>, and <a href="http://github.com/tiberiuana">tiberiuana</a> – and that’s just those who made recent pull requests (apologies for not tracking everyone who contributed other things). <a href="http://github.com/rniemeyer">rniemeyer</a> wrote a lot of documentation too.</p>
<p><strong>Why is it called 2.0.0? Why not 1.3.0?</strong></p>
<p>For a long time, we were planning this next version to be called 1.3.0. However,</p>
<ul>
<li>Quite a few community members are keen on adopting <a href="http://semver.org/" target="_blank">SemVer</a>-style versioning. 2.0.0 is a good place to start (expermentally) with that versioning convention. </li>
<li>It’s such a big set of core changes that if this doesn’t count as 2.0, I guess nothing ever would… </li>
</ul>
<h2>New features</h2>
<p>I’m mostly going to copy and paste the “what’s new” list from my earlier blog post about Knockout 1.3 beta, with a few further updates. Besides these things, there are all the usual bugfixes and performance improvements that you’d expect.</p>
<h4>1. Control flow bindings</h4>
<p>Previously, if you wanted to display a repeating sequence of UI elements (“foreach”), or have a section of the DOM exist only if some viewmodel condition was true (“if”), you’d need to create an entire template to define that. A bit cumbersome for something so simple and commonplace! Now you can use the new control flow bindings – <strong>if</strong>, <strong>ifnot</strong>, <strong>with</strong>, and <strong>foreach</strong> – to achieve basic, declarative control flow without the need for a template. This can make your code a lot more elegant and to-the-point.</p>
<p>Here’s an example of using “<strong>foreach</strong>” and “<strong>if</strong>”:</p>
<p><iframe style="width: 640px; height: 250px" src="http://jsfiddle.net/StevenSanderson/kR329/embedded/html,js,result/"></iframe></p>
<p><em>Tip: Switch to the “result” tab to see the output</em></p>
<p>The &quot;<strong>with</strong>&quot; binding changes the binding context to whatever object you specify. This makes it easy to compose many independent view models together – you have a host model that contains references to its children, and then use &quot;with&quot; to bind different sections of the page to different child models. Example:</p>
<p><iframe style="width: 640px; height: 365px" src="http://jsfiddle.net/StevenSanderson/yVBPS/embedded/html,js,result/"></iframe></p>
<p>Of course, the &quot;<strong>if</strong>&quot;, &quot;<strong>ifnot</strong>&quot;, &quot;<strong>foreach</strong>&quot;, and &quot;<strong>with</strong>&quot; bindings can all be combined and nested arbitrarily.</p>
<h4>2. Containerless control flow</h4>
<p>What if you want to set up template-less control flow (as in the above example), but <em>without</em> having to use extra container elements just to hold the &quot;<strong>foreach</strong>&quot;, &quot;<strong>if</strong>&quot;, etc., bindings? In that case, you can use the new comment-based control flow syntax like this:</p>
<p><iframe style="width: 640px; height: 250px" src="http://jsfiddle.net/StevenSanderson/8vms5/embedded/html,js,result/"></iframe></p>
<p>The comment-based control flow syntax works with the &quot;<strong>if</strong>&quot;, &quot;<strong>ifnot</strong>&quot;, &quot;<strong>foreach</strong>&quot;, &quot;<strong>with</strong>&quot;, and &quot;<strong>template</strong>&quot; bindings. In a large number of cases, this means you can skip string-based templates and use the binding-based control flows instead, which can run much faster depending on what exactly you’re doing.</p>
<h4>3. Access to parent binding contexts</h4>
<p>Whether you’re using regular nested templates, or nesting the new control flow blocks, sometimes you want to reference properties that exist at the upper levels of binding. There are four new pseudo-variables that you can use in any binding (inside or outside traditional templates):</p>
<ul>
<li><strong>$data</strong> – returns the current item </li>
<li><strong>$parent</strong> – returns the item from the parent binding context </li>
<li><strong>$parents</strong> – an array containing all the parent binding contexts. $parents[0] == $parent, then $parents[1] is the level above that, and so on. </li>
<li><strong>$root</strong> – returns the item at the top level of binding (usually your primary view model)</li>
</ul>
<p>Example:</p>
<p><iframe style="width: 640px; height: 250px" src="http://jsfiddle.net/StevenSanderson/W2Hrz/embedded/html,js,result/"></iframe></p>
<h4>4. Cleaner event handling</h4>
<p>In most cases, data-bind attributes can be kept clean and elegant. But one rough spot in the past has been event handlers, because to pass parameters, you were generally guided to use function literals. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;button data-bind=&quot;click: function() { viewModel.items.remove(this) }&quot;&gt;Click me&lt;/button&gt;</pre></div></div>

<p>Many of you rightly pointed out that this is pretty ugly. It’s much nicer if bindings can be kept minimal and declarative! To fix this, KO 2.0 offers two alternative event handling syntaxes, both of which are better.</p>
<p>Firstly, event handlers now receive the current model value as their first parameter. So, you can implement “remove-from-list” as follows, with no inline function literals:</p>
<p><iframe style="width: 640px; height: 250px" src="http://jsfiddle.net/StevenSanderson/caqva/embedded/html,js,result/"></iframe></p>
<p>The other option is to use jQuery event delegation to catch events and invoke functions on your viewmodel. This is made possible by the <strong>ko.dataFor</strong> and <strong>ko.contextFor</strong> helpers, which tell you which part of your viewmodel a given event relates to. Here’s the preceding example, but now using event delegation: </p>
<p><iframe style="width: 640px; height: 250px" src="http://jsfiddle.net/StevenSanderson/3knhM/embedded/html,js,result/"></iframe></p>
<h4>5. Binding providers (and hence external bindings)</h4>
<p>Most KO developers find the default <strong>data-bind</strong> attributes provide a very convenient, easy-to-maintain way of declaratively associating viewmodels with views. But if you would prefer to store your binding configuration elsewhere, the new <em>binding providers</em> API is an extensiblity point that allows alternative configuration mechanisms to be plugged in.</p>
<p>For example, here’s one possible alternative way that you could configure bindings. Notice the absence of data-bind attributes – I’m using a binding provider to set up the binding configuration in JavaScript:</p>
<p><iframe style="width: 640px; height: 250px" src="http://jsfiddle.net/StevenSanderson/2eY2u/embedded/html,js,result/"></iframe></p>
<p>To be clear, binding providers are a <em>general extensibility mechanism</em>, not any specific configuration format. The above example is just one possible way it can be used to define bindings in JavaScript. I’m looking forward to seeing suggestions from the community about how this extensibility can be used, and for what the most elegant and convenient way of defining bindings might be. I know some of you have already suggested externalising bindings (e.g., Brandon Satrom’s knockout.unobtrusive plugin); this new API will make it easier to implement custom binding mechanisms. Bring on more plugins!</p>
<h4>6. Throttling</h4>
<p align="center"><em><strong>Note:</strong> <strong>ko.computed</strong> is Knockout 2.0’s new name for <strong>ko.dependentObservable</strong> – it’s easier to say, type, and explain. This doesn’t affect backward compatibility, though – <strong>ko.dependentObservable</strong> still works, because it references the same function instance as <strong>ko.computed</strong> at runtime. You don’t have to change your existing code if you don’t want.</em></p>
<p>Occasionally, it’s desirable to limit how fast observables and computed observables update. For example, you might be using a ko.computed to invoke Ajax requests whenever a set of observables change. Sometimes you might want to change multiple underlying observables, but only have that ko.computed re-evaluate <em>once</em>, after all the changes (so that it only fires one Ajax request). Effectively, you want a kind of atomic update.</p>
<p>This is now pretty easy to do: you can apply an <em>extender</em> called &quot;<strong>throttle</strong>&quot; either to observables or to computed observables. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #006600; font-style: italic;">// Will not notify changes faster than once per 500ms </span>
<span style="color: #003366; font-weight: bold;">var</span> myObservable <span style="color: #339933;">=</span> ko.<span style="color: #660066;">observable</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;initial value&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> throttle<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Will not re-evaluate *or* notify changes faster than once per 500ms </span>
<span style="color: #003366; font-weight: bold;">var</span> myComputed <span style="color: #339933;">=</span> ko.<span style="color: #660066;">computed</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #006600; font-style: italic;">// Evaluation logic goes here, usually referencing other observables </span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">123</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> throttle<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The way this works is that, when throttled, notifications and evaluations are done <em>asynchronously</em>, and don’t occur at all until they stop being triggered for the specified throttle timeout duration.</p>
<p>In the case of making a computed value only update once even if there are multiple synchronous changes to its dependencies, you can use a very short timeout (for example, 1ms), and then it will re-evaluate as soon as possible after any series of synchronous changes to its dependencies. Example:</p>
<p><iframe style="width: 640px; height: 380px" src="http://jsfiddle.net/StevenSanderson/KxPMP/embedded/result,html,js"></iframe></p>
<h4>Breaking changes</h4>
<p>There are two changes that might realistically affect existing 1.2.x application code. Both are for good reasons and are important for the long-term health of the project…</p>
<ul>
<li><em>Event handlers now receive your model object as their first parameter, and the DOM event object as the second parameter</em>. Previously, they only received the DOM event object as the first parameter. So, if you are using the “click” or “event” bindings and are specifically catching and using the DOM event object, you’ll need to change your handler’s signature from this:</li>
</ul>
<blockquote>
<p><code>myViewModel.myEventHandler = function(evt) { /* do something with evt */ }</code></p>
<p>… to this:</p>
<p><code>myViewModel.myEventHandler = function(data, evt) { /* do something with evt */ }</code></p>
<p>This will <em>not</em> affect any event handlers that don’t specifically receive and process the “event” parameter. We chose to make this change because it significantly simplifies event handling in the majority of cases. Most developers have been able to upgrade to 2.0.0 beta without changing their applications at all.</p>
</blockquote>
<ul>
<li><em>KO 2.0.0 drops support for very old versions of jquery-tmpl.</em> If you need to use string-based templating (and most often you won’t now that KO 2.0.0 has native control flow bindings), get <a href="https://github.com/jquery/jquery-tmpl" target="_blank">jquery-tmpl 1.0.0pre</a>. But also consider dropping the string-based templating, and switching to control flow bindings instead, as jquery-tmpl is no longer in active development.</li>
</ul>
<h2>Summary</h2>
<p>Try the updated <a href="http://learn.knockoutjs.com/" target="_blank">interactive tutorials</a>, update your existing KO applications, have a great Christmas/vacation/newyear <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blog.stevensanderson.com/wp-content/uploads/2011/12/wlEmoticon-smile.png" /></p>
<div class="feedflare">
<a href="http://feeds.codeville.net/~ff/SteveCodeville?a=MHecUaelnVs:d45Dbe1N-QU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=MHecUaelnVs:d45Dbe1N-QU:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=MHecUaelnVs:d45Dbe1N-QU:G79ilh31hkQ"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=G79ilh31hkQ" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=MHecUaelnVs:d45Dbe1N-QU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=MHecUaelnVs:d45Dbe1N-QU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=MHecUaelnVs:d45Dbe1N-QU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SteveCodeville/~4/MHecUaelnVs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.stevensanderson.com/2011/12/21/knockout-2-0-0-released/feed/</wfw:commentRss>
		<slash:comments>47</slash:comments>
		<feedburner:origLink>http://blog.stevensanderson.com/2011/12/21/knockout-2-0-0-released/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=knockout-2-0-0-released</feedburner:origLink></item>
		<item>
		<title>Full-height app layouts: Navigation and History</title>
		<link>http://feeds.codeville.net/~r/SteveCodeville/~3/viyh6xic_FM/</link>
		<comments>http://blog.stevensanderson.com/2011/11/04/full-height-app-layouts-navigation-and-history/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 12:23:24 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Knockout]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://blog.stevensanderson.com/?p=413</guid>
		<description><![CDATA[This is the third in a series of posts about web app layouts, i.e., giving your web-based application a UI comparable to a desktop or mobile/tablet app. Posts so far: Layout basics – A CSS technique for slicing up the [...]]]></description>
			<content:encoded><![CDATA[<link rel="stylesheet" type="text/css" href="http://stevesanderson.github.com/fixed-height-layouts-demo/css/phone-mockups.css" />
<p>This is the third in a series of posts about <em>web app layouts</em>, i.e., giving your web-based application a UI comparable to a desktop or mobile/tablet app. Posts so far:</p>
<ol>
<li><a href="http://blog.stevensanderson.com/?p=393"><strong>Layout basics</strong></a> – A CSS technique for slicing up the browser window into arbitrarily nested panes both horizontally and vertically. You know, like a proper application, and not like an infinite-height document… </li>
<li><a href="http://blog.stevensanderson.com/?p=403"><strong>Animated transitions</strong></a> – How to put more than one content block into a given pane, and then switch between them with hardware-accelerated animations </li>
<li><strong>This post</strong> – Keeping track of what content has appeared in each pane, so the visitor can navigate back and forwards. And supporting deep linking. And injecting new panes dynamically. </li>
</ol>
<p><em>Disclaimer: To be clear, this post series just represents my own experiments in lightweight, flexible web app layouts and is not an official part of any Microsoft technology stack. No guarantees or warranties or SLAs, blah blah blah.</em></p>
<h4>The goal</h4>
<p>It’s very common on mobile/tablet-like UIs for users to navigate through some structure of content within a given pane. For example, in a tablet-like app, you’ll often want to have a left-hand pane representing navigation through a hierarchy, with the main section of the screen representing the currently selected item or folder:</p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2011/11/image.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="image" border="0" alt="image" src="http://blog.stevensanderson.com/wp-content/uploads/2011/11/image_thumb.png" width="581" height="448" /></a></p>
<p>So, you’ll want to be able to:</p>
<ul>
<li>Keep track of where the user has been, letting them go back and forwards, with each pane able to maintain its own independent history. </li>
<li>Perform smooth animated transitions within each pane when any navigation event occurs. </li>
<li>Support deep-linking to arbitrary locations in your content structure </li>
<li>Fetch and render content dynamically </li>
</ul>
<h4>Basic navigation</h4>
<p>Continuing from previous posts that introduced <a href="http://blog.stevensanderson.com/?p=393">pane hierarchies</a> and <a href="http://blog.stevensanderson.com/?p=403">panes.js</a>, it’s pretty easy to keep track of where a visitor has been. First you might set up a basic pane layout with a header/body/footer like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;div class=&quot;page&quot;&gt;
    &lt;div class=&quot;header row&quot;&gt;
        Header will go here
    &lt;/div&gt;
    &lt;div class=&quot;body row&quot;&gt;
        Body contents will go here
    &lt;/div&gt;
    &lt;div class=&quot;footer row&quot;&gt;
        My footer. Could put icons here.
    &lt;/div&gt;
&lt;/div&gt;</pre></div></div>

<p>… and then put multiple panes into the body row:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;div class=&quot;body row&quot;&gt;
    &lt;div id=&quot;location-continents&quot; class=&quot;pane&quot;&gt;
        &lt;h3&gt;Continents&lt;/h3&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;#america&quot;&gt;America&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;#europe&quot;&gt;Europe&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&nbsp;
    &lt;div id=&quot;location-america&quot; class=&quot;pane&quot;&gt;
        &lt;h3&gt;Countries in the Americas&lt;/h3&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;#canada&quot;&gt;Canada&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;#usa&quot;&gt;USA&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&nbsp;
    &lt;div id=&quot;location-canada&quot; class=&quot;pane&quot;&gt;
        &lt;h3&gt;Cities in Canada&lt;/h3&gt;
        &lt;ul&gt;
            &lt;li&gt;Vancouver&lt;/li&gt;
            &lt;li&gt;Toronto&lt;/li&gt;
            &lt;li&gt;Edmonton&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&nbsp;
    &lt;!-- ... etc ... --&gt;
&lt;/div&gt;</pre></div></div>

<p>Now you can begin history tracking by adding a bit of JavaScript to instantiate a PaneHistory object and navigate to an initial pane:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #006600; font-style: italic;">// Navigation</span>
<span style="color: #003366; font-weight: bold;">var</span> paneHistory <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> PaneHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
paneHistory.<span style="color: #000066;">navigate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;location-continents&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>… and perform pane navigations each time the visitor clicks on one of the links:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> dest <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">srcElement</span> <span style="color: #339933;">||</span> evt.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">href</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
    paneHistory.<span style="color: #000066;">navigate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;location-&quot;</span> <span style="color: #339933;">+</span> dest<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This code will intercept clicks on the links, figure out which pane they are trying to get to, and then animate that destination pane sliding smoothly into the body area from the right. </p>
<p>If you also want to let the visitor go “back” through the pane’s history stack, add a button perhaps into the page header:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;div class=&quot;header row&quot;&gt;
    &lt;p&gt;&lt;button class=&quot;goBack&quot;&gt;&amp;lt; Back&lt;/button&gt;&lt;/p&gt;
&lt;/div&gt;</pre></div></div>

<p>… and handle clicks by instructing the PaneHistory instance to perform a reverse navigation animation:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.goBack&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    paneHistory.<span style="color: #000066;">back</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That’s it. Try it out. Live example:</p>
<div class="handset">
<div><iframe src="http://stevesanderson.github.com/fixed-height-layouts-demo/navigation.html" frameborder="0"></iframe></div>
</div>
<p align="center">Also: <a href="http://stevesanderson.github.com/fixed-height-layouts-demo/navigation.html">Run full screen</a> (e.g., to try it on a phone)</p>
<h4>Supporting the back/forward buttons and deep-linking</h4>
<p>If your app will be deployed to the web (as opposed to using something like PhoneGap to package for an appstore), then you will almost certainly want to respect the browser’s native back/forward buttons and allow deep-linking to specific locations in your virtual navigation system.</p>
<p>There are many JavaScript libraries for working with browser history and the HTML5 pushState feature. Currently my favourite is <a href="https://github.com/balupton/History.js/">history.js</a> (license: New BSD) by Benjamin Lupton, because of its robustness, great pushState support, support for older browsers, and because it has no dependencies. </p>
<p>panes.js integrates with history.js, so once you’ve added a reference to history.js, you can start using UrlLinkedPaneHistory instead of PaneHistory. Specify the name of one or more URL parameters that the pane will use to represent what data it is showing. In this case, I’ll call my parameter “location”:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> paneHistory <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> UrlLinkedPaneHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    params<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> location<span style="color: #339933;">:</span> <span style="color: #3366CC;">'continents'</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then, update your paneHistory.navigate call so that it specifies which URL parameter is to be updated (in this case, my only parameter, “location”):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> dest <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">srcElement</span> <span style="color: #339933;">||</span> evt.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">href</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    paneHistory.<span style="color: #000066;">navigate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> location<span style="color: #339933;">:</span> dest <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And finally, since it no longer makes sense for “back” clicks to affect only a single pane (the URL history is global), update your “back” button handler so that it performs a global browser “back” navigation:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.goBack&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    History.<span style="color: #000066;">back</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>… and you’re done. Now if you run the follow demo full-screen (click the link below the fake phone UI), you’ll see the URL update as you navigate around, and you can use the back/forward buttons (still getting the animated transitions), and can bookmark or refresh the page without losing your position. If your browser supports HTML5 pushState, for example recent versions of Chrome and Firefox, then your URLs will be updated with real querystring parameters as you go. If your browser doesn&#8217;t support pushState, history.js will gracefully degrade to using a URL hash.</p>
<p>Try it:</p>
<div class="handset">
<div><iframe src="http://stevesanderson.github.com/fixed-height-layouts-demo/navigation-urlhistory.html" frameborder="0"></iframe></div>
</div>
<p>
<div align="center"><strong>Note:</strong> If you want to be able to see the URL updating,</div>
<div align="center">and to use the back/forward buttons, <a href="http://stevesanderson.github.com/fixed-height-layouts-demo/navigation-urlhistory.html">view this example full screen</a></div>
</p>
<h4>Dynamically generating panes</h4>
<p>If you do a <em>view HTML source </em>on either of the two previous demos, you’ll see that there’s a long list of pre-prepared &lt;DIV&gt; elements – one for each pane that you might visit (Europe, Canada, Vancouver, Toronto, etc….). That’s OK if your app only has a small and finite set of visitable panes (e.g., because those panes represent a fixed number of tabs), but it’s awkward if there are a lot of panes, and useless if the number is effectively infinite because panes represent navigation through data in some large external database.</p>
<p>Fortunately, this is easy to fix. Because panes.js doesn’t modify your DOM structure in any way, and requires no initialisation step to start using newly-inserted DOM elements, it composes perfectly with any external mechanism for updating the DOM. In this example, I’m going to use Knockout.js to inject new panes dynamically as data is fetched from some external source.</p>
<p>The trick here is to make the UrlLinkedPaneHistory a property of your Knockout view model. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> AppViewModel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">paneHistory</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> UrlLinkedPaneHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        params<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> location<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Making the set of history entries observable, so we can dynamically generate corresponding DIVs</span>
        entries<span style="color: #339933;">:</span> ko.<span style="color: #660066;">observableArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Each time the visitor navigates forwards, this will be called to fetch data for the new pane</span>
        loadPaneData<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>params<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// Here I'm loading data from some external source, asynchronously</span>
            locationInfoService.<span style="color: #660066;">getLocationInfoAsync</span><span style="color: #009900;">&#40;</span>params.<span style="color: #660066;">location</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
ko.<span style="color: #660066;">applyBindings</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> AppViewModel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><i>(Note: do a &#8220;view source&#8221; on the live example below if you want to see the finished code and what JS libraries you must reference to make all this work.)</i></p>
<p>Now the set of history entries is observable, we can ask Knockout to generate corresponding DIVs dynamically, by using a “foreach” binding:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;div class=&quot;body row&quot; data-bind=&quot;foreach: paneHistory.entries&quot;&gt;
    &lt;div id=&quot;location-continents&quot; class=&quot;pane scroll-y&quot; data-bind=&quot;attr: { id: paneId }&quot;&gt;
        &lt;h3 data-bind=&quot;text: paneData.name&quot;&gt;&lt;/h3&gt;
    &lt;/div&gt;
&lt;/div&gt;</pre></div></div>

<p>Notice that Knockout will assign an ID property to each pane &lt;DIV&gt; corresponding to the navigation parameters, so it can associate navigation events with the &lt;DIV&gt; you want to slide into view.</p>
<p>Of course, you don’t just want to display the “name” property of each item you’re visiting – you want to generate some more UI for each pane. Let’s generate a list of child locations that the user can navigate to:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;div class=&quot;body row&quot; data-bind=&quot;foreach: paneHistory.entries&quot;&gt; 
    &lt;div id=&quot;location-continents&quot; class=&quot;pane scroll-y&quot; data-bind=&quot;attr: { id: paneId }&quot;&gt; 
        &lt;h3 data-bind=&quot;text: paneData.name&quot;&gt;&lt;/h3&gt; 
        &lt;ul data-bind=&quot;foreach: paneData.children&quot;&gt; 
            &lt;li data-bind=&quot;text: name, 
                            click: $root.navigate, 
                            css: { navigable: childLocations.length }&quot;&gt;&lt;/li&gt; 
        &lt;/ul&gt; 
    &lt;/div&gt; 
&lt;/div&gt;</pre></div></div>

<p>Each of the &lt;li&gt; elements there will try to invoke a viewmodel method called “navigate” when you click it. Implement that by adding a “navigate” method to your viewmodel:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> AppViewModel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Rest of class unchanged</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">navigate</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> destination <span style="color: #339933;">=</span> ko.<span style="color: #660066;">dataFor</span><span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">srcElement</span> <span style="color: #339933;">||</span> evt.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>destination.<span style="color: #660066;">childLocations</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">paneHistory</span>.<span style="color: #000066;">navigate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> location<span style="color: #339933;">:</span> destination.<span style="color: #660066;">id</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> .<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now the visitor will be able to navigate forwards through the hierarchy of locations. What about navigating backwards too? You can put a “back” button into the header pane, and use a Knockout binding to make its text update to show the name of parent location (e.g., “&lt; Europe”):</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;div class=&quot;header row&quot;&gt;
    &lt;p data-bind=&quot;with: paneHistory.currentData().parent&quot;&gt;
        &lt;button data-bind=&quot;click: $root.navigate&quot;&gt;&amp;lt; &lt;span data-bind=&quot;text: name&quot;&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;/p&gt;
&lt;/div&gt;</pre></div></div>

<p>Done. Your visitor can now navigate through an arbitrarily deep hierarchy, with the data being pulled from the server as needed and dynamically rendered on the client. The browser’s back/forward buttons still work, deep linking works, and there are smooth animated transitions. And you wrote about <em>15 lines</em> of JavaScript <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blog.stevensanderson.com/wp-content/uploads/2011/11/wlEmoticon-smile.png" />. Have a go with it:</p>
<div class="handset">
<div><iframe src="http://stevesanderson.github.com/fixed-height-layouts-demo/navigation-dynamic-content.html" frameborder="0"></iframe></div>
</div>
<p>
<div align="center"><strong>Note:</strong> If you want to be able to see the URL updating,</div>
<div align="center">and to use the back/forward buttons, <a href="http://stevesanderson.github.com/fixed-height-layouts-demo/navigation-dynamic-content.html">view this example full screen</a></div>
</p>
<h4>What’s next?</h4>
<p>Well, what are you interested in? Possible next posts in this series:</p>
<ul>
<li>Using all this stuff to build a complete application with meaningful functionality </li>
<li>Making this kind of Single Page Application (SPA) work offline via HTML5 offline support </li>
<li>Packaging and selling such apps on mobile appstores via PhoneGap/Callback </li>
<li>Data access: editing collections of entities, and letting the user track, synchronise, and revert their changes </li>
</ul>
<p>If you’re building single page applications, whether for mobile or desktop or both, please <strong>let me know what sort of technologies you’re using, what challenges you face, and what kinds of features you’d like to see baked into the ASP.NET/MVC stack</strong>.</p>
<div class="feedflare">
<a href="http://feeds.codeville.net/~ff/SteveCodeville?a=viyh6xic_FM:XD4h9B2mTGU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=viyh6xic_FM:XD4h9B2mTGU:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=viyh6xic_FM:XD4h9B2mTGU:G79ilh31hkQ"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=G79ilh31hkQ" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=viyh6xic_FM:XD4h9B2mTGU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=viyh6xic_FM:XD4h9B2mTGU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=viyh6xic_FM:XD4h9B2mTGU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SteveCodeville/~4/viyh6xic_FM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.stevensanderson.com/2011/11/04/full-height-app-layouts-navigation-and-history/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		<feedburner:origLink>http://blog.stevensanderson.com/2011/11/04/full-height-app-layouts-navigation-and-history/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=full-height-app-layouts-navigation-and-history</feedburner:origLink></item>
		<item>
		<title>Full-height app layouts: Animated transitions within panes</title>
		<link>http://feeds.codeville.net/~r/SteveCodeville/~3/lj3F4VkVV88/</link>
		<comments>http://blog.stevensanderson.com/2011/10/12/full-height-app-layouts-animated-transitions-within-panes/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 15:48:45 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://blog.stevensanderson.com/?p=403</guid>
		<description><![CDATA[In the previous post, I showed a simple yet very useful and robust CSS technique for creating full-height layouts for web-based applications. To recap, it means you can subdivide both the width and height of the screen into a arbitrarily [...]]]></description>
			<content:encoded><![CDATA[<link href="http://stevesanderson.github.com/fixed-height-layouts-demo/css/phone-mockups.css" rel="stylesheet" type="text/css" />
<p>In the <a href="http://blog.stevensanderson.com/?p=393">previous post</a>, I showed a simple yet very useful and robust CSS technique for creating full-height layouts for web-based applications. To recap, it means you can subdivide both the width <em>and</em> height of the screen into a arbitrarily nested set of panes, each of which can scroll independently. This is useful whether you’re building apps for desktop browsers, for tablets, or for phones.</p>
<p><img style="display: block; float: none; margin-left: auto; margin-right: auto" alt="image" src="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image_thumb1.png" /></p>
<p>Not amazingly exciting just yet, but there’s more… <img src='http://blog.stevensanderson.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p>One of the other reasons for defining pane positioning using those particular CSS rules is that it becomes trivial to have multiple different content panes that can populate any given pane container, then to change which pane is visible within a container, and even to animate the transitions during those changes.</p>
<p>For example, here’s a layout involving a fixed header, a fixed footer, and two scrollable body regions (laid out using the same CSS rules as defined in the previous post):</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;div class=&quot;header row&quot;&gt;
    &lt;h2&gt;My header&lt;/h2&gt;
&lt;/div&gt;
&lt;div class=&quot;body row&quot;&gt;
    &lt;div class=&quot;first pane scroll-y&quot;&gt;
        &lt;p&gt;This is the first body view&lt;/p&gt;
    &lt;/div&gt;
    &lt;div class=&quot;second pane scroll-y&quot;&gt;
        &lt;p&gt;This is the second body view&lt;/p&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;footer row&quot;&gt;
    &lt;p&gt;My footer. Could put icons here.&lt;/p&gt;
&lt;/div&gt;</pre></div></div>

<p>This will lay out as follows, with the first body pane and second body pane occupying the same area on the screen:</p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image6.png"><img style="display: block; float: none; margin-left: auto; margin-right: auto" title="image" alt="image" src="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image_thumb6.png" width="401" height="336" /></a></p>
<p>Then, you can trivially switch between the two body panes just by controlling their visibility (e.g., by using <strong>$(&quot;.first.pane&quot;).hide()</strong> and <strong>$(&quot;.second.pane&quot;).show()</strong>).</p>
<h4>Animating the transitions</h4>
<p>Instead of just hiding and showing panes instantly, you can cause them to fade in/out or even to slide in and out. This pretty well replicates an aspect of the UI experience familiar to users of touch-based smartphones and tablets. </p>
<p>However, JavaScript-based animation mechanisms (such as $.animate in jQuery) aren’t as smooth as I’d like. They tend to give low frame rates, as the browser has to run custom JavaScript to compute the positions of elements for each frame. Fortunately since we’re controlling the layout purely using CSS, it’s both easy and robust to use pure CSS 3 animated transitions, giving silky-smooth results on devices that support hardware accelerated CSS transforms (e.g., iPhone and iPad), exactly like a native application.</p>
<p>To make this easy to reuse, I created <a href="http://stevesanderson.github.com/fixed-height-layouts-demo/script/panes.js">a small bit of JavaScript boilerplate</a> (about 1kb gzipped) that exposes functions for triggering pane transitions, being sure to take full advantage of hardware acceleration where available. For example, you can write:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #006600; font-style: italic;">// Initial pane visibility</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.first.pane&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">showPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Navigation between panes</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.first.pane button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clickOrTouch</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.second.pane&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">showPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> slideFrom<span style="color: #339933;">:</span> <span style="color: #3366CC;">'right'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.second.pane button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clickOrTouch</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.first.pane&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">showPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> slideFrom<span style="color: #339933;">:</span> <span style="color: #3366CC;">'left'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>… and then if you click any button in the first body pane, the second body pane will slide in smoothly from the right, and vice-versa. Note that although this code uses looks as if it uses jQuery, it doesn’t – it actually uses <a href="http://xuijs.com/">XUI</a> instead (a very lightweight JavaScript framework that implements a fraction of the jQuery API in a fraction of the space), but you could equally do similarly with jQuery.</p>
<p>The boilerplate code offers the following transitions:</p>
<ul>
<li><strong>slideFrom</strong> – causes the target pane to slide in from the specified direction, and simultaneously the current pane to slide out in the opposite direction</li>
<li><strong>coverFrom</strong> – causes the target pane to slide in from the specified direction. The current pane stays still, getting covered over by the target pane.</li>
<li><strong>uncoverTo</strong> – causes the current pane to slide out in the specified direction, revealing the target pane underneath</li>
<li><strong>default</strong> – instantly shows the target pane and hides the current pane</li>
</ul>
<p>… and it works on IE7+ (including WP7 Mango), iOS, Firefox, Chrome, Safari, Opera, and probably others. For browsers that support hardware-based CSS3 transforms, like Safari on iOS 4+, it will be used and is impeccably smooth; others will use unaccelerated CSS3 transforms, and for those that don’t support CSS3 transforms, it falls back on JavaScript-based animation.</p>
<p>Also notice the “clickOrTouch” event – on browsers that support touch events (e.g., iOS Safari), this fires the instant your finger comes into contact with the screen (and hence is noticably faster than a regular click), whereas on non-touch devices, it’s equivalent to a regular click (i.e., when you release the mouse).</p>
<h4>Runnable examples</h4>
<p>Here’s a phone-styled example just showing how you can transition the contents within a given pane, or you can transition the contents of the entire screen. Note that this is an iframe you can interact with, not just a pretty picture <img src='http://blog.stevensanderson.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="handset">
<div><iframe src="http://stevesanderson.github.com/fixed-height-layouts-demo/pane-transitions-phone.html" frameborder="0"></iframe></div>
</div>
<p align="center">Also: <a href="http://stevesanderson.github.com/fixed-height-layouts-demo/pane-transitions-phone.html">Run full screen (e.g., to try it on a phone)</a></p>
<p>Similarly, here’s a tablet-styled example showing how transitions work just the same in arbitrary sub-panes as well. </p>
<div class="tablet">
<div><iframe src="http://stevesanderson.github.com/fixed-height-layouts-demo/pane-transitions-tablet.html" frameborder="0"></iframe></div>
</div>
<p align="center">Also: <a href="http://stevesanderson.github.com/fixed-height-layouts-demo/pane-transitions-tablet.html">Run full screen (e.g., to try it on a tablet device)</a></p>
<p><b>Note:</b> To be clear, I&#8217;m not starting a new open source project here. These are experiments in mobile-friendly app layouts &#8211; I&#8217;m blogging the results just in case it&#8217;s useful to someone else! In the next post, I&#8217;ll focus on managing navigation history within each pane, so you can easily let a visitor go &#8220;back&#8221; and &#8220;forwards&#8221;.</p>
<div class="feedflare">
<a href="http://feeds.codeville.net/~ff/SteveCodeville?a=lj3F4VkVV88:BYp82Um8FZ0:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=lj3F4VkVV88:BYp82Um8FZ0:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=lj3F4VkVV88:BYp82Um8FZ0:G79ilh31hkQ"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=G79ilh31hkQ" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=lj3F4VkVV88:BYp82Um8FZ0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=lj3F4VkVV88:BYp82Um8FZ0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=lj3F4VkVV88:BYp82Um8FZ0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SteveCodeville/~4/lj3F4VkVV88" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.stevensanderson.com/2011/10/12/full-height-app-layouts-animated-transitions-within-panes/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		<feedburner:origLink>http://blog.stevensanderson.com/2011/10/12/full-height-app-layouts-animated-transitions-within-panes/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=full-height-app-layouts-animated-transitions-within-panes</feedburner:origLink></item>
		<item>
		<title>Full-height app layouts: A CSS trick to make it easier</title>
		<link>http://feeds.codeville.net/~r/SteveCodeville/~3/qTM4LdyCZU0/</link>
		<comments>http://blog.stevensanderson.com/2011/10/05/full-height-app-layouts-a-css-trick-to-make-it-easier/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 15:24:47 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://blog.stevensanderson.com/?p=393</guid>
		<description><![CDATA[In HTML layouts, there’s a fundamental difference between width and height. The natural state of affairs is that your page’s width is constrained to the width of the browser window, but its height is unconstrained – it grows however tall [...]]]></description>
			<content:encoded><![CDATA[<p>In HTML layouts, there’s a fundamental difference between <em>width</em> and <em>height</em>. The natural state of affairs is that your page’s width is constrained to the width of the browser window, but its height is unconstrained – it grows however tall is necessary to contain its contents.</p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image.png"><img style="display: block; float: none; margin-left: auto; margin-right: auto" title="image" alt="image" src="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image_thumb.png" width="311" height="192" /></a></p>
<p>That makes sense for <em>documents</em>, but it’s a pain for <em>application UIs</em>. Native application UIs tend to slice up the screen both horizontally and vertically into a nested set of panels, some of which may scroll/resize, others being docked against particular edges of their parent panes.</p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image1.png"><img style="display: block; float: none; margin-left: auto; margin-right: auto" title="image" alt="image" src="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image_thumb1.png" width="325" height="260" /></a></p>
<p>So, what’s the robust way, with HTML/CSS, to set up a nested collection of panes that exactly divide both the width and height of the browser window?</p>
<h4>Hang on, didn’t we have this one solved back in 1999?</h4>
<p>Hmm… this reminds me of something… I remember: the <strong>&lt;frameset&gt;</strong> tag from <em>HTML 4</em>. Yes, HTML frames do divide the browser window both horizontally and vertically, exactly consuming the available screen area. So why don’t we use them any more? There are loads of reasons, including:</p>
<ul>
<li>What you’re building is logically one page, but technically each frame is a separate HTML document, which makes interactions between them so much more complex</li>
<li>It’s not really practical to support deep-linking to (i.e., bookmarking) particular UI states</li>
<li>Mobile devices and tablets have very limited support for HTML 4 frames. iOS in particular requires the user to use two-fingered scrolling within panes. This is UX death.</li>
</ul>
<h4>OK, so what’s the 21st century solution?</h4>
<p>I’ve used lots of hacks and tricks over the years to get columns and panes into my web applications, often involving JavaScript, <strong>$(…).height(…)</strong>, <strong>window.body.clientHeight</strong>, and the <strong>onresize</strong> event. Ugh. Fragile and messy. But at long last this week I learned there is actually an elegant and robust way to set up nested exact-height panes with pure CSS, and it works on all browsers back to IE 7, even on current mobile browsers that <em>don’t </em>support position:fixed.</p>
<p>You know that if you set <strong>position: absolute</strong> on an element, then you can make it appear at a specified distance from the top, left, right, or bottom from its parent element. Well, it turns out that you can specify both <strong>top </strong><em>and </em><strong>bottom</strong>, or both <strong>left</strong> <em>and</em> <strong>right</strong>, and then it will dock against both edges and always resize to match its parent’s dimensions. </p>
<p>Let’s define some generic CSS rules for panes:</p>

<div class="wp_syntax"><div class="code"><pre class="css"><span style="color: #808080; font-style: italic;">/* Generic pane rules */</span>
body <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.row</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.col</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.row</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.col</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.scroll-x</span> <span style="color: #00AA00;">&#123;</span> overflow-x<span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.scroll-y</span> <span style="color: #00AA00;">&#123;</span> overflow-y<span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Now it’s easy to set up a fixed-height header, variable-height body, and fixed-height footer:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;body&gt;
    &lt;div class=&quot;header row&quot;&gt;
        &lt;h2&gt;My header&lt;/h2&gt;
    &lt;/div&gt;
&nbsp;
    &lt;div class=&quot;body row scroll-y&quot;&gt;
        The body
    &lt;/div&gt;
&nbsp;
    &lt;div class=&quot;footer row&quot;&gt;
        My footer
    &lt;/div&gt;
&lt;/body&gt;</pre></div></div>

<p>Of course, you also have to configure the heights and positions of the three rows by using a bit more CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="css"><span style="color: #6666ff;">.header</span><span style="color: #6666ff;">.row</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.body</span><span style="color: #6666ff;">.row</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.footer</span><span style="color: #6666ff;">.row</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The result? It’s the classic mobile phone app layout. Screenshot with some colours and content added for interest:</p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image2.png"><img style="margin: 0px auto; display: block; float: none" title="image" alt="image" src="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image_thumb2.png" width="264" height="480" /></a></p>
<p><a href="http://stevesanderson.github.com/fixed-height-layouts-demo/simple.html">Try it in your desktop or mobile browser here</a>. Works on IE7+.</p>
<h4>More nesting (or, tablet-style layouts)</h4>
<p>The reason for setting up the generic CSS rules in that way is that it makes it easy nest panes arbitrarily. For example, you could split the main viewport into two vertical columns:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;body&gt;
    &lt;div class=&quot;left col&quot;&gt;
        Left col here    
    &lt;/div&gt;
    &lt;div class=&quot;right col&quot;&gt;
        Right col here
    &lt;/div&gt;
&lt;/body&gt;</pre></div></div>

<p>… and then subdivide the right column so it has a fixed header, variable-height body, and fixed footer, just by putting some more <strong>row</strong> divs inside it:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;div class=&quot;right col&quot;&gt;
    &lt;div class=&quot;header row&quot;&gt;
        View or edit something
    &lt;/div&gt;
    &lt;div class=&quot;body row scroll-y&quot;&gt;
        Here’s some content that can scroll vertically
    &lt;/div&gt;
    &lt;div class=&quot;footer row&quot;&gt;
        Some status message here
    &lt;/div&gt;
&lt;/div&gt;</pre></div></div>

<p>Here’s the CSS to configure the widths/heights/positions of those panes:</p>

<div class="wp_syntax"><div class="code"><pre class="css"><span style="color: #6666ff;">.left</span><span style="color: #6666ff;">.col</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">250px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.right</span><span style="color: #6666ff;">.col</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">250px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.header</span><span style="color: #6666ff;">.row</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.body</span><span style="color: #6666ff;">.row</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.footer</span><span style="color: #6666ff;">.row</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Going further, in the right-hand pane’s body, you could also nest a horizontally-scrollable row:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;div class=&quot;body row scroll-y&quot;&gt;
    &lt;p&gt;Here's a horizontally-scrollable thing:&lt;/p&gt;
&nbsp;
    &lt;div class=&quot;scroll-x&quot;&gt;
        Any content in here, if it's too wide, becomes independently scrollable
    &lt;/div&gt;
&nbsp;
    &lt;p&gt;That's enough - bye.&lt;/p&gt;
&lt;/div&gt;</pre></div></div>

<p>The result of all this? Well, it’s a structure like this:</p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2011/10/layout.png"><img style="display: block; float: none; margin-left: auto; margin-right: auto" title="image" alt="image" src="http://blog.stevensanderson.com/wp-content/uploads/2011/10/layout.png" width="423" height="300" /></a></p>
<p>… but that’s pretty boring to look at, so here’s a version where I threw in a rough effort of some iPad-like styling:</p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image4.png"><img style="display: block; float: none; margin-left: auto; margin-right: auto" title="image" alt="image" src="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image_thumb4.png" width="824" height="768" /></a></p>
<p><a href="http://stevesanderson.github.com/fixed-height-layouts-demo/two-columns.html">Here’s a live example</a>. It still renders correctly on very small screens (like a WP7 or iPhone) but this 2-column layout really needs a wider screen to be usable.</p>
<h4>Enabling touch scrolling</h4>
<p>The scrolling looks and works fine on a desktop browser, but on a mobile browser it varies:</p>
<ul>
<li>On WP7, you’ll see no scrollbars, but you’ll get one-finger touch scrolling, <em>without</em> the lovely inertia/momentum effect. This is kind-of OK, though imperfect.</li>
<li>On iOS, you’ll see no scrollbars, and it requires <strong>two-finger scrolling</strong> (which is horrible), and it won’t use the inertia/momentum effect either. Badness.</li>
<li>There are similar problems on Android</li>
</ul>
<p>With iOS 5, it will be possible to enable fluid, <em>native</em> touch-based momentum scrolling to our divs just by making a tiny tweak to the CSS, thanks to <a href="http://johanbrook.com/browsers/native-momentum-scrolling-ios-5/">the new “touch scrolling” feature</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="css"><span style="color: #6666ff;">.scroll-x</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.scroll-y</span> <span style="color: #00AA00;">&#123;</span> -webkit-overflow-scrolling<span style="color: #00AA00;">:</span> touch<span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>I really hope this catches on and becomes a standard.</p>
<p>But in the meantime, for visitors on other mobile OSes, and until iOS 5 becomes prevalent, you can use the open-source <a href="http://cubiq.org/iscroll-4">iScroll</a> library that provides one-finger momentum scrolling for Webkit (iOS and Android) and Mozilla browsers. </p>
<p>Enabling momentum scrolling on any given element requires only one line of JavaScript (assuming you’ve referenced iScroll.js):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">new</span> iScroll<span style="color: #009900;">&#40;</span>theElementYouWantToEnableItFor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>For my example, I used the following block of JavaScript, which enables touch scrolling on all the .scroll-x and .scroll-y elements:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;!-- Touch scrolling --&gt;
&lt;!--[if !IE]&gt;&lt;!--&gt;
&lt;script src=&quot;script/iscroll.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
    var xScrollers = document.getElementsByClassName(&quot;scroll-x&quot;);
    for (var i = 0; i &lt; xScrollers.length; i++)
        new iScroll(xScrollers[i], { vScroll: false });
&nbsp;
    var yScrollers = document.getElementsByClassName(&quot;scroll-y&quot;);
    for (var i = 0; i &lt; yScrollers.length; i++)
        new iScroll(yScrollers[i], { hScroll: false });                
&lt;/script&gt;
&lt;!--&lt;![endif]--&gt;</pre></div></div>

<p>You could do this in fewer lines if you’re using a library like <a href="http://jquery.com/">jQuery</a> or <a href="http://xuijs.com/">XUI</a> (which is a tiny implementation of a small part of the jQuery API surface, intended for mobiles). Here’s the resulting mobile-style scrollbar:</p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image5.png"><img style="display: block; float: none; margin-left: auto; margin-right: auto" title="image" alt="image" src="http://blog.stevensanderson.com/wp-content/uploads/2011/10/image_thumb5.png" width="311" height="311" /></a></p>
<p>Of course, to see the momentum effect, you’ll need to <a href="http://stevesanderson.github.com/fixed-height-layouts-demo/touch-scrolling.html">run it in your Chrome/Safari/Firefox browser</a>.</p>
<p><strong>Credits:</strong> Thanks to <a href="http://www.alistapart.com/articles/conflictingabsolutepositions/">Rob Swan</a> and <a href="https://github.com/fellowshiptech/slablet">FellowshipTech</a> for their articles and projects where I found the CSS positioning trick that underlies this approach to exact-height layout.</p>
<div class="feedflare">
<a href="http://feeds.codeville.net/~ff/SteveCodeville?a=qTM4LdyCZU0:J8-sNKQNgzg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=qTM4LdyCZU0:J8-sNKQNgzg:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=qTM4LdyCZU0:J8-sNKQNgzg:G79ilh31hkQ"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=G79ilh31hkQ" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=qTM4LdyCZU0:J8-sNKQNgzg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=qTM4LdyCZU0:J8-sNKQNgzg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=qTM4LdyCZU0:J8-sNKQNgzg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SteveCodeville/~4/qTM4LdyCZU0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.stevensanderson.com/2011/10/05/full-height-app-layouts-a-css-trick-to-make-it-easier/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		<feedburner:origLink>http://blog.stevensanderson.com/2011/10/05/full-height-app-layouts-a-css-trick-to-make-it-easier/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=full-height-app-layouts-a-css-trick-to-make-it-easier</feedburner:origLink></item>
		<item>
		<title>Knockout 1.3.0 Beta Available</title>
		<link>http://feeds.codeville.net/~r/SteveCodeville/~3/HMvAdZAbJeM/</link>
		<comments>http://blog.stevensanderson.com/2011/08/31/knockout-1-3-0-beta-available/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 10:06:32 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Knockout]]></category>

		<guid isPermaLink="false">http://blog.stevensanderson.com/?p=371</guid>
		<description><![CDATA[Knockout version 1.3.0 is coming soon, and it&#8217;s going to be a huge release with a big stack of features that many of you have asked for. The key theme is developer happiness &#8211; the new features will let you [...]]]></description>
			<content:encoded><![CDATA[<p><img style="margin: 0px 15px 0px 0px; display: inline; float: left" alt="icon" align="left" src="http://blog.stevensanderson.com/wp-content/uploads/2011/07/icon-thumb.png" />Knockout version 1.3.0 is coming soon, and it&#8217;s going to be a huge release with a big stack of features that many of you have asked for. </p>
<p>The key theme is <em>developer happiness</em> &#8211; the new features will let you get more done with less code, eliminate some previous limitations, and offer more power to extend and customize how things work.</p>
<p style="padding-bottom: 1em; background-color: #ddd; padding-left: 1em; padding-right: 1em; margin-bottom: 1em; padding-top: 1em"><strong>New to Knockout?</strong> If you’ve never used it before, don’t start with this post – start with the <a href="http://learn.knockoutjs.com" target="_blank"><b>interactive tutorials</b></a>.</p>
<p>The 1.3 code is now stable and feature-complete, so it’s time for a beta release. You can get it from…</p>
<ul>
<li><strong>For the JavaScript files</strong>, get the <a href="http://cloud.github.com/downloads/SteveSanderson/knockout/knockout-1.3.0beta.js">minified version</a> (13kb gzipped, for production use) and/or the <a href="http://cloud.github.com/downloads/SteveSanderson/knockout/knockout-1.3.0beta.debug.js">debug version</a> </li>
<li><strong>For the source</strong>, head over to <a href="https://github.com/SteveSanderson/knockout">the GitHub repo</a> </li>
</ul>
<p>For Visual Studio users, the NuGet package ID is <strong>knockoutjs</strong>, and this latest version is 1.2.9.0 (reserving version 1.3.0 for the final 1.3 release).</p>
<p>So, what’s new? Well, full documentation for the new features is still to be written, but here&#8217;s a brief rundown of the main enhancements.</p>
<h4>1. Control flow bindings</h4>
<p>Previously, if you wanted to display a repeating sequence of UI elements (“foreach”), or have a section of the DOM exist only if some viewmodel condition was true (“if”), you’d need to create an entire template to define that. A bit cumbersome for something so simple and commonplace! Now you can use the new control flow bindings &#8211; <strong>if</strong>, <strong>ifnot</strong>, <strong>with</strong>, and <strong>foreach</strong> &#8211; to achieve basic, declarative control flow without the need for a template. This can make your code a lot more elegant and to-the-point.</p>
<p>Here’s an example of using “<strong>foreach</strong>” and “<strong>if</strong>”:</p>
<p><iframe style="width: 640px; height: 230px" src="http://jsfiddle.net/StevenSanderson/nvYdf/3/embedded/html,js,result/"></iframe></p>
<p><em>Tip: Switch to the “result” tab to see the output</em></p>
<p>The &quot;<strong>with</strong>&quot; binding changes the binding context to whatever object you specify. This makes it easy to compose many independent view models together &#8211; you have a host model that contains references to its children, and then use &quot;with&quot; to bind different sections of the page to different child models. Example:</p>
<p><iframe style="width: 640px; height: 370px" src="http://jsfiddle.net/StevenSanderson/f5w6u/3/embedded/html,js,result/"></iframe></p>
<p>Of course, the &quot;<strong>if</strong>&quot;, &quot;<strong>ifnot</strong>&quot;, &quot;<strong>foreach</strong>&quot;, and &quot;<strong>with</strong>&quot; bindings can all be combined and nested arbitrarily.</p>
<h4>2. Containerless control flow</h4>
<p>What if you want to set up template-less control flow (as in the above example), but <em>without</em> having to use extra container elements just to hold the &quot;<strong>foreach</strong>&quot;, &quot;<strong>if</strong>&quot;, etc., bindings? In that case, you can use the new comment-based control flow syntax like this:</p>
<p><iframe style="width: 640px; height: 260px" src="http://jsfiddle.net/StevenSanderson/BN5C9/embedded/html,js,result/"></iframe></p>
<p>The comment-based control flow syntax works with the &quot;<strong>if</strong>&quot;, &quot;<strong>ifnot</strong>&quot;, &quot;<strong>foreach</strong>&quot;, &quot;<strong>with</strong>&quot;, and &quot;<strong>template</strong>&quot; bindings. In a large number of cases, this means you can skip string-based templates and use the binding-based control flows instead, which can run much faster depending on what exactly you&#8217;re doing.</p>
<h4>3. Access to parent binding contexts</h4>
<p>Whether you&#8217;re using regular nested templates, or nesting the new control flow blocks, sometimes you want to reference properties that exist at the upper levels of binding. There are four new pseudo-variables that you can use in any binding (inside or outside traditional templates):</p>
<ul>
<li><strong>$data</strong> &#8211; returns the current item </li>
<li><strong>$parent</strong> &#8211; returns the item from the parent binding context </li>
<li><strong>$parents</strong> &#8211; an array containing all the parent binding contexts. $parents[0] == $parent, then $parents[1] is the level above that, and so on. </li>
<li><strong>$root</strong> &#8211; returns the item at the top level of binding (usually your primary view model) </li>
</ul>
<p>Example:</p>
<p><iframe style="width: 640px; height: 250px" src="http://jsfiddle.net/StevenSanderson/wKRSu/embedded/html,js,result/"></iframe></p>
<h4>4. jQuery-style event handling</h4>
<p>In most cases, data-bind attributes can be kept clean and elegant. But one rough spot in the past has been event handlers, because to pass parameters, you were generally guided to use function literals. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;button data-bind=&quot;click: function() { viewModel.items.remove(this) }&quot;&gt;Click me&lt;/button&gt;</pre></div></div>

<p>Many of you rightly pointed out that this is pretty ugly. It&#8217;s much nicer if bindings can be kept minimal and declarative! To fix this, KO 1.3 lets you replace that with:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;button class=&quot;remove-item&quot;&gt;Click me&lt;/button&gt;</pre></div></div>

<p>&#8230; and then in your JavaScript, add this (if you&#8217;re using jQuery):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.remove-item&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    viewModel.<span style="color: #660066;">items</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span>ko.<span style="color: #660066;">dataFor</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are two new helper functions that make this possible:</p>
<ul>
<li><strong>ko.dataFor(<em>domElement</em>)</strong> &#8211; returns the data item associated with a particular DOM element </li>
<li><strong>ko.contextFor(<em>domElement</em>)</strong> &#8211; returns the entire binding context associated with the DOM element, so you can retrieve its <strong>$parent</strong> property, etc. </li>
</ul>
<p>Example:</p>
<p><iframe style="width: 640px; height: 265px" src="http://jsfiddle.net/StevenSanderson/nb5Dh/embedded/html,js,result/"></iframe></p>
<h4>5. Binding providers (and hence external bindings)</h4>
<p>Most KO developers find the default <strong>data-bind</strong> attributes provide a very convenient, easy-to-maintain way of declaratively associating viewmodels with views. But if you would prefer to store your binding configuration elsewhere, the new <em>binding providers</em> API is an extensiblity point that allows alternative configuration mechanisms to be plugged in.</p>
<p>For example, here’s one possible alternative way that you could configure bindings. Notice the absence of data-bind attributes – I’m using a binding provider to set up the binding configuration in JavaScript:</p>
<p><iframe style="width: 640px; height: 330px" src="http://jsfiddle.net/StevenSanderson/n7h2A/1/embedded/html,js,result/"></iframe></p>
<p>To be clear, binding providers are a <em>general extensibility mechanism</em>, not any specific configuration format. The above example is just one possible way it can be used to define bindings in JavaScript. I’m looking forward to seeing suggestions from the community about how this extensibility can be used, and for what the most elegant and convenient way of defining bindings might be. I know some of you have already suggested externalising bindings (e.g., Brandon Satrom’s knockout.unobtrusive plugin); this new API will make it easier to implement custom binding mechanisms. Bring on more plugins!</p>
<h4>6. Throttling</h4>
<p>Occasionally, it&#8217;s desirable to limit how fast observables and dependent observables update. For example, you might be using a dependentObservable to invoke Ajax requests whenever a set of observables change. Sometimes you might want to change multiple underlying observables, but only have that dependentObservable re-evaluate <em>once</em>, after all the changes (so that it only fires one Ajax request). Effectively, you want a kind of atomic update.</p>
<p>This is now pretty easy to do: you can apply an <em>extender</em> called &quot;<strong>throttle</strong>&quot; either to observables or to dependent observables. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #006600; font-style: italic;">// Will not notify changes faster than once per 500ms </span>
<span style="color: #003366; font-weight: bold;">var</span> myObservable <span style="color: #339933;">=</span> ko.<span style="color: #660066;">observable</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;initial value&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> throttle<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Will not re-evaluate *or* notify changes faster than once per 500ms </span>
<span style="color: #003366; font-weight: bold;">var</span> myDependentObservable <span style="color: #339933;">=</span> ko.<span style="color: #660066;">dependentObservable</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #006600; font-style: italic;">// Evaluation logic goes here, usually referencing other observables </span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">123</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> throttle<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The way this works is that, when throttled, notifications and evaluations are done <em>asynchronously</em>, and don&#8217;t occur at all until they stop being triggered for the specified throttle timeout duration.</p>
<p>In the case of making a dependent observable only update once even if there are multiple synchronous changes to its dependencies, you can use a very short timeout (for example, 1ms), and then it will re-evaluate as soon as possible after any series of synchronous changes to its dependencies. Example:</p>
<p><iframe style="width: 640px; height: 350px" src="http://jsfiddle.net/StevenSanderson/Rnmb2/1/embedded/result,js,html/"></iframe></p>
<p>This is all I have time to write about just now, but there are also some other new extensibility features I haven&#8217;t mentioned. I&#8217;ll follow up with more as soon as I can.</p>
<h4>Backward compatibility notes</h4>
<p>This beta version of Knockout 1.3 should work with your existing applications, with the following exceptions:</p>
<ul>
<li>It has dropped compatibility with <strong><em>old</em> versions of jquery.tmpl</strong>. If you&#8217;re using a version of jquery.tmpl older than 1.0.0pre, you&#8217;ll need to upgrade. You can find it at <a href="https://github.com/jquery/jquery-tmpl">https://github.com/jquery/jquery-tmpl</a>. </li>
<li>The API for registering <strong>custom template engines</strong> has changed, so any existing custom ones will need to be updated. The change makes it much easier to define custom view engines (you don&#8217;t have to worry about &quot;rewriting&quot; any more). I&#8217;ll write a separate post about this soon. </li>
<li>Since the binding algorithm has been enhanced in many ways, any<strong> third-party plugins that specifically try to alter how bindings work</strong> may not work immediately (for example, the &quot;namespaces&quot; plugin). I&#8217;ll be talking to the creators of those plugins to ensure both KO and the plugins are updated to work together properly. Apologies for any inconvenience! The new extensibility APIs should make it much easier to implement these kinds of plugins now anyway. </li>
</ul>
<p>Please try it out and let me know what you think of the new features!</p>
<div class="feedflare">
<a href="http://feeds.codeville.net/~ff/SteveCodeville?a=HMvAdZAbJeM:f7RL4MLw-_8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=HMvAdZAbJeM:f7RL4MLw-_8:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=HMvAdZAbJeM:f7RL4MLw-_8:G79ilh31hkQ"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=G79ilh31hkQ" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=HMvAdZAbJeM:f7RL4MLw-_8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=HMvAdZAbJeM:f7RL4MLw-_8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=HMvAdZAbJeM:f7RL4MLw-_8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SteveCodeville/~4/HMvAdZAbJeM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.stevensanderson.com/2011/08/31/knockout-1-3-0-beta-available/feed/</wfw:commentRss>
		<slash:comments>69</slash:comments>
		<feedburner:origLink>http://blog.stevensanderson.com/2011/08/31/knockout-1-3-0-beta-available/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=knockout-1-3-0-beta-available</feedburner:origLink></item>
		<item>
		<title>Review: Open-source components used in learn.knockoutjs.com</title>
		<link>http://feeds.codeville.net/~r/SteveCodeville/~3/tQ33cYyUhPk/</link>
		<comments>http://blog.stevensanderson.com/2011/07/22/review-open-source-components-used-in-learnknockoutjs/#comments</comments>
		<pubDate>Fri, 22 Jul 2011 20:21:50 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.stevensanderson.com/2011/07/22/review-open-source-components-used-in-learnknockoutjs/</guid>
		<description><![CDATA[Over the last two months I’ve been using my wife-approved weekend coding time to build learn.knockoutjs.com, a set of interactive in-browser coding exercises to help people learn the basics of knockout.js in a convenient and fun way. If you haven’t [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last two months I’ve been using my wife-approved weekend coding time to build <a href="http://learn.knockoutjs.com">learn.knockoutjs.com</a>, a set of interactive in-browser coding exercises to help people learn the basics of knockout.js in a convenient and fun way. If you haven’t already seen it, grab a hot drink and treat yourself to 15 minutes of enjoyable learning right now!</p>
<p><a href="http://learn.knockoutjs.com/"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="/wp-content/uploads/2011/07/image.png" width="644" height="324" /></a></p>
<p>Since this was a one-person weekend project, I’ve been using all possible means to minimise the total development time. A key part of that has been using existing open source components so I can avoid reinventing stuff wherever possible. </p>
<p>In the end, I was surprised to realise just how many open source projects went into building this single-page application, and how high-quality and easy-to-use most of them were. So, I’d like to give them a bit of recognition by listing them here and giving you a mini-review of each. Maybe you’ll benefit from a few of these in your own projects.</p>
<h4>1: Knapsack</h4>
<p><a href="http://aboutcode.net/knapsack/">Knapsack</a> <font size="1"><strong>(NuGet: knapsack)</strong></font> is <a href="https://twitter.com/#!/andrewdavey">Andrew Davey</a>’s JavaScript / CSS dependency manager for ASP.NET. It lets you define a series of JS/CSS references like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">Html<span style="color: #008000;">.</span><span style="color: #0000FF;">ReferenceScript</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Scripts/Lib/knockout-1.3.0.latest.js&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Html<span style="color: #008000;">.</span><span style="color: #0000FF;">ReferenceScript</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Scripts/Custom/bindings.js&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Html<span style="color: #008000;">.</span><span style="color: #0000FF;">ReferenceScript</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Scripts/Custom/appViewModel.js&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Html<span style="color: #008000;">.</span><span style="color: #0000FF;">ReferenceStylesheet</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Content/App/boilerplate.css&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// ... etc ...</span></pre></div></div>

<p>… and then emit the actual &lt;script&gt; / &lt;link rel=&quot;stylesheet&quot;&gt; tags like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">@Html<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderScripts</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span></pre></div></div>

<p>What’s the point? Well, in <strong>debug </strong>mode, it just emits one &lt;script&gt; tag per referenced script, as usual, so you can use your browser’s JavaScript debugger as always. But in <strong>release</strong> mode, it emits just one &lt;script&gt; tag pointing to a special URL that returns <em>all</em> of the JavaScript code combined into a single file, minified using the <a href="http://aspnet.codeplex.com/releases/view/40584">MS Ajax Minifier</a> library. So, if you <em>view source</em> on learn.knockoutjs.com, you’ll see only one or two &lt;script&gt; tags even though I’m actually using dozens of JS files.</p>
<p>It also sets all the right cache/etag headers so the browser can avoid making unnecessary HTTP requests, but doesn’t risk using out-of-date scripts because the dynamic script URL changes whenever you change the referenced script files (the URL includes a hash of your JavaScript source code).</p>
<p>I’ve been very happy with knapsack, and would definitely recommend it. The one caveat I discovered is that it automatically references <em>all</em> JavaScript files in any folder that you point it at, which is surprising if you intentionally didn’t reference one of the files in that folder. That aside, if you can organise your JS/CSS files into folders to match how they should be combined, you’ll probably be very happy with Knapsack too.</p>
<p>Note: Knapsack will soon be changing its name to something else due to a cease-and-desist.</p>
<h4>2: YamlSerializer</h4>
<p>As part of my <em>minimum viable product</em> kind of philosophy for the site, I didn’t create any kind of editor for the tutorials, and I didn’t even store them in a database. The simplest editor is a text editor, and the simplest data store is files on disk. So, the tutorials are written in <a href="http://www.yaml.org/">YAML</a> – a human-editable structured text format. </p>
<p>To parse these YAML files, I used <a href="http://yamlserializer.codeplex.com/">YamlSerializer</a> <font size="1"><strong>(NuGet: yamlserializer)</strong></font>, a .NET library written by Osamu Takeuchi. So, I have .NET classes like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Tutorial
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Title <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> ICollection<span style="color: #008000;">&lt;</span>tutorialStep<span style="color: #008000;">&gt;</span> Steps <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008080; font-style: italic;">// ... various other properties ...</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> TutorialStep
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Instructions <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008080; font-style: italic;">// ... other properties ...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>… and YAML files like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text">title: Introduction
steps:
 - instructions: |
    ##Welcome!
    Blah blah blah, here's what to do...
 - instructions: |
    ##Doing the next thing
    Do the following things...</pre></div></div>

<p>… and YamlSerializer parses Tutorial/TutorialStep instances like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">Dictionary data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> YamlSerializer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Deserialize</span><span style="color: #008000;">&#40;</span>allText<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Tutorial <span style="color: #008000;">&#123;</span>
    Title <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>data<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;title&quot;</span><span style="color: #008000;">&#93;</span>,
    Steps <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> stepDict <span style="color: #0600FF; font-weight: bold;">in</span> <span style="color: #008000;">&#40;</span>Dictionary<span style="color: #008000;">&#41;</span>data<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;steps&quot;</span><span style="color: #008000;">&#93;</span>
             <span style="color: #0600FF; font-weight: bold;">select</span> <span style="color: #008000;">new</span> TutorialStep <span style="color: #008000;">&#123;</span>
                 Instructions <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>stepDict<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;instructions&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
                 <span style="color: #008080; font-style: italic;">// ... other properties ...</span>
             <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
    <span style="color: #008080; font-style: italic;">// ... other properties ...</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>It’s maybe not quite as pretty as it could be with a more generics-oriented API (or maybe even a dynamic one?) but it’s robust, fast enough (though I do cache the parsed result), and I had it working in minutes. So I’m satisfied with that!</p>
<h4>3: Ninject</h4>
<p><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 2px 0px 5px 10px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="right" src="/wp-content/uploads/2011/07/image-thumb.png" width="244" height="143" /></p>
<p>I didn’t strictly need to use Dependency Injection in this project, but since it takes exactly 10 seconds to set up thanks to NuGet, I did it anyway. My favourite DI container is <a href="http://ninject.org/">Ninject</a> <font size="1"><strong>(NuGet: Ninject.MVC3)</strong></font>, a very easy-to-configure product from <a href="http://twitter.com/#!/nkohari">Nate Kohari</a>. The MVC 3 version hooks itself into the MVC 3 Dependency Injection extensibility point automatically (internally, it uses <a href="http://twitter.com/#!/davidebbo">David Ebbo</a>’s <a href="https://bitbucket.org/davidebbo/webactivator/wiki/Home">WebActivator</a> to make itself run when your app first starts).</p>
<p>In this site, it’s only used to supply ITutorialRepository instances to my one-and-only HomeController. Really I could have just hardcoded the dependency, as there is only one tutorial repository (YamlTutorialRepository), but hey it genuinely did take less than one minute to set up the DI and it made me feel less like I was just hacking things together…</p>
<h4>4: CodeMirror</h4>
<p><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="right" src="/wp-content/uploads/2011/07/image-thumb1.png" width="244" height="63" /></p>
<p>See those lovely in-browser code editors with context-specific syntax highlighting, the ones that even work on IE6? That’s <a href="http://codemirror.net/">CodeMirror</a>, a JavaScript library from <a href="http://twitter.com/#!/marijnjh">Marijn Haverbeke</a>. </p>
<p>This was not particularly easy to get working just as I wanted – I hit a range of browser-specific performance problems mainly because of my compulsive need to put rounded corners and dropshadows on every element on the page. Nonetheless, it’s a completely vital component, the entire site couldn’t exist without it, <em>and</em> it’s an impressive technical achievement in its own right, so I’m willing it to give it plenty of slack. </p>
<p>The one alternative code editor I tried was <a href="http://ace.ajax.org/">Ace</a> – it looks very promising, but had serious layout issues in IE (e.g, if you resize the font). In the future Ace might overtake CodeMirror to become the smartest and best-performing in-browser code editor, but it’s not there yet.</p>
<h4>5: Amplify.js</h4>
<p><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="right" src="/wp-content/uploads/2011/07/image-thumb2.png" width="244" height="55" /></p>
<p>Did you notice that if you work on one of the tutorials, then leave the tutorial (possibly even the whole site), and then later come back, the site will offer to restore your code and your position in that tutorial? How can this be, given that you didn’t even register for an account? It’s <em>HTML 5 local storage</em>, of course.</p>
<p>To implement this, I used <a href="http://amplifyjs.com/">Amplify.js</a> <font size="1"><strong>(NuGet: amplifyjs)</strong></font>, a JavaScript library from <a href="http://appendto.com/">appendTo</a>. Of all the JavaScript libraries I used, this one was the easiest to get working. It was completely trivial, in fact – just about three lines of code in total.</p>
<p>Whenever you run your code, the site saves your state:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">amplify.<span style="color: #660066;">store</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tutorialState.&quot;</span> <span style="color: #339933;">+</span> tutorial.<span style="color: #660066;">Id</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> view<span style="color: #339933;">:</span> ...<span style="color: #339933;">,</span> viewModel<span style="color: #339933;">:</span> ...<span style="color: #339933;">,</span> stepIndex<span style="color: #339933;">:</span> ... <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Similarly, whenever you load a tutorial, the site checks if you have saved state:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> savedState <span style="color: #339933;">=</span> amplify.<span style="color: #660066;">store</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tutorialState.&quot;</span> <span style="color: #339933;">+</span> id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>… and if so, offers to restore it. The site doesn’t care what browser you’re running, because Amplify.js figures out what local storage options exist and uses whichever one is going to work for you.</p>
<h4>6: json2.js</h4>
<p>Amplify.js needs your browser to support JSON.stringify, the JavaScript API for serializing an object to JSON. All decent browsers do, but just in case you’re running IE 7 (What kind of web developer are you? Shame on you!), I reference <a href="https://github.com/douglascrockford/JSON-js">json2.js</a> <font size="1"><strong>(NuGet: json2)</strong></font> which provides an implementation of JSON.stringify. Json2.js is the work of legendary JavaScript guru and JSON inventer, <a href="http://crockford.com/">Douglas Crockford</a>.</p>
<h4>7: Highlight.js</h4>
<p align="left">In the tutorial instructions, there are lots of little snippets of code showing you what to do. For the syntax highlighting here, I used <a href="http://softwaremaniacs.org/soft/highlight/en/">highlight.js</a> from <a href="http://twitter.com/#!/isagalaev">Ivan Sagalaev</a>. </p>
<p>There are dozens of code-highlighting JavaScript libraries, and I’ve no reason to say this one is better than the rest. I chose this one simply because it lets me “highlight” a certain DOM element at an arbitrary time of my choosing, and doesn’t get fussy about doing things when the page first loads. Also it tolerates existing HTML markup in the element you’re highlighting, which is how I was able to put special yellow backgrounds on selected bits of code to emphasise the key points.</p>
<p>Highlight.js was pretty easy to work with. I made a Knockout <a href="http://learn.knockoutjs.com/#/?tutorial=custombindings">custom binding</a> to display the tutorial steps, and whenever it updates the UI, it highlights any code blocks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pre code'</span><span style="color: #339933;">,</span> element<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    hljs.<span style="color: #660066;">highlightBlock</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'    '</span> <span style="color: #009966; font-style: italic;">/* tab = four spaces */</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To customise the colour scheme, you just edit its CSS file. Pretty convenient.</p>
<h4>8: Showdown.js</h4>
<p>I said the tutorials are written in YAML, but that’s just how the .NET objects are represented as text. To represent the actual markup within the instructions (e.g., the headings, links, code blocks, etc.), I used <a href="http://daringfireball.net/projects/markdown/">Markdown</a>, a simple text-formatting DSL invented by <a href="http://daringfireball.net/">John Gruber</a>. So I’ve got “markdown” syntax within my YAML files, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text"> - instructions: |
    ##How to write markdown
    To emphasise text, use *asterisks*. To include a code snippet, just indent it like this:
&nbsp;
        var blah = &quot;this is some JavaScript&quot;
&nbsp;
    You can put bits of text into code font `using backtick quotes`.</pre></div></div>

<p>That markdown text gets sent to the client (in JSON form, of course – so many text-based formats!). To render it as HTML in the browser, I use <a href="http://softwaremaniacs.org/playground/showdown-highlight/">showdown.js</a>, a Markdown-to-HTML conversion library from John Fraser. This is pretty straightforward – I have a Knockout custom binding like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">ko.<span style="color: #660066;">bindingHandlers</span>.<span style="color: #660066;">markdown</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    update<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> valueAccessor<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> markdownValue <span style="color: #339933;">=</span> ko.<span style="color: #660066;">utils</span>.<span style="color: #660066;">unwrapObservable</span><span style="color: #009900;">&#40;</span>valueAccessor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> htmlValue <span style="color: #339933;">=</span> markdownValue <span style="color: #339933;">&amp;&amp;</span> <span style="color: #003366; font-weight: bold;">new</span> Showdown.<span style="color: #660066;">converter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">makeHtml</span><span style="color: #009900;">&#40;</span>markdownValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>htmlValue <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>… and then I can bind elements to show markdown data like as follows, and it will refresh the display whenever the markdown text changes:</p>

<div class="wp_syntax"><div class="code"><pre class="html">&lt;div data-bind=&quot;markdown: someMarkdownValue&quot;&gt;&lt;/div&gt;</pre></div></div>

<h4>9: jQuery and friends</h4>
<p><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 2px 0px 5px 10px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="right" src="/wp-content/uploads/2011/07/image-thumb3.png" width="244" height="77" /></p>
<p><em>Obviously</em> I used jQuery. I won’t bother discussing the core library because everyone knows about that, but I will mention the jQuery plugins I used:</p>
<ul>
<li><a href="http://jqueryui.com/">jQuery UI</a>, not for any of the visual elements, but merely for its “draggable” API. (You can drag to resize the panes, see?). I didn’t need any of its buttons, menus, etc.</li>
<li><a href="http://trentrichardson.com/Impromptu/">jQuery Impromptu</a>, to display the overlaid prompts (e.g., for choosing a tutorial or for explaining that your code will be overwritten when you click “Help, it’s not working!”). To be honest, this is the only component I regretted using. It’s is probably great if you use it for its intended use case (which includes things like multi-step wizards), but all I wanted was to move an existing DOM element in front of a gray background, lightbox style. jQuery Impromptu tries to do all manner of extra things that that screwed around with my DOM and its default animation was horribly stuttery on Chrome and Safari. In the end, I butchered its source code almost beyond recognition to get the desired behaviour with tolerable performance. It would have been much quicker for me to write my own version from scratch…</li>
<li><a href="http://api.jquery.com/category/plugins/templates/">jQuery Templates</a> (used internally by Knockout, and soon to be replaced with <a href="https://github.com/BorisMoore/jsrender">JsRender</a>)</li>
<li><a href="http://www.asual.com/jquery/address/">jQuery Address</a> (used internally by knockout.address for the hash-based navigation)</li>
</ul>
<h4>10: … and of course, Knockout.js</h4>
<p><img style="margin: 2px 0px 5px 10px; display: inline; float: right" title="icon" alt="icon" align="right" src="/wp-content/uploads/2011/07/icon-thumb.png" width="130" height="130" /></p>
<p>Not surprisingly, the UI is all orchestrated with knockout.js <strong><font size="1">(NuGet: knockoutjs)</font></strong>, so I have a very tidy MVVM architecture for the client-side code. There are objects to represent each of the major UI elements (panes, draggable splitters, code editors, prompts, etc.), and Knockout takes care of keeping the visible UI in sync with that whole object graph.</p>
<p>But I don’t need to explain this – you can learn much more from <a href="http://learn.knockoutjs.com">the tutorials</a>…</p>
<h3>Summary</h3>
<p>Building an application by smashing together a lot of different open-source projects is pretty quick. The end result is not always a perfectly harmonious symphony, as each component brings its own coding styles and conventions, but it works.</p>
<p>What I’ve learned about components is kind of predictable: the most useful ones are</p>
<ul>
<li><strong>Focused</strong>. Just solve one problem, please.</li>
<li><strong>Self-contained</strong>. I’m not going to accept a tall stack of dependencies; it’s too risky.</li>
<li><strong>Clearly documented</strong>. First I want examples, <em>then</em> a systematic list of API functions.</li>
<li>Available on <strong>NuGet</strong> <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="/wp-content/uploads/2011/07/wlemoticon-smile.png" /></li>
</ul>
<p>It wasn’t difficult to find components to suit my requirements. It’s great to see the open source ecosystems are thriving, both on .NET and JavaScript. </p>
<div class="feedflare">
<a href="http://feeds.codeville.net/~ff/SteveCodeville?a=tQ33cYyUhPk:cRqRdKBx1ZE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=tQ33cYyUhPk:cRqRdKBx1ZE:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=tQ33cYyUhPk:cRqRdKBx1ZE:G79ilh31hkQ"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=G79ilh31hkQ" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=tQ33cYyUhPk:cRqRdKBx1ZE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=tQ33cYyUhPk:cRqRdKBx1ZE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=tQ33cYyUhPk:cRqRdKBx1ZE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SteveCodeville/~4/tQ33cYyUhPk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.stevensanderson.com/2011/07/22/review-open-source-components-used-in-learnknockoutjs/feed/</wfw:commentRss>
		<slash:comments>38</slash:comments>
		<feedburner:origLink>http://blog.stevensanderson.com/2011/07/22/review-open-source-components-used-in-learnknockoutjs/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=review-open-source-components-used-in-learnknockoutjs</feedburner:origLink></item>
		<item>
		<title>Knockout.js 1.2.0 released</title>
		<link>http://feeds.codeville.net/~r/SteveCodeville/~3/CI089lTqdDw/</link>
		<comments>http://blog.stevensanderson.com/2011/04/20/knockoutjs-120-released/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 21:30:10 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.stevensanderson.com/2011/04/20/knockoutjs-120-released/</guid>
		<description><![CDATA[Earlier today I published the final 1.2.0 build of knockout.js. This is a big release with stacks of new goodness since 1.1.2: Changed the license to MIT (more permissive than the previous license, MS-PL) Added useful new bindings: event, attr, [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier today I published <a href="http://knockoutjs.com/">the final 1.2.0 build of knockout.js</a>. This is a big release with stacks of new goodness since 1.1.2:</p>
<ul>
<li>Changed the license to <a href="http://www.opensource.org/licenses/mit-license.php">MIT</a> (more permissive than the previous license, MS-PL)</li>
<li>Added useful new bindings: <a href="http://knockoutjs.com/documentation/event-binding.html">event</a>, <a href="http://knockoutjs.com/documentation/attr-binding.html">attr</a>, <a href="http://knockoutjs.com/documentation/html-binding.html">html</a></li>
<li>Enhanced some existing bindings: <a href="http://knockoutjs.com/documentation/options-binding.html">options</a>, <a href="http://knockoutjs.com/documentation/checked-binding.html">checked</a></li>
<li>Enhanced templating features (now supports jquery.tmpl 1.0.0pre as well as previous versions, <a href="http://knockoutjs.com/documentation/template-binding.html#note_5_dynamically_choosing_which_template_is_used">template polymorphism</a>, <a href="http://knockoutjs.com/documentation/template-binding.html#note_6_passing_additional_data_to_your_template_using_">passing additional data via options</a>)</li>
<li>Various performance improvements (e.g., now <a href="http://knockoutjs.com/documentation/template-binding.html#note_7_templates_are_precompiled_and_cached">precompiles and caches templates</a>, and ensures all data/subscriptions associated with DOM elements are cleaned immediately when those elements are removed) plus bugfixes</li>
<li>Better compilation style (ensures Closure Compiler no longer adds any extra variables to global namespace)</li>
<li>Various new extensibility points requested by plugin authors</li>
<li>Minor syntactical improvements (e.g., added zero-arg constructor for observable arrays)</li>
<li>All <a href="http://knockoutjs.com/documentation/introduction.html">documentation</a> updated to match 1.2.0</li>
</ul>
<p>Thanks very much to the many people who have contributed ideas, pull requests, bug reports, and support to make this release possible! I’m really enjoying <a href="http://groups.google.com/group/knockoutjs">the community</a> that’s growing around this.</p>
<h4>Hang on, what’s Knockout.js?</h4>
<p>In case you’ve never heard of it, here’s tekmaven’s explaination: <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="/wp-content/uploads/2011/04/wlemoticon-smile.png" /></p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2011/04/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="/wp-content/uploads/2011/04/image-thumb1.png" width="501" height="90" /></a></p>
<p>(By the way, for Visual Studio developers: <strong>Install-Package knockoutjs</strong>)</p>
<p>More specifically, Knockout is a JavaScript library for building rich, responsive user interfaces. It uses automatic dependency tracking and declarative bindings to update your UI whenever your data model changes, letting you cleanly structure your code around the MVVM pattern.</p>
<h4>No, that’s just marketing blather… show me something real</h4>
<p>OK then, to learn more:</p>
<ul>
<li>See the live examples on <a href="http://knockoutjs.com/">knockoutjs.com</a></li>
<li>See my <a href="http://blog.stevensanderson.com/2010/07/05/introducing-knockout-a-ui-library-for-javascript/">original introductory post</a> or the tutorial for <a href="http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/">using it with ASP.NET MVC</a> (note: it works with any other server-side technology too, as it’s just a JavaScript library)</li>
<li>Get advanced hints and techniques from <a href="http://www.knockmeout.net/">Ryan Niemeyer’s site, knockmeout.net</a> (special kudos to Ryan, a.k.a. rpn, for his truly amazing support efforts on the forum!)</li>
<li>Watch this fast-paced introduction presented at MIX 2011:
<div><a href="http://channel9.msdn.com/Events/MIX/MIX11/FRM08"><br />
                        <img src="/wp-content/uploads/2011/04/mixvideolarge.jpg" style="display: block; margin: 0.2em auto"/><br />
                    </a> </div>
</li>
</ul>
<div class="feedflare">
<a href="http://feeds.codeville.net/~ff/SteveCodeville?a=CI089lTqdDw:eik7ee_wCvk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=CI089lTqdDw:eik7ee_wCvk:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=CI089lTqdDw:eik7ee_wCvk:G79ilh31hkQ"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=G79ilh31hkQ" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=CI089lTqdDw:eik7ee_wCvk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=CI089lTqdDw:eik7ee_wCvk:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=CI089lTqdDw:eik7ee_wCvk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SteveCodeville/~4/CI089lTqdDw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.stevensanderson.com/2011/04/20/knockoutjs-120-released/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		<feedburner:origLink>http://blog.stevensanderson.com/2011/04/20/knockoutjs-120-released/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=knockoutjs-120-released</feedburner:origLink></item>
		<item>
		<title>MvcScaffolding: Scaffolding custom collections of files</title>
		<link>http://feeds.codeville.net/~r/SteveCodeville/~3/D9BcY7YcvIY/</link>
		<comments>http://blog.stevensanderson.com/2011/04/08/mvcscaffolding-scaffolding-custom-collections-of-files/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 10:45:52 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.stevensanderson.com/2011/04/08/mvcscaffolding-scaffolding-custom-collections-of-files/</guid>
		<description><![CDATA[This blog post is part of a series about the MvcScaffolding NuGet package: Introduction: Scaffold your ASP.NET MVC 3 project with the MvcScaffolding package Standard usage: Typical use cases and options One-to-Many Relationships Scaffolding Actions and Unit Tests Overriding the [...]]]></description>
			<content:encoded><![CDATA[<p>This blog post is part of a series about the MvcScaffolding NuGet package:</p>
<ol>
<li><a href="http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/">Introduction: Scaffold your ASP.NET MVC 3 project with the MvcScaffolding package</a> </li>
<li><a href="http://blog.stevensanderson.com/2011/01/13/mvcscaffolding-standard-usage/">Standard usage: Typical use cases and options</a> </li>
<li><a href="http://blog.stevensanderson.com/2011/01/28/mvcscaffolding-one-to-many-relationships/">One-to-Many Relationships</a> </li>
<li><a href="http://blog.stevensanderson.com/2011/03/28/scaffolding-actions-and-unit-tests-with-mvcscaffolding/">Scaffolding Actions and Unit Tests</a> </li>
<li><a href="http://blog.stevensanderson.com/2011/04/06/mvcscaffolding-overriding-the-t4-templates/">Overriding the T4 templates</a> </li>
<li><a href="http://blog.stevensanderson.com/2011/04/07/mvcscaffolding-creating-custom-scaffolders/">Creating custom scaffolders</a> </li>
<li><strong>This post: Scaffolding custom collections of files</strong></li>
</ol>
<p>Previously you’ve seen how to modify the output from existing scaffolders (by <a href="http://blog.stevensanderson.com/?p=321">overriding their T4 templates</a>) and how to <a href="http://blog.stevensanderson.com/?p=328">create custom scaffolders</a> that take arbitrary parameters and render new templates of their own.</p>
<p>But what if, instead of generating just a single file, you want to generate multiple files that all work together? Well, it’s not surprising that you can do so.</p>
<ul>
<li>You can put multiple <strong>Add-ProjectItemViaTemplate</strong> commands in your PowerShell file. You can generate as many different files, using as many different templates, as you want. </li>
<li>You can invoke one scaffolder from another. For example your custom scaffolder logic can contain “<strong>Scaffold Controller</strong>” or “<strong>Scaffold View</strong>” instructions to generate controllers and views using the built-in logic. </li>
</ul>
<p>In this final post in the current MvcScaffolding series, I&#8217;ll show how some of these ideas can be tied together to build something useful.</p>
<h4>Example: An Ajax-powered Controller</h4>
<p>I showed an example of doing this in my MvcConf talk (<a href="http://channel9.msdn.com/Series/mvcConf/mvcConf-2-Steve-Sanderson-MvcScaffolding" target="_blank">video here</a>) a couple of months ago, when I built a custom ControllerWithAjaxGrid scaffolder. This custom scaffolder produces a controller (obviously), but instead of having the usual index/edit/details/delete views, it just has a single view that contains an Ajax-powered grid from which the user can view, add, edit, and delete records. Videos are hard to reuse code from, so for your convenience I’ll reproduce the code in this blog post. </p>
<p>To see it in action, <a href="http://blog.stevensanderson.com/blogfiles/2011/April/CustomControllerScaffolder.zip">download the demo project</a>, and then scaffold an Ajax-grid controller for the included StockItem model by executing “<strong>Scaffold ControllerWithAjaxGrid StockItem</strong>” in the console:</p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2011/04/snaghtml2e143ef5.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="SNAGHTML2e143ef5" border="0" alt="SNAGHTML2e143ef5" src="/wp-content/uploads/2011/04/snaghtml2e143ef5-thumb.png" width="633" height="248" /></a></p>
<p>Run the controller by launching the application and navigating to /StockItems:</p>
<p><a href="http://blog.stevensanderson.com/wp-content/uploads/2011/04/image.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="/wp-content/uploads/2011/04/image-thumb.png" width="640" height="225" /></a></p>
<p>It uses jqGrid to do all the client-side stuff. By the way, if you’re wondering how to make jqGrid do different things like sorting or filtering, don’t ask me, I have no idea – <a href="http://www.trirand.com/blog/" target="_blank">talk to the experts</a>! Let’s keep this post focused on the scaffolding side of things.</p>
<h4>How ControllerWithAjaxGrid Works</h4>
<p>I won’t list all the code for ControllerWithAjaxGrid and its template – you can <a href="http://blog.stevensanderson.com/blogfiles/2011/April/CustomControllerScaffolder.zip">download the runnable demo project</a> for that. As an outline of how it works, ControllerWithAjaxGrid.ps1 contains the following instruction to create the controller itself:</p>

<div class="wp_syntax"><div class="code"><pre class="text">Scaffold MvcScaffolding.Controller $ModelType -Project $Project `
    -CodeLanguage $CodeLanguage -OverrideTemplateFolders $TemplateFolders `
    -NoChildItems -Force:$Force</pre></div></div>

<p>This inherits all the normal controller-scaffolding logic (e.g., knowing which folder to output the class file into) from MvcScaffolding.Controller, and merely overrides location where the T4 template is found (using –OverrideTemplateFolder). I was then able to put a ControllerWithContext.cs.t4 file in my custom scaffolder’s folder and hence override the code that gets generated. My custom controller template returns JsonResult data to support the Ajax grid. For example, here&#8217;s the template for its GetData action:</p>

<div class="wp_syntax"><div class="code"><pre class="text">public JsonResult GridData(int rows, int page)
{
    var count = _context.&lt;#= modelNamePlural #&gt;.Count();
    var pageData = _context.&lt;#= modelNamePlural #&gt;.OrderBy(x =&gt; x.&lt;#= Model.PrimaryKey #&gt;).Skip((page - 1) * rows).Take(rows);
    return Json(new {
        page = page,
        records = count,
        rows = pageData,
        total = Math.Ceiling((decimal)count / rows)
    }, JsonRequestBehavior.AllowGet);
}</pre></div></div>

<p>Similarly, my custom controller doesn’t need all the usual views – it only needs one that contains the JavaScript code to display the grid. I generate that view using a “Scaffold View” command in ControllerWithAjaxGrid.ps1, again using the –OverrideTemplateFolders parameter to make the View scaffolder load the T4 template from ControllerWithAjaxGrid’s folder.</p>
<p>One interesting thing about the view template is that it gets a list of all the properties on your model class:</p>

<div class="wp_syntax"><div class="code"><pre class="text">&lt;# var properties = viewDataType.VisibleMembers().OfType&lt;codeProperty&gt;(); #&gt;</pre></div></div>

<p>… then later uses this to generate some JavaScript code relating to each property:</p>

<div class="wp_syntax"><div class="code"><pre class="text">&lt;script type=&quot;text/javascript&quot;&gt;
    jQuery(&quot;#ajaxGrid&quot;).jqGrid({
        ...
        colNames: [&lt;#= string.Join(&quot;, &quot;, properties.Select(prop =&gt; &quot;'&quot; + prop.Name + &quot;'&quot;)) #&gt;],
        ...
    });
&lt;/script&gt;</pre></div></div>

<p>Finally, my custom controller needs to be sure that there is actually a data context class that it can use for data access. This is easy to arrange: I just put the following command in ControllerWithAjaxGrid.ps1:</p>

<div class="wp_syntax"><div class="code"><pre class="text">Scaffold DbContext $ModelType -DbContextType ((Get-Project $Project).Name + &quot;Context&quot;)</pre></div></div>

<p>You can see I’m using the convention that the data context class should be called <em>ProjectName</em>Context, but you could change that if you wanted. </p>
<p>For the full code including the T4 templates, <a href="http://blog.stevensanderson.com/blogfiles/2011/April/CustomControllerScaffolder.zip">download the demo project</a>.</p>
<h4>Conclusion</h4>
<p>You could use the same techniques to create other custom controller types with different combinations of views, data access classes, and so on. </p>
<p>If you’re unfamiliar with PowerShell then it might take a bit of learning, but once you’ve got a custom scaffolder to suit your project’s requirements, you can commit your <strong>/CodeTemplates</strong> folder into source control, and then everyone on your team can quickly generate controllers in the style you’ve defined, supplying whatever custom parameters you’ve declared.</p>
<p>For more information about creating custom controllers, see <a href="http://blog.stevensanderson.com/?p=328">the tutorial in my previous blog post</a> and the <a href="http://mvcscaffolding.codeplex.com/wikipage?title=T4Scaffolding%27s%20cmdlets%20for%20use%20in%20custom%20scaffolders">list of useful cmdlets for custom scaffolder scripts</a>.</p>
<div class="feedflare">
<a href="http://feeds.codeville.net/~ff/SteveCodeville?a=D9BcY7YcvIY:HB-f-1eiTns:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=D9BcY7YcvIY:HB-f-1eiTns:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=D9BcY7YcvIY:HB-f-1eiTns:G79ilh31hkQ"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=G79ilh31hkQ" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=D9BcY7YcvIY:HB-f-1eiTns:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?i=D9BcY7YcvIY:HB-f-1eiTns:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.codeville.net/~ff/SteveCodeville?a=D9BcY7YcvIY:HB-f-1eiTns:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SteveCodeville?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SteveCodeville/~4/D9BcY7YcvIY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.stevensanderson.com/2011/04/08/mvcscaffolding-scaffolding-custom-collections-of-files/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		<feedburner:origLink>http://blog.stevensanderson.com/2011/04/08/mvcscaffolding-scaffolding-custom-collections-of-files/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=mvcscaffolding-scaffolding-custom-collections-of-files</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.906 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-05-17 01:22:04 --><!-- Compression = gzip -->

