<?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.feedburner.com/~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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
<channel>
	<title>Los Techies</title>
    
	<link>http://lostechies.com</link>
	<description>LosTechies.com was originally discussed a few years ago, over a couple of adult beverages whose name sounds very similar to l(D)os t(E)quies. Anyway the thought was to create a public forum where technical ideas and thoughts can be shared in the same way we all get together around a good meal and drinks. Ideas and thoughts are cultivated in discussion, and brought to fruition through professional debate and laughter. Sounds good in theory, well read our thoughts and ideas, take part in our debates and rejoice in our laughter.</description>
	<lastBuildDate>Thu, 09 Feb 2012 15:30:47 +0000</lastBuildDate>
		<language>en</language>
        <image>
		<url>http://lostechies.com/favicon.ico</url>
		<title>LosTechies Master Site Feed Posts &amp; Pages</title>
		<link>http://lostechies.com</link>
	</image>
    
<generator>http://wordpress.org/?v=3.1.1</generator>
	<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/LosTechies" /><feedburner:info uri="lostechies" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Asynchronously Load HTML Templates For Backbone Views</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/BNfhhMjdorY/</link>
		<comments>http://lostechies.com/derickbailey/2012/02/09/asynchronously-load-html-templates-for-backbone-views/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 15:30:47 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Async]]></category>
		<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Backbone.Marionette]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Principles and Patterns]]></category>
		<category><![CDATA[User Experience]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/derickbailey/?p=805]]></guid>
		<description><![CDATA[As JavaScript applications become larger and larger, we have to think about the download size, memory usage and other performance constraints for our end users. There are a number of aspects to consider, one of which is how to deliver&#160;&#8230; <a href="http://lostechies.com/derickbailey/2012/02/09/asynchronously-load-html-templates-for-backbone-views/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>As JavaScript applications become larger and larger, we have to think about the download size, memory usage and other performance constraints for our end users. There are a number of aspects to consider, one of which is how to deliver the HTML templates that your application is using, to the user&#8217;s browser, so that your application can render the templates.</p>
<p>For my smaller applications, I tend to stuff a number of &lt;script&gt; blocks in to the HTML that the user downloads. This makes it easy for me to work with and I don&#8217;t have to worry about whether or not the template exists. But when the number of templates gets to be more than 5 or 6 small ones, this gets out of hand quickly. It makes it hard for me to manage them as I have to scroll through a lot of template code. Putting them in external files and then including them in the page with some server side technology helps the developer problem but it doesn&#8217;t solve the client problem of having to download all of these templates even if they are never used.</p>
<p>To deal with the issues surrounding templates in HTML files, we can split our templates in to separate files and then use asynchronous calls to our server to load them as needed.</p>
<h2>Backbone.View Render Semantics</h2>
<p>One of my goals, other than the asynchronous template loading, is to keep the general semantics of a Backbone View&#8217;s `render` method. It&#8217;s a short list, but it&#8217;s an important list as most of the Backbone community expects the render method to work this way.</p>
<p>Semantics are generally important as they give us information about how methods and objects are expected to be used, as well. This, in turn, informs the method signature and behavior. And all of this comes back to the <a href="http://code-magazine.com/article.aspx?quickid=1001061&amp;page=6">Liskov Substitution Principle (LSP)</a> from the <a href="http://code-magazine.com/Article.aspx?quickid=1001061">SOLID principles</a>, which tells us that we need to pay attention to semantics so that we can drop in replacements as needed.</p>
<p>The general semantics and method signature of a view&#8217;s render include:</p>
<ul>
<li><strong>No parameters</strong>: the render method shouldn&#8217;t require any parameters. You should be able to call `view.render()` and have it work fine</li>
<li><strong>Chainable</strong>: the render method should return `this` so that it can be chained with other method and attribute calls. This is most commonly done as `view.render().el` to grab the element that was rendered to</li>
<li><strong>Populate `el`, don&#8217;t replace it</strong>: the render method should populate a view&#8217;s `el` with any contents that the view needs to display. It generally shouldn&#8217;t replace the `el` as a whole</li>
</ul>
<p>Aside from these three items, there&#8217;s a lot of flexibility in how a view will typically be rendered. There&#8217;s also plenty of room for interpretation and divergence from this list. Many applications use a render method signature that takes parameters, or that replaces the `el` entirely. When changes like this are made, it&#8217;s a good idea to document them so that people will know why the changes are in place.</p>
<p>The benefit of keeping these semantics, though, is that you can swap out a synchronous, pre-loaded template rendering view with a view that uses an asynchronous template loading mechanism. Or, better yet, you can have an intelligent system that uses asynchronous calls to get the template the first time it needs it, and then uses caching to keep the template around and do synchronous rendering on subsequent requests for this view / template. If the semantics for the view are kept in place, it doesn&#8217;t matter how the view implements the rendering. The view can be dropped in or removed as needed, without having to change the surrounding code that uses it.</p>
<h2>Simple Async Template Retrieval</h2>
<p>We can keep this very simple, to begin with, using jQuery&#8217;s AJAX calls to load the template with a callback to do the actual rendering after the template is loaded.</p>
<div id="gist-1752642" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nx">template</span><span class="o">:</span> <span class="s1">&#39;my-view-template&#39;</span><span class="p">,</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s2">&quot;/templates/&quot;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">template</span> <span class="o">+</span> <span class="s2">&quot;.html&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">template</span><span class="p">){</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">html</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="nx">template</span><span class="p">).</span><span class="nx">tmpl</span><span class="p">();</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">that</span><span class="p">.</span><span class="nx">$el</span><span class="p">.</span><span class="nx">html</span><span class="p">(</span><span class="nx">html</span><span class="p">);</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="k">this</span><span class="p">;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1752642/5c0cd33f99709a06d1ab876885942e3aa2c755fc/1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1752642#file_1.js" style="float:right;margin-right:10px;color:#666">1.js</a>
            <a href="https://gist.github.com/1752642">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>In this example, we&#8217;re assuming that the view has a `template` attribute. This attribute represents the file that will be loaded from the server, and that file contain the actual template to be used.</p>
<p>We&#8217;re also using a convention of `/templates/{name}.html` for the template location on the server. This can be implemented easily in many different web server technologies. In Sinatra, for example, you can create a &#8220;public/templates&#8221; folder and put your HTML template files directly in that folder. They will be available without having to do anything more than start the Sinatra server.</p>
<p>When the call to `render` is made, the code makes an AJAX call back to the server to retrieve the specified template. A callback method is provided &#8211; and at this point, it assumes a successful call to get the template. When the template is returned from the server, the callback is fired, and the standard render code (using jQuery templates in this example) is executed.</p>
<p>Note that the `el` for the view is populated inside of the callback, but we are still calling `return this` at the end of the function. Even if we chain access to the `el` from the `render` method and immediately add the el&#8217;s contents to the DOM, this will still work:</p>
<blockquote>
<p>$(&#8220;#content&#8221;).html(view.render().el);</p>
</blockquote>
<p>The reason this works is that we are only populating the `el` with contents. We are not replacing it. When the AJAX call finally returns with the template, rendering it and populating the `el` will show the contents immediately because we&#8217;re setting the `html` method of an HTML element that is already attached to the DOM. It&#8217;s as if we had called `$(&#8220;#content&#8221;).html(&#8220;&lt;div&gt;some html&lt;/div&gt;&#8221;);` directly.</p>
<h2>Caching Templates</h2>
<p>Now that we have a template loading asynchronously, it would be nice to only load it once instead of every time it needs to be used. This will improve the overall performance of the application, from the user&#8217;s perspective.</p>
<p>To do this, we&#8217;ll need a little more than just some code in the render method. We want to re-use templates that we&#8217;ve already loaded, which means we can&#8217;t just store the template on the view instance. We need to store it in a place where any view instance can grab a copy of the template if it exists, or have an asynchronous call back to the server done to get the template when needed.</p>
<div id="gist-1752642" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">TemplateManager</span> <span class="o">=</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nx">templates</span><span class="o">:</span> <span class="p">{},</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nx">get</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">id</span><span class="p">,</span> <span class="nx">callback</span><span class="p">){</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">template</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">templates</span><span class="p">[</span><span class="nx">id</span><span class="p">];</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="nx">template</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">callback</span><span class="p">(</span><span class="nx">template</span><span class="p">);</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span> <span class="k">else</span> <span class="p">{</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s2">&quot;/templates/&quot;</span> <span class="o">+</span> <span class="nx">id</span> <span class="o">+</span> <span class="s2">&quot;.html&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">template</span><span class="p">)){</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">$tmpl</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="nx">template</span><span class="p">);</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">that</span><span class="p">.</span><span class="nx">templates</span><span class="p">[</span><span class="nx">id</span><span class="p">]</span> <span class="o">=</span> <span class="nx">$tmpl</span><span class="p">;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">callback</span><span class="p">(</span><span class="nx">$tmpl</span><span class="p">);</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC22'><br/></div><div class='line' id='LC23'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1752642/fa83841cfc2901dc092f45ab8c95dcbc701c0734/2.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1752642#file_2.js" style="float:right;margin-right:10px;color:#666">2.js</a>
            <a href="https://gist.github.com/1752642">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The template manager object has one primary method that we call: `get`. This method takes in a template name to be loaded and a callback method that is executed when the template is found. By using a callback method instead of returning a value directly, we can ensure both synchronous and asynchronous calls will work correctly.</p>
<p>When you call `get`, it will check a hash / object literal to see if the template you want is already loaded using the template name that you provide to make this check. If it exists, it executes the callback immediately and passes the template along. If it does not exist yet, an AJAX call is made with jQuery to get the template from the server. Once the template is loaded, the callback that you passed in will be executed and the template is passed to it.</p>
<p>We can now update our view to use the template manager:</p>
<div id="gist-1752642" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nx">template</span><span class="o">:</span> <span class="s1">&#39;my-view-template&#39;</span><span class="p">,</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">TemplateManager</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">template</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">template</span><span class="p">){</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">html</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="nx">template</span><span class="p">).</span><span class="nx">tmpl</span><span class="p">();</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">that</span><span class="p">.</span><span class="nx">$el</span><span class="p">.</span><span class="nx">html</span><span class="p">(</span><span class="nx">html</span><span class="p">);</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="k">this</span><span class="p">;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1752642/e7c6dd25bacf6d8ea48046aa83a1ad13f1ecbfc4/3.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1752642#file_3.js" style="float:right;margin-right:10px;color:#666">3.js</a>
            <a href="https://gist.github.com/1752642">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This isn&#8217;t much of a change from the view&#8217;s perspective. It&#8217;s better encapsulated, though, and a little easier to read. The real work is now being done in the TemplateManager object and we can change how it behaves as needed without having to update our views.</p>
<h2>Beyond Simple Caching</h2>
<p>There&#8217;s one remaining problem that this code has. If you have multiple instances of a view requesting the same template at roughly the same time, multiple AJAX calls will be made &#8211; one for each instance of the view. The net result is a slowdown in the application&#8217;s performance from too many network calls, and also a visual oddity where the views will appear one at a time as the AJAX calls finish. This can be a pretty big drag on performance and UI responsiveness.</p>
<p>Thanks to some previous digging in to jQuery&#8217;s deferred and some help from Steve Flitcroft (<a href="https://twitter.com/#!/red_square">@red_square</a>), I was able to solve this problem fairly easily. I put <a href="https://github.com/derickbailey/bbclonemail/blob/master/public/javascripts/bbclonemail.views.js#L28-52">the following code</a> in to my BBCloneMail app, which uses my <a href="https://github.com/derickbailey/backbone.marionette">Backbone.Marionette</a> plugin. It&#8217;s not directly implemented in Marionette&#8217;s `TemplateManager` but most of what you need is already there. There&#8217;s only a few additional lines of code that you need to make this work.</p>
<div id="gist-1752642" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="p">(</span><span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">promises</span> <span class="o">=</span> <span class="p">{};</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="c1">// Cache the returned deferred/promise by the id of the template</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="c1">// so that we can prevent multiple requests for the same template</span></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="c1">// from making the ajax call.</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="c1">//</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="c1">// This code is only safe to run synchronously. There exists a</span></div><div class='line' id='LC9'>&nbsp;&nbsp;<span class="c1">// race condition in this function, when run asynchronously,</span></div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="c1">// which would nullify the benefit under certain circumstances.</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">loadTemplateAsync</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">tmpId</span><span class="p">){</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">promise</span> <span class="o">=</span> <span class="nx">promises</span><span class="p">[</span><span class="nx">tmpId</span><span class="p">]</span> <span class="o">||</span> <span class="nx">$</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s2">&quot;/templates/&quot;</span> <span class="o">+</span> <span class="nx">tmpId</span> <span class="o">+</span> <span class="s2">&quot;.html&quot;</span><span class="p">);</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">promises</span><span class="p">[</span><span class="nx">tmpId</span><span class="p">]</span> <span class="o">=</span> <span class="nx">promise</span><span class="p">;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">promise</span><span class="p">;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC16'><br/></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="c1">// Use jQuery to asynchronously load the template. </span></div><div class='line' id='LC18'>&nbsp;&nbsp;<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Marionette</span><span class="p">.</span><span class="nx">TemplateManager</span><span class="p">.</span><span class="nx">loadTemplate</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">templateId</span><span class="p">,</span> <span class="nx">callback</span><span class="p">){</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">tmpId</span> <span class="o">=</span> <span class="nx">templateId</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="s2">&quot;#&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">);</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">promise</span> <span class="o">=</span> <span class="nx">loadTemplateAsync</span><span class="p">(</span><span class="nx">tmpId</span><span class="p">);</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">promise</span><span class="p">.</span><span class="nx">done</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">template</span><span class="p">){</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">callback</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">$</span><span class="p">(</span><span class="nx">template</span><span class="p">));</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC24'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC25'><span class="p">})();</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1752642/5d5796e3335c80182324ce00ca9263f371f6c265/5.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1752642#file_5.js" style="float:right;margin-right:10px;color:#666">5.js</a>
            <a href="https://gist.github.com/1752642">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The basic idea is that I&#8217;m using a jQuery deferred / promise to fire the callback method from the loadTemplate parameters. To prevent multiple requests for the same template heading back to the server, I&#8217;m caching the promises by the template id. When a call to loadTemplate is made through the template manager, I check to see if I have a promise for that template already. If I do, I register the loadTemplate&#8217;s callback parameter with the promise. If I don&#8217;t, I create the promise and then store it by the template&#8217;s Id. Either way, I register the callback with the promise which <a href="http://lostechies.com/derickbailey/2012/02/07/rewriting-my-guaranteed-callbacks-code-with-jquery-deferred/">guarantees it will be executed</a>. Once the template is returned from the server and the promise is fulfilled (resolved), all of the callbacks are fired off with the template data and everything renders correctly.</p>
<p>Note, though, that this code is not 100% safe. If you call the template manager from code that is already asynchronous, you can end up with a race condition where multiple promises are created and multiple calls to get the template are done. You&#8217;ll still get all of your views rendered just fine, but this will eliminate the benefit of using a promise to reduce network calls. Calling this code synchronously, though, won&#8217;t cause this race condition and the templates will only load once before they are cached and re-used.</p>
<p><span style="font-size: 18px; font-weight: bold;">See It In Action</span></p>
<p>If you&#8217;d like to see an example of this code in action (including the deferred/promise code from above), check out my <a href="http://bbclonemail.heroku.com/">BBCloneMail</a> application and <a href="https://github.com/derickbailey/bbclonemail">it&#8217;s source code</a>. You&#8217;ll see the update to the TemplateManager&#8217;s `loadTemplate` function in the code. When you view the live site, switch to the Contacts view and refresh the page. There will be a small delay in the template loading, and then all of the contacts (which all individually requested the same contact template) will all display at once. Subsequent requests to those areas of the app will be cached, of course, improving the user experience and performance even more.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=BNfhhMjdorY:UJLvyF_u2B4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=BNfhhMjdorY:UJLvyF_u2B4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=BNfhhMjdorY:UJLvyF_u2B4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=BNfhhMjdorY:UJLvyF_u2B4:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=BNfhhMjdorY:UJLvyF_u2B4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=BNfhhMjdorY:UJLvyF_u2B4:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/BNfhhMjdorY" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/derickbailey/2012/02/09/asynchronously-load-html-templates-for-backbone-views/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/e592bd1326b1e80188ed4c0bf26b9f75?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Derick Bailey</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/derickbailey/2012/02/09/asynchronously-load-html-templates-for-backbone-views/</feedburner:origLink></item>
	<item>
		<title>OSS Rules of Engagement</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/osx_gppA5m4/</link>
		<comments>http://lostechies.com/jimmybogard/2012/02/08/oss-rules-of-engagement/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 14:07:55 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[OSS]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/jimmybogard/2012/02/08/oss-rules-of-engagement/]]></guid>
		<description><![CDATA[After two posts to a reaction of a comment on feedback I had for the Git installer, I thought it was pertinent to share my thoughts on OSS rules of engagement. Scott Chacon left a comment along the lines of&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/02/08/oss-rules-of-engagement/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>After <a href="http://ayende.com/blog/154305/send-me-a-patch-for-that">two</a> <a href="http://wekeroad.com/2012/02/07/kiss-the-ring/">posts</a> to a <a href="http://devlicio.us/blogs/hadi_hariri/archive/2012/02/06/submit-a-patch.aspx">reaction</a> of a <a href="http://lostechies.com/jimmybogard/2012/02/01/improving-the-git-windows-experience-downloads/#comment-426770262">comment</a> on <a href="http://lostechies.com/jimmybogard/2012/02/01/improving-the-git-windows-experience-downloads/">feedback I had for the Git installer</a>, I thought it was pertinent to share my thoughts on OSS rules of engagement. <a href="http://scottchacon.com/">Scott Chacon</a> left a comment along the lines of “your time spent complaining is better directed at submitting a pull request”. Which, as an OSS developer and project maintainer, I agree up to a point, until that reaction discourages feedback.</p>
<p>I understand how daunting it can be to contribute to an unfamiliar project, especially if you recognize that the people most familiar with the codebase would take a lot less time to do so, and probably do it “right” in the first place. I also understand how frustrating it can be to have large, visible OSS project where the number of people complaining/giving feedback far dwarfs the number of people actually contributing.</p>
<p>So what are the rules of OSS engagement for me?</p>
<h3>1. Un-actionable complaints will largely be ignored</h3>
<p>Mostly because I don’t have too much time to figure out what you want. I generally will respond with prodding, but unless you respond back, I won’t have anything actionable to address. Having an example of the alternative is generally the minimal bar here.</p>
<h3>2. Feedback is encouraged. Example client code even better</h3>
<p>As an OSS developer maintaining libraries, I often get feedback of “Can it do XYZ?”. Sometimes the answer is No, but I would like to see what your vision of how it should behave.</p>
<p>What should the methods be called? What does the configuration look like? What’s an example of the behavior you’re envisioning?</p>
<p>I don’t even care if the code compiles – just show me an example of what you’re thinking.</p>
<h3>3. Unit tests for bugs encouraged, but not required</h3>
<p>I just really need code to reproduce the bug. A report of a bug with zero code is much harder to reproduce, and much less likely to get fixed.</p>
<p>If you want to up your odds of getting a bug fixed, the more code I get demonstrating the problem, the better.</p>
<h3>4. The more actionable the feedback, the more likely it gets integrated.</h3>
<p>Pull requests are at the top end of the “actionable feedback” scale. An email reporting a bug with no code as at the low end. The more actionable the feedback I receive, the more likely I will have the time/energy to respond to the feedback.</p>
<p>However, I won’t turn people away for <em>not</em> giving me a pull request. I encourage it, and often point folks to try, especially if they want to “up the odds”.</p>
<p><strong>I might encourage you to send me a pull request, but I’ll never disparage you for <em>not</em> sending me one.</strong></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=osx_gppA5m4:JcwjXxaR5rE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=osx_gppA5m4:JcwjXxaR5rE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=osx_gppA5m4:JcwjXxaR5rE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=osx_gppA5m4:JcwjXxaR5rE:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=osx_gppA5m4:JcwjXxaR5rE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=osx_gppA5m4:JcwjXxaR5rE:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/osx_gppA5m4" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/jimmybogard/2012/02/08/oss-rules-of-engagement/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/cc359c5ccf90d7a24b5976316797b5ec?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Jimmy Bogard</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/jimmybogard/2012/02/08/oss-rules-of-engagement/</feedburner:origLink></item>
	<item>
		<title>Rewriting My ‘Guaranteed Callbacks’ Code With jQuery Deferred</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/enxgwOKoqew/</link>
		<comments>http://lostechies.com/derickbailey/2012/02/07/rewriting-my-guaranteed-callbacks-code-with-jquery-deferred/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 16:11:33 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Async]]></category>
		<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Backbone.Marionette]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JQuery]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/derickbailey/?p=809]]></guid>
		<description><![CDATA[In a previous post, I showed some code that I wrote to guarantee the execution of a callback method after a Backbone collection was loaded. Even if you added the callback after the collection had been loaded, it would run. Shortly&#160;&#8230; <a href="http://lostechies.com/derickbailey/2012/02/07/rewriting-my-guaranteed-callbacks-code-with-jquery-deferred/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>In a previous post, I showed some code that I wrote to <a href="http://lostechies.com/derickbailey/2012/02/03/get-a-model-from-a-backbone-collection-without-knowing-if-the-collection-is-loaded/">guarantee the execution of a callback method</a> after a Backbone collection was loaded. Even if you added the callback after the collection had been loaded, it would run. Shortly after writing that blog post, I extracted the code in to my <a href="https://github.com/derickbailey/backbone.marionette">Backbone.Marionette</a> framework as an object called &#8220;Callbacks&#8221;. It worked and it got the job done, but I knew it could be done better.</p>
<p>Then a few of my friends started talking about <a href="http://api.jquery.com/category/deferred-object/">jQuery&#8217;s &#8220;deferred&#8221; objects and &#8220;promises&#8221;</a>. I had heard of this previously but had not bothered to learn it. When I finally decided to dig in to it, this morning, I found that it was a much more robust implementation of what I had just written. I found <a href="http://www.erichynds.com/jquery/using-deferreds-in-jquery/">a good article that helped me understand it better</a>, and began working on an update for Marionette to use deferred objects instead of my own code. Addy Osmani also wrote <a href="http://msdn.microsoft.com/en-us/scriptjunkie/gg723713">a really good article on MSDN</a>, which sheds even more light on how these work and how to work with them.</p>
<h2>My New &#8220;Callbacks&#8221; Object</h2>
<p>Here&#8217;s the end result of my digging in to this and replacing my Callbacks object with a jQuery deferred / promise implementation:</p>
<div id="gist-1760312" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">// Callbacks</span></div><div class='line' id='LC2'><span class="c1">// ---------</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="c1">// A simple way of managing a collection of callbacks</span></div><div class='line' id='LC5'><span class="c1">// and executing them at a later point in time, using jQuery&#39;s</span></div><div class='line' id='LC6'><span class="c1">// `Deferred` object.</span></div><div class='line' id='LC7'><span class="nx">Marionette</span><span class="p">.</span><span class="nx">Callbacks</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">deferred</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">Deferred</span><span class="p">();</span></div><div class='line' id='LC9'>&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">promise</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">deferred</span><span class="p">.</span><span class="nx">promise</span><span class="p">();</span></div><div class='line' id='LC10'><span class="p">};</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'><span class="nx">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span><span class="nx">Marionette</span><span class="p">.</span><span class="nx">Callbacks</span><span class="p">.</span><span class="nx">prototype</span><span class="p">,</span> <span class="p">{</span></div><div class='line' id='LC13'>&nbsp;&nbsp;</div><div class='line' id='LC14'>&nbsp;&nbsp;<span class="c1">// Add a callback to be executed. Callbacks added here are</span></div><div class='line' id='LC15'>&nbsp;&nbsp;<span class="c1">// guaranteed to execute, even if they are added after the </span></div><div class='line' id='LC16'>&nbsp;&nbsp;<span class="c1">// `run` method is called.</span></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="nx">add</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">){</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">promise</span><span class="p">.</span><span class="nx">done</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">options</span><span class="p">){</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">callback</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC21'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC22'><br/></div><div class='line' id='LC23'>&nbsp;&nbsp;<span class="c1">// Run all registered callbacks with the context specified. </span></div><div class='line' id='LC24'>&nbsp;&nbsp;<span class="c1">// Additional callbacks can be added after this has been run </span></div><div class='line' id='LC25'>&nbsp;&nbsp;<span class="c1">// and they will still be executed.</span></div><div class='line' id='LC26'>&nbsp;&nbsp;<span class="nx">run</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">options</span><span class="p">){</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">deferred</span><span class="p">.</span><span class="nx">resolve</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span></div><div class='line' id='LC28'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC29'><span class="p">});</span></div><div class='line' id='LC30'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1760312/d3a94a8d1f93f70a76dd8f05fc9768c9618ec8a7/1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1760312#file_1.js" style="float:right;margin-right:10px;color:#666">1.js</a>
            <a href="https://gist.github.com/1760312">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This is a reduction in code and complexity, by about 75% on both counts.</p>
<h2>Things To Note</h2>
<p>Backbone.Marionette.Callbacks is a constructor function and you are expected to instantiate it before using it:</p>
<blockquote>
<p>var callbacks = new Backbone.Marionette.Callbacks();</p>
<p>callbacks.add(function(){ … });</p>
</blockquote>
<p>I&#8217;m using a single deferred object and promise object in this code. That&#8217;s done on purpose. I am collection an arbitrary list of callbacks, at an unknown time, and want to guarantee execution of those callbacks. By limiting this code to a single deferred object and a single promise, I can do exactly that.</p>
<p>When you call `add`, it sets up a `done` callback using the single promise object. No matter how many times you call `add`, it uses the same promise for that instance of Callbacks.</p>
<p>Then when you finally call `run`, it resolves the deferred object. Calling `resolve` on the deferred object will kick off all of the callback methods that I had set up with the when/then code.</p>
<p>After you call `run` and resolve the deferred object, you can still add callback functions to this Callbacks instance. When you do add more, jQuery is smart enough to know that the promise has already been fulfilled and it immediately executes the callback that you registered.</p>
<h2>Specs And Usage</h2>
<p>If you&#8217;d like to see the specs for this to show how it works with pre-run registered and post-run registered callback methods, you can grab the source code for <a href="https://github.com/derickbailey/backbone.marionette">Backbone.Marionette</a> and look at <a href="https://github.com/derickbailey/backbone.marionette/blob/master/spec/javascripts/callbacks.spec.js">the Jasmine specs for Callbacks</a>.</p>
<p>I&#8217;m using this Callbacks object in <a href="https://github.com/derickbailey/backbone.marionette/blob/master/backbone.marionette.js#L414-423">Marionette.Application&#8217;s &#8220;addInitializer&#8221; function</a>, which I&#8217;m using to guarantee initializer callback execution. Once I get back to working on the client project that I wrote that original code for, I&#8217;ll also update it to use the new Callbacks implementation instead of my original implementation, too.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=enxgwOKoqew:M9meUCLl0L4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=enxgwOKoqew:M9meUCLl0L4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=enxgwOKoqew:M9meUCLl0L4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=enxgwOKoqew:M9meUCLl0L4:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=enxgwOKoqew:M9meUCLl0L4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=enxgwOKoqew:M9meUCLl0L4:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/enxgwOKoqew" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/derickbailey/2012/02/07/rewriting-my-guaranteed-callbacks-code-with-jquery-deferred/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/e592bd1326b1e80188ed4c0bf26b9f75?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Derick Bailey</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/derickbailey/2012/02/07/rewriting-my-guaranteed-callbacks-code-with-jquery-deferred/</feedburner:origLink></item>
	<item>
		<title>Constraints, expectations and real estate</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/VtZSXugZShQ/</link>
		<comments>http://lostechies.com/jimmybogard/2012/02/06/constraints-expectations-and-real-estate/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 21:30:22 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[Agile]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/jimmybogard/2012/02/06/constraints-expectations-and-real-estate/]]></guid>
		<description><![CDATA[One of my favorite shows on TV these days is (don’t laugh) the show Property Virgins on HGTV. In it, an experienced Realtor walks first-time home buyers through the house selection and offer process. A lot of times the “let’s&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/02/06/constraints-expectations-and-real-estate/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>One of my favorite shows on TV these days is (don’t laugh) the show <a href="http://www.hgtv.com/property-virgins/show/index.html">Property Virgins</a> on HGTV. In it, an experienced Realtor walks first-time home buyers through the house selection and offer process.</p>
<p>A lot of times the “let’s watch a couple pick a house” type shows highlight the inexperience of the buyers. <strong>Buyers tend to focus on the wrong thing</strong>, like paint colors or light fixtures, and gloss over things that are hard to change, like room layout. But the best part of the show happens right at the beginning, in a brilliant move to reset the usually unrealistic buyer expectations.</p>
<p>At the beginning of the show, the host asks the buyers a few key questions to identify both their budgetary constraints as well as their aspects of a home they most value. It could be location, layout, lot size, etc. But the buyers always have some sort of dollar amount they won’t go over.</p>
<p>The next step the Realtor does is rather brilliant – they take the first-time home buyers to a house that meets all of their aspects for an ideal home. It has the perfect layout, the perfect location, all the amenities they want, in the right condition, at the right size and so on. The buyers inevitably love this home, at which point the Realtor asks the buyers to guess how much the home costs. Also inevitably, the buyers are pretty far off the actual price, and inevitably their ideal home is typically at least double, if not triple or quadruple the buyers’ budget.</p>
<p>And inevitably, <strong>the buyers have sticker shock</strong>!</p>
<h3>Talking buyers off the ledge</h3>
<p>This fantastic approach accomplishes two important goals:</p>
<ul>
<li>Convey what perfection costs</li>
<li>Force a prioritization</li>
</ul>
<p>The host is never mean about showing the expensive house, but instead presents it as something to aspire towards. This also resets in the buyer’s minds that every house they see from there on out is going to have <strong>some subset of their valued aspects</strong>.</p>
<p>But instead of discouraging the first time home buyers, this approach tends to force them to focus on what is most important to them in a house.</p>
<h3>Resetting expectations</h3>
<p>We often have clients that are first-time software buyers, or first-time-with-someone-who-knows-what-they-are-actually-doing software buyers. A lot of what we do in the initial part of the project is framing the project for success. We look at everything the client wants, talk about scope and budget, then reset expectations back down to an attainable level.</p>
<p>What we don’t want to see happen are those buyers that look at dozens or even hundreds of homes looking for that house that checks every single checkbox and comes in at their budget. That house might exist, or it might not, but a lot of time can be wasted searching and searching.</p>
<p><strong>Constraints force prioritization and hard decisions</strong>. Having an experienced guide (like that Realtor host) ensures that the buyer understands what’s feasible for their budget, as well as the guiding hand on helping to share experience on what things matter (the electric wiring needs replacing) or do not (the bedroom has shag carpet).</p>
<p><strong>Delivering value is really only half the equation</strong>. It’s up to us as developers to make sure the buyer understands what they are getting for their money (or time), relative to what’s out there in the market. If you bought a house in isolation, it’s hard to know whether you’re getting taken to the cleaners. But by having frames of reference (Product XYZ is similar to yours, and took ABC man hours/dollars to build), we can center the conversation around “what’s most valuable to me” instead of “how can we squeeze everything in”.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=VtZSXugZShQ:pnzjtZmkRL4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=VtZSXugZShQ:pnzjtZmkRL4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=VtZSXugZShQ:pnzjtZmkRL4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=VtZSXugZShQ:pnzjtZmkRL4:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=VtZSXugZShQ:pnzjtZmkRL4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=VtZSXugZShQ:pnzjtZmkRL4:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/VtZSXugZShQ" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/jimmybogard/2012/02/06/constraints-expectations-and-real-estate/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/cc359c5ccf90d7a24b5976316797b5ec?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Jimmy Bogard</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/jimmybogard/2012/02/06/constraints-expectations-and-real-estate/</feedburner:origLink></item>
	<item>
		<title>Powerfully simple persistence: MongoDB</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/wWEvVribfMM/</link>
		<comments>http://lostechies.com/joshuaflanagan/2012/02/06/easy-persistence-mongodb/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 16:15:00 +0000</pubDate>
		<dc:creator>Joshua Flanagan</dc:creator>
				<category><![CDATA[mongodb]]></category>
		<category><![CDATA[ruby]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/joshuaflanagan/2012/02/06/easy-persistence-mongodb/]]></guid>
		<description><![CDATA[In my post &#8220;Great time to be a developer&#8220;, I listed MongoDB as one of the tools that made my task (track travel times for a given route) easy. This post will show you how. What do I need to&#160;&#8230; <a href="http://lostechies.com/joshuaflanagan/2012/02/06/easy-persistence-mongodb/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>In my post &#8220;<a href="http://lostechies.com/joshuaflanagan/2012/01/31/great-time-to-be-a-developer/" target="_blank">Great time to be a developer</a>&#8220;, I listed <a href="http://www.mongodb.org/" target="_blank">MongoDB</a> as one of the tools that made my task (track travel times for a given route) easy. This post will show you how.</p>
<h2>What do I need to store?</h2>
<p>My travel time data collection job needs the URL for the traffic data endpoint for each route that I&#8217;ll be tracking. I could have just hardcoded the URL in the script, but I knew that my co-workers would be interested in tracking their routes too, so it made sense to store the list of routes in the database.</p>
<p>I need to store the list of &#8216;trips&#8217;. I define a trip as the reported travel details for a given route and departure time (Josh&#8217;s route at 9am, Josh&#8217;s route at 9:10am, Tim&#8217;s route at 9:10am, etc.). I want to capture the date of each trip so that I can chart the trips for a given day, and compare day to day variation. Even though I really only need to the total travel time for each trip, I want to capture the entire response&nbsp; from the traffic service (travel times, directions, traffic delay, etc.) so that I could add new visualizations in the future.</p>
<h2>Setup</h2>
<p>First, I had to install mongo on my laptop. I used the <a href="http://www.mongodb.org/downloads" target="_blank">homebrew</a> package manager, but <a href="http://www.mongodb.org/downloads" target="_blank">binary releases are readily available</a>.</p>
<pre>brew install mongodb
</pre>
<p>I need to add the route for my commute. I fire up the mongo console by typing <font size="4" face="Cordia New">mongo</font>. I&#8217;m automatically connected to the default &#8216;test&#8217; database in my local mongodb server. I add my route:</p>
<pre class="brush:js; gutter:false; tab-size:2;toolbar: false">&gt; db.routes.save({
  name: 'josh',
  url: 'http://theurlformyroute...'
})</pre>
<p>I verify the route was saved:</p>
<pre class="brush:js; gutter:false; wrap-lines:true; tab-size:2;toolbar: false">&gt; db.routes.find()
{"_id" : ObjectId("4f22434d47dd721cf842bdf6"),
 "name" : "josh",
 "url" : "http://theurlformyroute..." }
</pre>
<p>It is worth noting that I haven&#8217;t skipped any steps. I fired up the mongo console, ran the save command, and now I have the route in my database. I didn&#8217;t need to create a database, since the &#8216;test&#8217; database works for my needs. I didn&#8217;t need to define the routes collection &#8211; it was created as soon as I stored something in it. I didn&#8217;t need to define a schema for the data I&#8217;m storing, because there is no schema. I am now ready to run my data collection script.</p>
<h2>Save some data</h2>
<p>I&#8217;ll use the ruby MongoDB driver (gem install mongo) directly (you can also use something like mongoid or mongomapper for a higher-level abstraction). My update script needs to work with the URL for each route:</p>
<pre class="brush:ruby; gutter:false; wrap-lines:false; tab-size:2;toolbar: false">db = Mongo::Connection.new.db("test")
db["routes"].find({}, :fields =&gt; {"url" =&gt; 1}).each do |route|
  url = route["url"]
  # collect trip data for this route's url
end
</pre>
<p>I want to group related trips for a commute, so I create a &#8216;date_key&#8217; based on the current date/time. A date_key looks like: 2012-01-25_AM, 2012-01-25_PM, or 2012-01-26_AM. Now to store the details returned from the traffic service:</p>
<pre class="brush:ruby; gutter:false; wrap-lines:false; tab-size:2;toolbar: false">trip_details = TrafficSource.get(url)
db["routes"].update({"_id" =&gt; route["_id"]}, {
  "$addToSet" =&gt; {"trip_keys" =&gt; date_key},
  "$push" =&gt; {"trips.#{date_key}" =&gt; trip_details}
})
</pre>
<p>After running for a couple days, this will result in a route document that looks something like:</p>
<pre class="brush:js; gutter:false; wrap-lines:false; tab-size:2;toolbar: false">{
  _id: 1234,
  name: 'josh',
  url: 'http://mytravelurl...',
  trip_keys: ['2012-01-25_AM', '2012-01-25_PM', '2012-01-26_AM',...],
  trips: {
    2012-01-25_AM: [{departure: '9:00', travelTime: 24, ...}, {departure: '9:10', travelTime: 26}, ...],
    2012-01-25_PM: [{departure: '9:00', travelTime: 28, ...}, {departure: '9:10', travelTime: 29}, ...],
    2012-01-26_AM: [{departure: '9:00', travelTime: 25, ...}, {departure: '9:10', travelTime: 25}, ...],
    ...
  }
}
</pre>
<p>That is *all* of the MongoDB-related code in the data collection script. I haven&#8217;t left out any steps &#8211; programmatic, or administratrive. None of the structue was defined ahead of time. I just &#8216;$push&#8217;ed some trip details into &#8216;trips.2012-01-25_AM&#8217; on the route. It automatically added an object to the &#8216;trips&#8217; field, with a &#8217;2012-01-25_AM&#8217; field, which holds an array of trip details. I also store a list of unique keys in the trip_keys field using $addToSet in the same `update` statement.</p>
<h2>Show the data</h2>
<p>The web page that charts the travel times makes a single call to MongoDB:</p>
<pre class="brush:ruby; gutter:false; wrap-lines:false; tab-size:2;toolbar: false;">route = db["routes"].find_one(
  {:name =&gt; 'josh'},
  :fields =&gt; {"trips" =&gt; 1}
)</pre>
<p>The entire trips field, containing all of the trips grouped by date_key, is now available in the ruby hash <font size="4" face="Courier New">route</font>. With a little help from ruby&#8217;s <a href="http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-map" target="_blank">Enumerable#map</a>, I transform the data into a format consumable by <a href="http://www.highcharts.com/" target="_blank">Highcharts JS</a>.</p>
<h2>Production</h2>
<p>Just to be thorough, I&#8217;ll mention that I had to modify the script for production use. I replaced the `db` local variable with a method that uses the <a href="http://addons.heroku.com/mongolab" target="_blank">mongolab</a> connection when available, or falls back to the local test connection:</p>
<pre class="brush:ruby; gutter:false; wrap-lines:false; tab-size:2;toolbar: false">def db
  @db ||=
  begin
    mongolab_uri = ENV['MONGOLAB_URI']
    return Mongo::Connection.new.db("test") unless mongolab_uri
    uri = URI.parse(mongolab_uri)
    Mongo::Connection.from_uri(mongolab_uri).db(uri.path.gsub(/^\//, ''))
  end
end
</pre>
<h2>Conclusion</h2>
<p>A couple queries, a single, powerful update statement, and no administration or schema preparation. Paired with the <a href="http://api.mongodb.org/ruby/current/file.TUTORIAL.html" target="_blank">ruby driver</a>&#8216;s seemless mapping to native Hash objects, it is hard to imagine a simpler, equally powerful, persistence strategy for this type of project.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=wWEvVribfMM:bgHxTA8PtXI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=wWEvVribfMM:bgHxTA8PtXI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=wWEvVribfMM:bgHxTA8PtXI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=wWEvVribfMM:bgHxTA8PtXI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=wWEvVribfMM:bgHxTA8PtXI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=wWEvVribfMM:bgHxTA8PtXI:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/wWEvVribfMM" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/joshuaflanagan/2012/02/06/easy-persistence-mongodb/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/10adb7caa4eff9c02d244172bc8db6df?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=PG" medium="image">
            <media:title type="html">Joshua Flanagan</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/joshuaflanagan/2012/02/06/easy-persistence-mongodb/</feedburner:origLink></item>
	<item>
		<title>3 Stages Of A Backbone Application’s Startup</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/d8eSj1rOt_8/</link>
		<comments>http://lostechies.com/derickbailey/2012/02/06/3-stages-of-a-backbone-applications-startup/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 13:30:12 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Backbone.Marionette]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Semantics]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/derickbailey/?p=803]]></guid>
		<description><![CDATA[Most Backbone application examples that you find on the web are very small and very simple. They tend to use a very simple idea of an application &#8220;init&#8221; method to start up a router and have the router kick off&#160;&#8230; <a href="http://lostechies.com/derickbailey/2012/02/06/3-stages-of-a-backbone-applications-startup/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>Most Backbone application examples that you find on the web are very small and very simple. They tend to use a very simple idea of an application &#8220;init&#8221; method to start up a router and have the router kick off the real application code. I find this path to be very constraining, though. For the most basic of applications it works perfectly fine, but when you get in to anything of any real size and complexity, it becomes difficult to work with.</p>
<p>My Backbone.Marionette add-on helps to alleviate this problem by introducing the idea of application initializer callbacks. I&#8217;ve talked about these before, and I use them quite extensively in my applications. It really helps to keep my code clean and separated. And I recently realized that there&#8217;s an unintended benefit to using initializers like this: explicitly identifying the different stages of starting an application.</p>
<h2>The 3 Stages</h2>
<p>The three stages that I have identified in my own applications are (in order):</p>
<ol>
<li>The Application&#8217;s Foundation</li>
<li>The Application&#8217;s Initialization</li>
<li>A Contextual Start (optional)</li>
</ol>
<p>Honestly, I could break this down in to a significantly larger number of stages if I really wanted to get fine-grained and detailed. More specifically, it&#8217;s the 2nd stage that can be broken down in to pre-initialization, initialization, and post-initialization. But I think as a general place to start, identifying 3 primary stages for an application&#8217;s startup is a good idea. It gives us a better understanding of what code should go where, helps us keep our code clean, improves re-usability and flexibility, and will likely help us scale our apps as well.</p>
<p><span style="font-size: 18px; font-weight: bold;">Stage 1: The Application&#8217;s Foundation</span></p>
<p>Consider this to be the stage where the browser is simply grabbing all of the specified resources from the server and running through the first pass of parsing the HTML, laying out what the static HTML has provided, styling it with the basic CSS definitions, displaying any images and text from the static HTML, and parsing the JavaScript files so it can get ready to run them. I&#8217;ve never seen an HTML application that didn&#8217;t have this stage in it, because without it, your browser doesn&#8217;t have anything to do or show.</p>
<p>This is the one step the everyone does, and does well.</p>
<h2>Stage 2: The Application&#8217;s Initialization</h2>
<p>Every application has an initialization stage. Some are more elaborate than others. At times you can get away with the over-simplified &#8220;app&#8221; object with an &#8220;init&#8221; method on it, like many of the sample apps and boilerplate codes will suggest:</p>
<div id="gist-1731648" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">App</span> <span class="o">=</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nx">init</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// app initialization and startup goes here</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC5'><span class="p">}</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'><span class="nx">App</span><span class="p">.</span><span class="nx">init</span><span class="p">();</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1731648/5d7d6d280ce51e71ccf1f103954fad1a99e5b4b2/1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1731648#file_1.js" style="float:right;margin-right:10px;color:#666">1.js</a>
            <a href="https://gist.github.com/1731648">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>But when you get in to an application of any significant size and complexity, the single &#8220;init&#8221; method becomes a junk drawer where you tend to throw code because you haven&#8217;t yet identified where it should belong. Using a series of small initializer functions for each area of your application will help to alleviate this problem, as I&#8217;ve mentioned before. But the real question is:</p>
<p>What goes in the initializers?</p>
<p>Initializers are the bits of code, functionality, data and display that are absolutely required for your app to do it&#8217;s job, no matter what part of the application the user is trying to load up and use. They are the bits that must be initialized before the user can do anything meaningful with your application.</p>
<p>If your app has a menu structure generated by Backbone code, and it must always be present in the application, this should be in an initializer. If you&#8217;re building a multi-room chat application and you need to list the rooms that the user has favorited in a small block on the screen that is always visible to the user, this should be an initializer. If you&#8217;re building an image gallery and you need to load a thumb-nail list of images to show, no matter which image the user is trying to view, this is an initializer.</p>
<p>Other parts of your application code that the user doesn&#8217;t directly see may also be contenders for initializers, too. For example, if you have a router that needs to be up and running, the router probably needs to be instantiated inside of an initializer. But &#8211; and this is an important but &#8211; the routes on that router should not be executed during initialization. Instantiate the router and wait until after initialization has completed to call the &#8220;Backbone.history.start()&#8221; method, kicking off your route handlers.</p>
<h2>Stage 3: A Contextual Start (optional)</h2>
<p>I call this stage optional because not every application has a contextual kick off. Sometimes an application only needs initializers. The classic &#8220;todo&#8221; application is a great example of this. There is no contextual start up for this app. When the application is loaded, it initializes itself with the list of to do items and then it waits for the user to interact with the list.</p>
<p>When a Backbone application uses a router to respond to url pushState or hash fragment changes, though, it does have a contextual starting point. Each route that a user is allowed to bookmark or copy as a direct link will provide the context that our application needs to use, to get the user back to where they want to be. Even if a Backbone app has a router and contextual start, though, it may not be used. If there user hits the root of the application, there may not be any additional code to run for the empty (&#8220;&#8221;) route. When the user hits a route, though, that route must server up the context and application state that the user expects to see.</p>
<p>The problem that I see in most routed Backbone applications, though, is that they bundle the application initialization with the contextual start. That is, people tend to use the router and it&#8217;s callback methods as the sole place to get the application initialized <em>and</em> get the user back to the context of the route that they requested. For trivial applications, this may be fine. The initialization code may be so small that it doesn&#8217;t really matter if it&#8217;s crammed in to the router. But for any real application with any amount of complexity, this is a bad idea. It couples two very distinct parts of the application startup very tightly, and it can lead to bloated and unmaintainable routers with limited entry points in to the application.</p>
<p>So… what goes in the contextual start, other than just saying route callbacks?</p>
<p>Your route callbacks should be as simple as possible. They shouldn&#8217;t initialize your system as a whole. Rather, they should be used to determine the state of the application that the user wishes to see. Therefore, the code that goes in to a route callback should be the smallest amount of code that you can write, to get your application from it&#8217;s initial state (the state that it was in when the initializers completed) to the desired state.</p>
<p>In an image gallery application, a user may hit a bookmark that points to a specific image. For example, &#8220;#images/4&#8243;. The route callback that executes should load the requested image and display it on the screen:</p>
<div id="gist-1731648" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Router</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nx">routes</span><span class="o">:</span> <span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s2">&quot;image/:id&quot;</span><span class="o">:</span> <span class="s2">&quot;imageById&quot;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC5'>&nbsp;</div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="nx">imageById</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">id</span><span class="p">){</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">image</span> <span class="o">=</span> <span class="nx">imageCollection</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">id</span><span class="p">);</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">App</span><span class="p">.</span><span class="nx">showImage</span><span class="p">(</span><span class="nx">image</span><span class="p">);</span></div><div class='line' id='LC9'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC10'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1731648/89de647c4000cb1aad368e7b9e58072e55b8b2b9/2.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1731648#file_2.js" style="float:right;margin-right:10px;color:#666">2.js</a>
            <a href="https://gist.github.com/1731648">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>In a multi-room chat application, a user may hit a bookmark that should take them directly in to a specific chat room. For example, &#8220;#backbone&#8221;. The route callback that executes should take this room name and call the code that is necessary to enter the chat room.</p>
<p>Chances are this is a fairly involved set of code. You&#8217;ll need to load the list of users in the chat room. You&#8217;ll need to clear the current chat windows and possibly pre-load recent messages to be displayed. You may also need to change a browser&#8217;s websocket event listeners to pick up events for this specific room instead, so that messages for this room can be displayed as they come in.</p>
<p>This is a lot of code to run, and it&#8217;s fare more code than should be allowed in a route callback method. It should, then, be encapsulated in an object that is responsible for registering the user as having entered that chat room. This object is then called from the router:</p>
<div id="gist-1731648" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Router</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nx">routes</span><span class="o">:</span> <span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s2">&quot;:roomname&quot;</span><span class="o">:</span> <span class="s2">&quot;chatroom&quot;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC5'>&nbsp;</div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="nx">chatroom</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">roomname</span><span class="p">){</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">ChatApp</span><span class="p">.</span><span class="nx">enterRoom</span><span class="p">(</span><span class="nx">roomname</span><span class="p">);</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC9'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1731648/124f65b2e0709143308b84f18e4cc4c9e11d6adf/3.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1731648#file_3.js" style="float:right;margin-right:10px;color:#666">3.js</a>
            <a href="https://gist.github.com/1731648">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Furthermore, there&#8217;s a high likelihood that the user will be able to enter chat rooms using some interaction on the web page. They may click on a chat room name in their favorite&#8217;s list, or they may type in a command like &#8220;/join #backbone&#8221; in to the chat application&#8217;s command area. For any of the the multiple ways to enter a chatroom, the code that is executed should be the same. Having the route callback be as simple and stupid as possible will promote code re-use and allow these three options to be easily implemented. If you only need to call &#8220;ChatApp.enterRoom(&#8216;someRoom&#8217;)&#8221; to enter any room that the user specifies, providing options for how the user enters a room becomes trivial.</p>
<h2>Putting It All Together</h2>
<p>There are many different ways of stringing these different stages together. There are many different tools that help us to easily create the HTML that we need, include the CSS and JavaScript assets, and kick things off. Some applications are small enough that an initialization step and contextual start step can be combined. Most applications of any significant size, though, should think about separating these in to explicit steps in order to promote better architecture, clean code, and re-use of code.</p>
<p>For me, I&#8217;m accomplishing stages 2 and 3 with my Backbone.Marionette add-on. It provides a very clean way to create initializers:</p>
<div id="gist-1731648" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">App</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Marionette</span><span class="p">.</span><span class="nx">Application</span><span class="p">();</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="nx">App</span><span class="p">.</span><span class="nx">addInitializer</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="c1">// add some app initialization code, here</span></div><div class='line' id='LC5'><span class="p">});</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'><span class="nx">App</span><span class="p">.</span><span class="nx">addInitializer</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="c1">// more initialization stuff </span></div><div class='line' id='LC9'>&nbsp;&nbsp;<span class="c1">// for a different part of the app</span></div><div class='line' id='LC10'><span class="p">});</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'><span class="c1">// run all the initializers and start the app</span></div><div class='line' id='LC13'><span class="nx">App</span><span class="p">.</span><span class="nx">start</span><span class="p">();</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1731648/bbd27a6654733f3d4ecfb49aadc29a536d5fbb8f/4.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1731648#file_4.js" style="float:right;margin-right:10px;color:#666">4.js</a>
            <a href="https://gist.github.com/1731648">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>At the time of this writing, though, I don&#8217;t have an explicit &#8220;startup&#8221; callback mechanism in place. I have a &#8220;start&#8221; method on my Marionette.Application object. But right now this really only fires up the initializer callbacks. So, I&#8217;m currently using the &#8220;initialize:after&#8221; event to run my start code. I see this as a problem, though &#8211; not of a technical nature, but of a semantic nature. And yes, semantics are important.</p>
<div id="gist-1731648" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">App</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Marionette</span><span class="p">.</span><span class="nx">Application</span><span class="p">();</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="cm">/* ... initializers go here ... */</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="c1">// contextual startup</span></div><div class='line' id='LC6'><span class="nx">App</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;initialize:after&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="nx">Backbone</span><span class="p">.</span><span class="nx">history</span><span class="p">.</span><span class="nx">start</span><span class="p">();</span></div><div class='line' id='LC8'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1731648/102becd9b2b776c4569830f2383ae625105885a2/5.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1731648#file_5.js" style="float:right;margin-right:10px;color:#666">5.js</a>
            <a href="https://gist.github.com/1731648">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>I&#8217;m not sure what the new implementation will look like, but it may end up being a &#8220;startup&#8221; callback method, similar to the initializer methods. The startup callback (or callbacks?) would execute after all of the initialization is done. This will provide a cleaner separation between the two concerns of initialization vs startup. I&#8217;ll have to see where this leads me, though, as right now, the majority of what I do for a contextual start is simply call &#8220;Backbone.history.start()&#8221;, as I showed in the above example.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=d8eSj1rOt_8:RsDjNC197bU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=d8eSj1rOt_8:RsDjNC197bU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=d8eSj1rOt_8:RsDjNC197bU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=d8eSj1rOt_8:RsDjNC197bU:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=d8eSj1rOt_8:RsDjNC197bU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=d8eSj1rOt_8:RsDjNC197bU:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/d8eSj1rOt_8" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/derickbailey/2012/02/06/3-stages-of-a-backbone-applications-startup/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/e592bd1326b1e80188ed4c0bf26b9f75?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Derick Bailey</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/derickbailey/2012/02/06/3-stages-of-a-backbone-applications-startup/</feedburner:origLink></item>
	<item>
		<title>A year in review with CQRS</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/P2oupNFdZ-E/</link>
		<comments>http://lostechies.com/johnteague/2012/02/05/a-year-in-review-with-cqrs/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 21:31:54 +0000</pubDate>
		<dc:creator>John Teague</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/johnteague/?p=94]]></guid>
		<description><![CDATA[For the past year my team has been building and maintaining and application using CQRS with an Event Store as our persistence model.  I started gathering requirements for this project last January and the team started development in February.  We&#160;&#8230; <a href="http://lostechies.com/johnteague/2012/02/05/a-year-in-review-with-cqrs/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>For the past year my team has been building and maintaining and application using CQRS with an Event Store as our persistence model.  I started gathering requirements for this project last January and the team started development in February.  We deployed the first version in June.  The first version was the bare minimum the end users needed to stop using Excel for the database and manual process.  After the initial deployment we continued to make further enhancements to put in the &#8220;Oops we really need this feature&#8221; to the application and usability enhancements to make it even more functional.  We are still working on this application today.  The business made a radical change in process and now were changing the application to meet the new needs of the business.</p>
<p>Given our history with this application and the phases we&#8217;ve gone through (initial development, maintenance / enhancements, and complete change in process) I think it&#8217;s a good case study in the feasibility of using CQRS style applications.  We&#8217;ve made some good decisions and some, err.., not so good decisions that I learned quite a bit from.</p>
<h3>A little about the Application</h3>
<p>The application is the standard &#8220;enterprise workflow&#8221; application, where the end users work product goes through several stages and different people do different tasks in order to complete the work.  These type of State-based, processes fit well with the CQRS model, as different events occur on the items in question, which determine what&#8217;s required and what action needs to take place.  There were very few &#8220;CRUD&#8221; type screens, but there were a few.</p>
<h3>My definition of CQRS</h3>
<blockquote><p>Separate models for reads and writes</p></blockquote>
<p>I think my definition heavily influenced how we built this application.  Notice there&#8217;s nothing there about messaging, MSMQ, distributed architecture, Event Sourcing, etc&#8230;  We did use some of these technologies and architecture in this app, however <em>they are not required for CQRS</em>. It&#8217;s simply an acknowledgement that the write model looks and behaves differently from the read model.  This simple acknowledgment can have profound impact on the way build your application and your ability to solve problems for your end users (and if you&#8217;re not doing that, well&#8230;)</p>
<h3>Not Everything has to fit into the CQRS Mold</h3>
<p>I think one common mistake made while building applications with CQRS is forcing every single functionality through the CQRS square hole.  There were parts of our application that were very simple and did not require the complexity of CQRS.  That&#8217;s right, CQRS is complex. I said it.  Commands must be created, validated and executed.  Events must be fired and handled.  Usually the complexity is mitigated by the simplification of the domain models.  But if you&#8217;re domain models are already simple, then a simpler approach will yield better results.</p>
<h3>Do Not Start With a Distributed Architecture</h3>
<p>When most people see examples or presentations of CQRS, it usually includes some sort of distributed architecture, where the commands and messages are put on some sort of queue, like MSMQ, RabbitMQ, or whatever.  And there can be some great advantages when using these techniques, mostly scalability.  Using a distributed approach increases the complexity to a whole new level.  However it is simply not necessary, at least at first.</p>
<p>For our application, we created a very simple in-process bus.  Meaning, execution of the command and all events were executed on the same thread.  We used an IOC container to find all of the event handlers based upon the event class type. It is a web application, so the threads were handled by the web server.  For a typical event, we had 4-6 event handlers, updating 4-5 different database tables.</p>
<p>There were some real advantages to starting with an in-process model.  We were able to get faster feedback when errors came up, and it was easier for us to find and fix bugs. This enabled us to build the application faster and get it into the end users quickly.</p>
<p>Once we were in production, we started recording metrics on the how quickly requests took and where there were performance issues.  What we discovered was the only real performance issues were when the users would perform batch operations, the same operation on many records (set based operations are nearly impossible with this architecture, or at least I haven&#8217;t figured out how to do it).  So once we determined which functions were commonly done in batch form, we moved them out of process with NServiceBus.  I think that&#8217;s one of the areas where this approach shines.  Moving these out of process, when necessary, is really simple.</p>
<blockquote><p>Move work out of process only when necessary</p></blockquote>
<h3>Think Creatively about your Domain and View Models</h3>
<p>One of the mistakes we made was the design of our domain models.  From a purely DDD / aggregate root / ubiquitous language, there was really only one AR.  But having just one AR means it&#8217;s REALLY big.  In retrospective, we should have been more creative with how we define the AR into something different.  Honestly, while this application has come complicated logic, I don&#8217;t think that we needed to solve it with a Domain Driven approach.  However, the library we used (per client request) required us to do so.  I don&#8217;t have a solution for this yet, when I do I&#8217;ll let you know (hint, I think the Actor/Model patter Chris Patterson has been talking about is a better fit).</p>
<h3>Event Sourcing &#8230; Meh</h3>
<p>Event Sourcing has been probably the most misunderstood and miscommunicated implementation strategy surrounding CQRS.  Like other pieces of the architecture, it&#8217;s an implementation choice, not a requirement for building this type of application.  Honestly, I was never sold on the concept before this project.  On this project I don&#8217;t think we got any of the benefits touted from using it.  To be clear, I think one of the problems is our implementation of the Event Sourcing persistence.  We are currently storing a binary copy of the event.  Which means that event lives in perpetuity and we can&#8217;t change it nor get rid of it. If it was serialized with something like Protocol Buffers I think we would have more flexibility, but I haven&#8217;t tested that yet so I can&#8217;t say for certain.</p>
<p>The biggest feature about using ES is the ability to replay the events and rebuild the state of your view models.  This sounds really cool, you get the ability to create new views in your application (like new reports)  and be fully avaialble with all data as if they were there from the beginning.  The only problem is that we&#8217;ve only done that once in the entire lifetime of the application.</p>
<p>But at the same time, using the event sourcing approach hasn&#8217;t really cost us anything either.  In the end, for this application, I would have been happy with just a Key-Value store of data or a document database for aggregate persistence.</p>
<p>Overall, I&#8217;m pleased with the application.  Like I said there was a big change in business process and we&#8217;ve been able to make those changes very fast in my opinion.  This is mainly due to the extreme decoupling you get from the way views are represented to how a domain object is changed or created.  I will likely continue to create applications of this type using this approach.  With a few changes underneath of course:)</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=P2oupNFdZ-E:WE9pk0BNijo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=P2oupNFdZ-E:WE9pk0BNijo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=P2oupNFdZ-E:WE9pk0BNijo:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=P2oupNFdZ-E:WE9pk0BNijo:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=P2oupNFdZ-E:WE9pk0BNijo:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=P2oupNFdZ-E:WE9pk0BNijo:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/P2oupNFdZ-E" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/johnteague/2012/02/05/a-year-in-review-with-cqrs/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/4ec3c07d6f4527238a3f5c6ec5fefd63?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">John Teague</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/johnteague/2012/02/05/a-year-in-review-with-cqrs/</feedburner:origLink></item>
	<item>
		<title>Get A Model From A Backbone Collection Without Knowing If The Collection Is Loaded</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/XeHBx3R6ztk/</link>
		<comments>http://lostechies.com/derickbailey/2012/02/03/get-a-model-from-a-backbone-collection-without-knowing-if-the-collection-is-loaded/#comments</comments>
		<pubDate>Fri, 03 Feb 2012 15:51:52 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Async]]></category>
		<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Backbone.Marionette]]></category>
		<category><![CDATA[Javascript]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/derickbailey/?p=799]]></guid>
		<description><![CDATA[In working with a client recently, we ran in to a rather sticky situation. We were setting up a route to &#8220;persons/:id&#8221;, and as you would expect, we wanted to load the person in question and display the details of&#160;&#8230; <a href="http://lostechies.com/derickbailey/2012/02/03/get-a-model-from-a-backbone-collection-without-knowing-if-the-collection-is-loaded/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>In working with a client recently, we ran in to a rather sticky situation. We were setting up a route to &#8220;persons/:id&#8221;, and as you would expect, we wanted to load the person in question and display the details of that person. The trick, though, is that we needed to wait until the persons collection was loaded to be able to retrieve the person from the collection. If we navigate to this route from somewhere else in the application, this isn&#8217;t an issue. The persons collection was already loaded and everything goes on normally. If we use a bookmark to get to this url directly, though, then there was no guarantee that the persons collection was loaded because we had not previously run any code to load the collection.</p>
<p>Depending on how the application is architected, and when the persons collection is expected to be loaded, there are a few options that I can see for solving this problem.</p>
<p><span style="font-size: 18px; font-weight: bold;">Option: Fetch The Model From The Server</span></p>
<p>The most basic option, and probably the easiest to deal with, is just to fetch the model from the server based on the id parameter that you get from the route.</p>
<p>In this case you just need to create a new model instance, set the `id` attribute on the model directly and then call `.fetch` on the model. It will make the trip to the server to get the data. You can either listen to a &#8220;change&#8221; event on the model or provide a &#8220;success&#8221; callback in the fetch method, to know when the data has been returned so that you can load it in to your view and display it as needed.</p>
<div id="gist-1730803" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">personById</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">id</span><span class="p">){</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">person</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Person</span><span class="p">();</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nx">person</span><span class="p">.</span><span class="nx">id</span> <span class="o">=</span> <span class="nx">id</span><span class="p">;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nx">person</span><span class="p">.</span><span class="nx">fetch</span><span class="p">({</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">success</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">model</span><span class="p">,</span> <span class="nx">response</span><span class="p">){</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">App</span><span class="p">.</span><span class="nx">showPerson</span><span class="p">(</span><span class="nx">model</span><span class="p">);</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="p">})</span></div><div class='line' id='LC9'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1730803/766ff0b3d93a3c2409d8f6d196581e8e1ec28477/1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1730803#file_1.js" style="float:right;margin-right:10px;color:#666">1.js</a>
            <a href="https://gist.github.com/1730803">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The major problem here is that you have not loaded the persons collection at all. If the persons collection is expected to be loaded because it&#8217;s being used for something related to displaying or working with the individual person model, then this option might not work for you. You could run some additional code to load the collection separately (asynchronously, since it works that way by default). This might help get around any potential issues of needing the collection.</p>
<h2>Option: Use The Collection&#8217;s &#8220;reset&#8221; Event</h2>
<p>Another easy option may be to load the collection when the request for the route is made. You would set up a new collection instance, bind a callback function to the collection&#8217;s &#8220;reset&#8221; event, and then call `.fetch` on the collection. The callback method would be responsible for retrieving the specific model from the collection and then creating and displaying the view.</p>
<div id="gist-1730803" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">personById</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">id</span><span class="p">){</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">persons</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">PersonCollection</span><span class="p">();</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="c1">// note that &quot;bind&quot; is now &quot;on&quot; in Backbone v0.9.x</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="nx">persons</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;reset&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">collection</span><span class="p">,</span> <span class="nx">response</span><span class="p">){</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">person</span> <span class="o">=</span> <span class="nx">collection</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">id</span><span class="p">);</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">App</span><span class="p">.</span><span class="nx">showPerson</span><span class="p">(</span><span class="nx">person</span><span class="p">);</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="nx">persons</span><span class="p">.</span><span class="nx">fetch</span><span class="p">();</span></div><div class='line' id='LC11'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1730803/68fa47edcd14d5be22ff12f63298766a11d11970/2.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1730803#file_2.js" style="float:right;margin-right:10px;color:#666">2.js</a>
            <a href="https://gist.github.com/1730803">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>There are some potential issues with this solution, though. If you already have the persons collection loaded, then you&#8217;re going to load it a second time just to get the one model from it. To mitigate this problem, you would need two different entry points in to the display of the person: one for when you hit the route directly through a bookmark, and one for when the user is already in the app and navigates to the person display through other means.</p>
<p>Having two different entry points in to this part of the app may not be a bad idea. This largely depends on how the application is architected. You wouldn&#8217;t want to duplicate all of the code that sets up the display of the person&#8217;s details in both of the entry points, but you wouldn&#8217;t want to have a bunch of ugly if-statements in that code to determine how to set things up, either. Some simple abstractions of the common bits would help keep this code manageable.</p>
<h2>Option: Building An &#8220;onReset&#8221; Callback</h2>
<p><strong>UPDATE:</strong></p>
<p>FYI &#8211; I have an updated version of this code available in my <a href="https://github.com/derickbailey/backbone.marionette">Backbone.Marionette</a> plugin, as Backbone.Marionette.Callbacks. It <a href="http://lostechies.com/derickbailey/2012/02/07/rewriting-my-guaranteed-callbacks-code-with-jquery-deferred/">reduces the code and complexity significantly</a>, and also eliminates the race condition issues that I mention below. Be sure to use that Callbacks object instead of the code I&#8217;ve listed here.</p>
<p><strong>:END UPDATE</strong></p>
<p><strong><br /></strong></p>
<p>The third option that I can think of &#8211; and the one that I implemented for this particular client project &#8211; is a variation of using the collection&#8217;s reset event. The idea is to build an &#8220;onReset&#8221; callback system that is aware of whether or not the collection has already been loaded or is still waiting to be loaded.</p>
<p>If you have the persons collection being loaded from some other application initialization code, then you don&#8217;t necessarily have the ability to use a simple reset event as shown above. You could try to use the reset event, but there&#8217;s a race condition that is introduced in low-latency, high speed networks (i.e. your local development machine).</p>
<p>If you don&#8217;t control when the `.fetch` method is called, then you may end up binding to the reset event after the collection has already been reset. In that scenario &#8211; which is very likely to happen when working in a local development environment &#8211; your view for the specific person model will never get displayed.</p>
<p>The solution I came up with is to have a callback mechanism built in to the collection, that pays attention to the collection&#8217;s reset event and knows to either wait for the reset event to be fired, or to fire the callbacks immediately because the collection has already been loaded. I&#8217;m calling this an &#8220;onReset&#8221; callback, for lack of a better description at this point.</p>
<p>The code to use the onReset callbacks would look something like this:</p>
<div id="gist-1730803" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">// App.js</span></div><div class='line' id='LC2'><span class="c1">// ------</span></div><div class='line' id='LC3'><span class="c1">// some initialization code that happens elsewhere, using Backbone.Marionette</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="nx">App</span><span class="p">.</span><span class="nx">addInitializer</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="nx">App</span><span class="p">.</span><span class="nx">persons</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">PersonCollection</span><span class="p">();</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="nx">App</span><span class="p">.</span><span class="nx">persons</span><span class="p">.</span><span class="nx">fetch</span><span class="p">();</span></div><div class='line' id='LC8'><span class="p">});</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'><span class="c1">// PersonRouter.js</span></div><div class='line' id='LC13'><span class="c1">// ---------------</span></div><div class='line' id='LC14'><span class="c1">// The router callback that needs to get the person</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'><span class="nx">personById</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">id</span><span class="p">){</span></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="nx">App</span><span class="p">.</span><span class="nx">persons</span><span class="p">.</span><span class="nx">onReset</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">collection</span><span class="p">){</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">person</span> <span class="o">=</span> <span class="nx">collection</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">id</span><span class="p">);</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">App</span><span class="p">.</span><span class="nx">showPerson</span><span class="p">(</span><span class="nx">person</span><span class="p">);</span></div><div class='line' id='LC20'>&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC21'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1730803/956ae14990497446c30ffb762d3c903461698df3/3.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1730803#file_3.js" style="float:right;margin-right:10px;color:#666">3.js</a>
            <a href="https://gist.github.com/1730803">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>In this setup, adding an onReset callback guarantees the callback&#8217;s execution. If the collection has not yet been loaded, then it stores the callback and waits for the reset event to fire. If the reset event has already been fired, then it simply executes the callback immediately. Either way, your callback will be executed and you will have the collection available when it does.</p>
<h2>Race Condition Reduced. Eliminated?</h2>
<p>Here&#8217;s the implementation for the onReset code. It&#8217;s generally functional and I haven&#8217;t yet run in to any problems, yet.</p>
<div id="gist-1730803" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">OnResetCollection</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Collection</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nx">constructor</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nx">slice</span><span class="p">(</span><span class="nx">arguments</span><span class="p">);</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Collection</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">constructor</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">onResetCallbacks</span> <span class="o">=</span> <span class="p">[];</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;reset&quot;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">collectionReset</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="nx">onReset</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">){</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">onResetCallbacks</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">callback</span><span class="p">);</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">collectionLoaded</span> <span class="o">&amp;&amp;</span> <span class="k">this</span><span class="p">.</span><span class="nx">fireResetCallbacks</span><span class="p">();</span></div><div class='line' id='LC13'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;<span class="nx">collectionReset</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">collectionLoaded</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">collectionLoaded</span> <span class="o">=</span> <span class="kc">true</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">fireResetCallbacks</span><span class="p">();</span></div><div class='line' id='LC20'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'>&nbsp;&nbsp;<span class="nx">fireResetCallbacks</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">callback</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">onResetCallbacks</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="nx">callback</span><span class="p">){</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">callback</span><span class="p">(</span><span class="k">this</span><span class="p">);</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">fireResetCallbacks</span><span class="p">();</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC28'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC29'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1730803/8507df0b66d1788cf02245b8fd2bb05fcd4e8df5/4.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1730803#file_4.js" style="float:right;margin-right:10px;color:#666">4.js</a>
            <a href="https://gist.github.com/1730803">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>You can then extend from OnResetCollection instead of Backbone.Collection to get this functionality.</p>
<p>I still worry that there&#8217;s a potential race condition in between the logic and the pop&#8217;ing of items off the array. I&#8217;ve travelled every logical path of execution for the asynchronous call and onReset call that I can think of, and I can&#8217;t find any issue. I would love to hear from someone more experienced with race conditions in JavaScript, though.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=XeHBx3R6ztk:r4ex9KWLLyo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=XeHBx3R6ztk:r4ex9KWLLyo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=XeHBx3R6ztk:r4ex9KWLLyo:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=XeHBx3R6ztk:r4ex9KWLLyo:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=XeHBx3R6ztk:r4ex9KWLLyo:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=XeHBx3R6ztk:r4ex9KWLLyo:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/XeHBx3R6ztk" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/derickbailey/2012/02/03/get-a-model-from-a-backbone-collection-without-knowing-if-the-collection-is-loaded/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/e592bd1326b1e80188ed4c0bf26b9f75?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Derick Bailey</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/derickbailey/2012/02/03/get-a-model-from-a-backbone-collection-without-knowing-if-the-collection-is-loaded/</feedburner:origLink></item>
	<item>
		<title>JavaScript File &amp; Folder Structures: Just Pick One</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/ODkkZo82XnY/</link>
		<comments>http://lostechies.com/derickbailey/2012/02/02/javascript-file-folder-structures-just-pick-one/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 13:29:31 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[AntiPatterns]]></category>
		<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Model-View-Controller]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/derickbailey/?p=795]]></guid>
		<description><![CDATA[Rails wants you to put specific files in specific folder structures, based on the object type that will be in the file. Java demands that files in a folder structure are namespaced by that folder structure. VisualStudio also makes it&#160;&#8230; <a href="http://lostechies.com/derickbailey/2012/02/02/javascript-file-folder-structures-just-pick-one/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>Rails wants you to put specific files in specific folder structures, based on the object type that will be in the file. Java demands that files in a folder structure are namespaced by that folder structure. VisualStudio also makes it seem like file / folder names should be the namespace / class name as well &#8211; but that&#8217;s just a good suggestion and not a requirement. Other languages and frameworks have some requirements for how you organize your files, too… with the exception of browser based JavaScript (… mostly…)</p>
<h2>JavaScript File Organization: You&#8217;re Doing It Right</h2>
<p>The reality of JavaScript in general web-based development (not talking about server side at all, here) is that it makes absolutely no difference how your files and folders are organized. Zero difference at all… at least as far as the JavaScript runtime is concerned.</p>
<p>The only thing that matters is that you include your files in your HTML with a &lt;script&gt; block, correctly. You also need to pay attention to which order they are included in most cases, to make sure things are defined before they are used (tools like RequireJS and other script loaders and module definition / loaders help with this).</p>
<p>What does this really mean? It means:</p>
<blockquote>
<p><strong><em>You&#8217;re Doing It RIGHT!</em></strong></p>
</blockquote>
<p>Yup. You&#8217;re doing it right, because it doesn&#8217;t matter how you do it.</p>
<p>What does matter, though, is that you and your team (if you have a team) pick an organizational convention and stick with it. It&#8217;s actually very more important for your team to have a good file and folder structure for your JavaScript. But you don&#8217;t need to worry about what that structure is. Pick a standard and use it. When it fails (and it will), re-work your structure so that it works within the newly understood constrains and move on.</p>
<p>Of course, that fact that your browser and it&#8217;s JavaScript runtime don&#8217;t care about your file and folder organization doesn&#8217;t I&#8217;m without <em>my</em> opinions on how files and folders should be organized.</p>
<p>For example&#8230;</p>
<h2>M, V and C Folders</h2>
<p>A lot of people organize JavaScript MV* application files in a folder structure like this (BackboneJS in this case):</p>
<p><img title="Screen Shot 2012-02-01 at 9.43.16 PM.png" src="http://lostechies.com/derickbailey/files/2012/02/Screen-Shot-2012-02-01-at-9.43.16-PM.png" border="0" alt="Screen Shot 2012 02 01 at 9 43 16 PM" width="417" height="80" /></p>
<p>To the best of my knowledge, this folder structure is based on the &#8220;models&#8221;, &#8220;views&#8221; and &#8220;controllers&#8221; folder structure that was popularized by Ruby on Rails. Sure others may have had it first, but it was Rails that made it popular. Other MVC framework followed suit and demanded that you put your controller objects in the controllers folder, your model objects in your models folder, etc. But unless you&#8217;re Rails (or another framework that wants to be like Rails), this folder structure is stupid.</p>
<p>I&#8217;m pretty sure that Rails uses this folder structure to assume the types of objects that are found within the files. And I know for sure that it uses file names to assume the class that will be defined within the file. That is, when rails sees a file called &#8220;/app/controllers/foo_controller.rb&#8221;, it expects to find a class called &#8220;FooController&#8221; and it expects that this class will inherit from some Rails controller base class. If these expectations are not met, errors are thrown to say so.</p>
<p>I understand why Rails does it this way: file and folder based conventions make it easy to assume what a file will contain, and that makes it easy for the runtime to optimize for performance when pre-loading and caching the code contained within the files. This makes sense to me in Rails because the convention is based on good ideas for optimizing the way Rails works and the way it looks for files and how it loads them.</p>
<p>But, unless you&#8217;re Rails or another framework that wants to assume certain files in certain folder contain certain code, this is a terrible way to organize files.</p>
<h2>The Junk Drawer</h2>
<p>There are some good examples of other standards along this line. For example, I tend to follow the convention of a &#8220;public&#8221; folder with &#8220;css&#8221;, &#8220;images&#8221;, and &#8220;javascripts&#8221; folders. But honestly, this folder structure exhibits many of the same problems of being stupid that organizing files in M, V, and C folders does.</p>
<p>The real problem with these types of folder structures is that they become junk drawers. Even DHH and the Rails core team recognize that this is a poor folder structure outside the confines of Rails+Ruby code. That&#8217;s one of the reasons they added the Asset Pipeline in Rails 3.1. DHH even called the &#8220;javascripts&#8221; folder a junk drawer, very directly, in a RailsConf keynote in 2011 (or was it 2010?) &#8211; complete with a slide showing a drawer full of junk.</p>
<p>With any application that moves beyond a trivial number of files, these content-type, mime-type, code-type and general type-based folder structures turn in to a bloated pile of junk that is very difficult to sift through. Who wants to look at a folder with 20, 50 or 100 files in it, when you only really care about 2, 5 or 10 of those files?</p>
<p>And what happens when you suddenly have an object type that doesn&#8217;t fit your pre-established conventions? You end with a &#8220;lib&#8221; folder, like Rails, which becomes the ultimate junk drawer. &#8220;It&#8217;s not a model? It&#8217;s not a controller? It goes in lib.&#8221; &#8211; no matter what the actual functionality contained within the file is. The &#8220;lib&#8221; folder is asking to be a junk drawer… demanding it, really. So, do you follow that same junk drawer convention for non-M, V or C type-based files in your JavaScript apps? That doesn&#8217;t any make sense to me.</p>
<h2>How I Organize Files: By Functionality</h2>
<p>I prefer to organize my JavaScript files the way I used to organize my C# files in .NET projects: by functional area of the application. That is, I group files together in folders based on the area of the application that they facilitate.</p>
<p>For example, my BBCloneMail application has the following folder structure for it&#8217;s JavaScript:</p>
<p><img title="Screen Shot 2012-02-01 at 9.47.12 PM.png" src="http://lostechies.com/derickbailey/files/2012/02/Screen-Shot-2012-02-01-at-9.47.12-PM.png" border="0" alt="Screen Shot 2012 02 01 at 9 47 12 PM" width="418" height="112" /></p>
<p>Note that I&#8217;m still using the &#8220;javascripts&#8221; parent folder, but underneath of that I&#8217;m organizing by functional area of the application. In the root &#8220;javascripts&#8221; folder, are the primary application files &#8211; the ones that define the overall application bits. In the &#8220;mail&#8221; folder are all of the files that relate to the &#8220;mail&#8221; application. And, in the &#8220;contacts&#8221; folder are all of the files that relate to the &#8220;contacts&#8221; application.</p>
<p>I don&#8217;t care what &#8220;type&#8221; is contained in the file. That&#8217;s a completely irrelevant way to organize files to me. It makes no sense for me to organize files this way because many of my files contain more than one &#8220;type&#8221; of object. For example, I often put very simple model, collection and view definitions that are very closely related, in the same file.</p>
<h2>Why Type-Based Folders Might Be A Good Idea</h2>
<p>In spite of all my over-opinionated hand-waiving above, there are some good reasons to use type based folders in JavaScript. One reason is asynchronous file loading based on conventions.</p>
<p>You might have a JavaScript app that makes use of a templating engine (of which there are dozens, these days). It&#8217;s not always a good idea to pre-load every possible template in to the user&#8217;s browser, for download size and performance reasons. Sometimes it makes sense to fetch the template that you want the first time it&#8217;s requested.</p>
<p>To do this, it might make sense to use a convention to retrieve the files. I&#8217;ve seen several Backbone apps that use a jQuery selector to load files, as one example of this. When a Backbone view specifies a template as &#8220;#my-view-template&#8221;, the application&#8217;s template manager would make a request to the server to load something along the lines of &#8220;/templates/my-view-template.html&#8221;.</p>
<p>If you&#8217;re trying to organize your templates in a functional area of the application, you&#8217;ll have the added overhead of inserting the functional area folder name, such as &#8220;/mail/templates/inbox-template.html&#8221; for an &#8220;Inbox&#8221; view in a &#8220;mail&#8221; app, trying to load an &#8220;#inbox-template&#8221; jQuery selector as the template to render.</p>
<p>So… there&#8217;s at least one possible reason to use a type-based folder. I would still stick the type-based folder name under my functional area, though. I don&#8217;t want to mix up the templates between my functional areas of the application, by accident.</p>
<h2>A Tradeoff: Folder Names vs File Names</h2>
<p>Here&#8217;s one potential trade-off for using functional folder names vs type based folder names: you might have to specify the type in the file name. For example, if you have a model type name &#8220;Person&#8221;, a collection type named &#8220;Persons&#8221;, and a view to represent a single person or a collection of persons, what do you call that view? If you&#8217;re organizing things by type, you can call every &#8220;Person&#8221; and &#8220;Persons&#8221;</p>
<p>This can be very confusing. I was recently working with a client who was using an editor that only shows the file name for the open files, and none of the folder path for the files. He ended up with 4 &#8220;person.js&#8221; files in his open file list. Which one was the Person model, view, router, or controller file? We had to open each file to find the one we needed, every time. So, we took the hit on the file names. The &#8220;person.js&#8221; file contained the person model, while &#8220;persons.js&#8221; contained the collection, &#8220;personviews.js&#8221; contained the view definitions and &#8220;personrouter.js&#8221; contained the router. Thus, we&#8217;ve moved the need for specifying the object type from the folder structure (with all it&#8217;s bad ideas for using that) to the file name.</p>
<p>I&#8217;ve read at least one blog post that advocated using type-based folder names specifically to avoid the &#8220;ugliness&#8221; of having type names in your files. I seriously laughed out loud when I read that. Whether the type is in the file name or folder name is a moot point. You&#8217;re likely going to end up specifying the type somewhere. I would much rather have it in my file names because it&#8217;s easier for me to see things grouped together based on functionality, than based on the type of object contained in a file.</p>
<h2>It&#8217;s Just An Opinion, And A Loose One At That</h2>
<p>My own use of these conventions and ideas is rather loose at this point. I don&#8217;t stick strictly to anything, and I mix and match based on the project type, number of files and other constraints that a given project presents. Because, like I said at the top of this egregiously long post, you&#8217;re doing it right.</p>
<p>No matter what file and folder structure you pick for you JavaScript apps (assuming you&#8217;re using a suite of libraries that doesn&#8217;t force you in to a specific folder structure), you&#8217;re doing it right. JavaScript in a browser environment really doesn&#8217;t care what the file and folder structure is. But that doesn&#8217;t mean we as developers shouldn&#8217;t care. Pick a file and folder structure that fits the constraints of your application and change the structure as your app&#8217;s constraints change.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=ODkkZo82XnY:85tgx-gdHIQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=ODkkZo82XnY:85tgx-gdHIQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=ODkkZo82XnY:85tgx-gdHIQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=ODkkZo82XnY:85tgx-gdHIQ:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=ODkkZo82XnY:85tgx-gdHIQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=ODkkZo82XnY:85tgx-gdHIQ:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/ODkkZo82XnY" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/derickbailey/2012/02/02/javascript-file-folder-structures-just-pick-one/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/e592bd1326b1e80188ed4c0bf26b9f75?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Derick Bailey</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/derickbailey/2012/02/02/javascript-file-folder-structures-just-pick-one/</feedburner:origLink></item>
	<item>
		<title>Improving the Git Windows experience: Downloads</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/LXEupXHNrLs/</link>
		<comments>http://lostechies.com/jimmybogard/2012/02/01/improving-the-git-windows-experience-downloads/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 18:22:47 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[Mercurial]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/jimmybogard/2012/02/01/improving-the-git-windows-experience-downloads/]]></guid>
		<description><![CDATA[I love Git. It’s very powerful tool that lets me bend my repository to my will, with commands and features that blow the other source control providers I’ve used out of the water. However, the tooling just doesn’t do it&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/02/01/improving-the-git-windows-experience-downloads/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>I love Git. It’s very powerful tool that lets me bend my repository to my will, with commands and features that blow the other source control providers I’ve used out of the water.</p>
<p>However, the tooling just doesn’t do it justice. From the download, installation, integration and CLI experience, it always feels like (in Windows land) that you’re playing in someone else’s back yard.</p>
<p>Over the next few posts, I’m going to compare the experience of using Git with that of Mercurial, who has, in my opinion, lesser features, but a much MUCH better experience.</p>
<h3>The Mercurial download experience</h3>
<p>Let’s look at searching and downloading the Mercurial client. When I google “Mercurial” or “Mercurial Windows” or “Mercurial Windows Download” or variants, two of the top results are the <a href="http://mercurial.selenic.com/">official Mercurial home page</a>, or the official Windows client, <a href="http://tortoisehg.bitbucket.org/">TortoiseHg</a>.</p>
<p>From there, I want to download Mercurial. Both websites offer very clear ways of doing so. The Mercurial site:</p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/02/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="http://lostechies.com/jimmybogard/files/2012/02/image_thumb.png" width="644" height="231"></a></p>
<p>And the Tortoise Hg site:</p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/02/image1.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="http://lostechies.com/jimmybogard/files/2012/02/image_thumb1.png" width="644" height="371"></a></p>
<p>Two very large “download buttons”. These buttons are interesting in that:</p>
<ul>
<li>They link <strong>directly</strong> to the file to be downloaded.
<li>They both link to the <strong>exact same installer</strong>
<li>They know what OS you’re using, and display the correct installer accordingly</li>
</ul>
<p>TortoiseHg is the official Hg client for Windows, and includes:</p>
<ul>
<li>The command line interface
<li>Windows Explorer integration
<li>Visual tools (Workbench etc.)
<li>Visual Studio integration
<li>Merge tools</li>
</ul>
<p>It’s a completely out-of-the box client that includes EVERYTHING that you might need to run Mercurial, all in one package, and consistently presented to the end user.</p>
<p>Next, let’s look at the Git download experience.</p>
<h3>The Git download experience</h3>
<p>When searching for Git downloads, you’re primarily directed to one of two sites – <a href="http://git-scm.com/">the official Git site</a>, or the <a href="http://code.google.com/p/msysgit/">official Git tools site for Windows</a>, hosted on Google Code (and also GitHub, curiously enough). The Git site is clean enough:</p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/02/image2.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="http://lostechies.com/jimmybogard/files/2012/02/image_thumb2.png" width="644" height="375"></a></p>
<p>Except I have 3 download links instead of one. Not a big deal most of the time, but already choices are presented to the end user over the Mercurial site. Clicking on the Windows link takes me to this page:</p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/02/image3.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="http://lostechies.com/jimmybogard/files/2012/02/image_thumb3.png" width="644" height="283"></a></p>
<p>Instead of linking me directly to the installer file to download immediately, I’m directed to the downloads page of the Google Code site, where I am presented with yet even more options. There is nothing in this screen that screams “THIS IS THE INSTALLER YOU WANT IGNORE THE OTHERS”. As someone new to Git, how do I know which to choose? Probably the first one, and most people would choose the first one, but <strong>presenting choices here is pointless and confusing</strong>.</p>
<p>Not to mention, I’m whisked away to a site that has nothing to do with the original Git site. The official Git site didn’t mention “msysgit” but now I’m on the msysgit Google Code site. Even more confusing is that the file has the name “preview” in it, and the installer is labeled as “Beta”. So is the right one or not? I might be inclined to search for the last “good” release and not a beta/preview one.</p>
<p>The Git installer is also less featured than the Mercurial one. The official Git Windows tools include:</p>
<ul>
<li>Windows Explorer integration (very limited)
<li>A CLI through the Git Bash or directly in a command prompt
<li>Visual tools (OK tools)</li>
</ul>
<p>However, I typically don’t point folks to the official Git client. Instead, I point them to Git Extensions, a more fully featured toolset that includes:</p>
<ul>
<li>Windows Explorer integration
<li>Visual Studio integration
<li>Richer visual tools
<li>Bundled merge tool
<li>Bundled Git installer</li>
</ul>
<p>This isn’t the official Git Windows client, so you basically have to know it exists to find it. Almost none of the online tutorials recommend it, even though it matches much more closely to what Mercurial provides out of the box.</p>
<h3>Improving the Git download experience</h3>
<p>In two easy steps:</p>
<ul>
<li>Have the official Windows client be as full featured as the Hg one. Could just start with Git Extensions and go from there.
<li>Copy the Mercurial website’s behavior</li>
</ul>
<p>In short, <strong>prefer Simplicity over Choices</strong>. Have defaults, and obvious ways to get to the non-defaults.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=LXEupXHNrLs:D2tefklpeaA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=LXEupXHNrLs:D2tefklpeaA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=LXEupXHNrLs:D2tefklpeaA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=LXEupXHNrLs:D2tefklpeaA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=LXEupXHNrLs:D2tefklpeaA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=LXEupXHNrLs:D2tefklpeaA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/LXEupXHNrLs" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/jimmybogard/2012/02/01/improving-the-git-windows-experience-downloads/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/cc359c5ccf90d7a24b5976316797b5ec?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Jimmy Bogard</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/jimmybogard/2012/02/01/improving-the-git-windows-experience-downloads/</feedburner:origLink></item>
	<item>
		<title>Great time to be a developer</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/EnPDzlpZS2E/</link>
		<comments>http://lostechies.com/joshuaflanagan/2012/01/31/great-time-to-be-a-developer/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 05:09:58 +0000</pubDate>
		<dc:creator>Joshua Flanagan</dc:creator>
				<category><![CDATA[heroku]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sinatra]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/joshuaflanagan/2012/01/31/great-time-to-be-a-developer/]]></guid>
		<description><![CDATA[I am in awe of the free tools available to software developers today. It is amazing how fast, and cheaply, you can turn an idea into productive code. I was so pumped by a recent experience, I decided to share.&#160;&#8230; <a href="http://lostechies.com/joshuaflanagan/2012/01/31/great-time-to-be-a-developer/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>I am in awe of the free tools available to software developers today. It is amazing how fast, and cheaply, you can turn an idea into productive code. I was so pumped by a recent experience, I decided to share.</p>
<h2>The Problem</h2>
<p>My employer is moving to a new location in a part of the city that I&#8217;m not very familiar with. I have no idea what the traffic patterns are like, and I&#8217;m wondering when to leave for work in the morning. I tried looking at various web mapping services. Some factor in <em>current</em> traffic, but I couldn&#8217;t find any that could tell me historical traffic information, making it impossible to make a decision about departure time in advance.</p>
<h2>Idea</h2>
<p>Tracking historical travel times for the world would be a huge task, but what if I could create a small history of my specific route? The TomTom Live Traffic Route Planner site can give me directions to work, and estimate travel time based on current traffic conditions. I discovered that it returns a lot of the trip information from a single AJAX call. A quick copy/paste of the URL to <font size="4" face="Cordia New">curl</font> confirmed that I could repeat the call and get the same data. I just need to hit that endpoint at various times in the morning and store the results. Later, I&#8217;ll be able to analyze the results and determine the best time to leave for work.</p>
<h2>Keep it Minimal</h2>
<p>Now I have an idea, but I don&#8217;t know if it is worth pursuing. I don&#8217;t know if the data I&#8217;m getting is accurate (it worked once or twice from curl <em>now</em>, but maybe the long URL contains some session id that will expire?). I don&#8217;t want to invest a lot of time or money in building it out. Also, it&#8217;s 6pm Tuesday night, I only have 3 more days left this week before I make the commute to the new office. I need to start collecting data as soon as possible; hopefully Wednesday morning. It&#8217;s time to write some code. Fast.</p>
<p>This is where the quality of available tools really make an impact. To name a few:</p>
<blockquote><p><a href="http://www.ruby-lang.org" target="_blank">Ruby</a> &#8211; low ceremony scripting with a vast ecosystem of libraries to accomplish common tasks.</p>
<p><a href="https://github.com/jnunemaker/httparty" target="_blank">HTTParty</a> &#8211; crazy simple library to call a URL and get a Ruby hash of the response data &#8211; no parsing, no Net:HTTP.</p>
<p><a href="http://www.heroku.com/" target="_blank">Heroku</a> &#8211; There is no better option for hosting applications in the early proving stage. Create and deploy a new app in seconds, for free. The free <a href="http://addons.heroku.com/scheduler" target="_blank">Heroku Scheduler</a> add-on lets me run a script every 10 minutes in my hosted environment &#8212; exactly what I need for my data collection.</p>
<p><a href="http://www.mongodb.org/" target="_blank">MongoDB</a> &#8211; natural fit for persisting an array (the trips calculated every 10 minutes) of ruby hashes (responses from traffic service). No schema, no mapping, no fuss.<br />&nbsp;<br /><a href="http://addons.heroku.com/mongolab" target="_blank">MongoLabs</a> &#8211; free MongoDB hosting on Heroku. One click to add, and I have a connection string for my own 240MB in the cloud. Sweet.</p>
</blockquote>
<p>By 11pm Tuesday night, my script is running in the cloud, ready to start collecting data Wednesday morning. I&#8217;m not going to spend any time on building a UI until I know if the data collection works.</p>
<h2>Checkpoint</h2>
<p>On Wednesday night, I use the mongo console to review the trip data that was collected in the morning. I see that the trip duration changes for each request, which gives me hope that I&#8217;ll have meaningful data to answer my question. However, I also notice that the reported &#8220;traffic delay&#8221; time is always zero. I&#8217;m a little concerned that my data source isn&#8217;t reliable. I&#8217;m glad I haven&#8217;t invested too much yet. At this point, I can just write off the time as a well-spent refresher of MongoDB.</p>
<h2>Further exploration</h2>
<p>I&#8217;m still curious to see a visualization of the data. I decide to spend a couple hours to see if I can build a minimal UI to chart the departure vs. duration times. Again, the available tools gave me fantastic results with minimal effort:</p>
<blockquote><p><a href="http://www.sinatrarb.com/" target="_blank">sinatra</a> &#8211; an incredibly simple DSL for exposing your ruby code to the web. All I needed was a single endpoint that would pull data from mongo and dump it to the client to render a chart. Anything more than sinatra would be overkill, and anything less would be tedious.</p>
<p><a href="http://www.highcharts.com/" target="_blank">Highcharts JS</a> &#8211; amazing javascript library for generating slick client-side charts. A ton of options (including the very helpful <a href="http://www.highcharts.com/demo/spline-irregular-time" target="_blank">datetime x-axis</a>), well-documented, and free for non-commercial use. I didn&#8217;t have a &#8220;go-to&#8221; option for client-side charting, so I had to do a quick survey of what was available. This is the first one I tried, and it left me with absolutely no reason to look at others.</p>
</blockquote>
<p>After a couple hours (mostly to learn Highcharts), I have my chart and a potential answer (leave before 7:45, or after 9):<br /><a href="http://lostechies.com/joshuaflanagan/files/2012/01/traveltime_chart.png"><img style="background-image: none; border-right-width: 0px; margin: 0px 0px 24px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="traveltime_chart" border="0" alt="traveltime_chart" src="http://lostechies.com/joshuaflanagan/files/2012/01/traveltime_chart_thumb.png" width="571" height="349"></a></p>
<h2>Conclusion</h2>
<p>I essentially spent one evening writing the data collection script, and another night building the web page that rendered the chart. I&#8217;ve proven to myself that the idea was sound, if not the data source. I will continue to poke around to see if I can find a more reliable API for travel times, but otherwise consider this project &#8220;done.&#8221; In years past, getting to this point would have meant a <em>lot</em> more effort on my part. It is awesome how much of the hard work was done by the people and companies that support a great developer ecosystem.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=EnPDzlpZS2E:edz0zIVsOYI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=EnPDzlpZS2E:edz0zIVsOYI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=EnPDzlpZS2E:edz0zIVsOYI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=EnPDzlpZS2E:edz0zIVsOYI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=EnPDzlpZS2E:edz0zIVsOYI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=EnPDzlpZS2E:edz0zIVsOYI:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/EnPDzlpZS2E" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/joshuaflanagan/2012/01/31/great-time-to-be-a-developer/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/10adb7caa4eff9c02d244172bc8db6df?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=PG" medium="image">
            <media:title type="html">Joshua Flanagan</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/joshuaflanagan/2012/01/31/great-time-to-be-a-developer/</feedburner:origLink></item>
	<item>
		<title>Cleaning Out Git Remotes The Easy Way</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/cuTeetr7LKo/</link>
		<comments>http://lostechies.com/derickbailey/2012/01/31/cleaning-out-git-remotes-the-easy-way/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 20:53:17 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Git]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/derickbailey/?p=791]]></guid>
		<description><![CDATA[I had a rather large number of remote repositories set up in my Backbone.ModelBinding repository on my box, due to the wonderful community of contributors. But it was time for me to clean out the remotes as I no longer&#160;&#8230; <a href="http://lostechies.com/derickbailey/2012/01/31/cleaning-out-git-remotes-the-easy-way/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>I had a rather large number of remote repositories set up in my Backbone.ModelBinding repository on my box, due to the wonderful community of contributors. But it was time for me to clean out the remotes as I no longer needed any of the old ones. To make it easy, I used a simple bash script:</p>
<blockquote>
<p>git remote | while read a; do<br />&gt; git remote rm $a<br />&gt; done</p>
</blockquote>
<p>This took care of removing every single remote that I had in my &#8216;git remote&#8217; list … including the &#8220;origin&#8221; that I actually did want, now that I think about it. :P</p>
<p>But my bash-scripting-fu is terrible, so I couldn&#8217;t figure out how to not destroy all of them. I tried adding an if statement in there to check if it&#8217;s not &#8220;origin&#8221;, but couldn&#8217;t get it to work. Anyone got some better bash-chops than me, and can point out how to do this?</p>
<p> </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=cuTeetr7LKo:fTgmVg_eDuY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=cuTeetr7LKo:fTgmVg_eDuY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=cuTeetr7LKo:fTgmVg_eDuY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=cuTeetr7LKo:fTgmVg_eDuY:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=cuTeetr7LKo:fTgmVg_eDuY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=cuTeetr7LKo:fTgmVg_eDuY:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/cuTeetr7LKo" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/derickbailey/2012/01/31/cleaning-out-git-remotes-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/e592bd1326b1e80188ed4c0bf26b9f75?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Derick Bailey</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/derickbailey/2012/01/31/cleaning-out-git-remotes-the-easy-way/</feedburner:origLink></item>
	<item>
		<title>Multiple messages and transport messages in NServiceBus</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/RbWF8MyhB9s/</link>
		<comments>http://lostechies.com/jimmybogard/2012/01/31/multiple-messages-and-transport-messages-in-nservicebus/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 14:11:59 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[NServiceBus]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/jimmybogard/2012/01/31/multiple-messages-and-transport-messages-in-nservicebus/]]></guid>
		<description><![CDATA[Andreas Öhlund posted recently on the concept of the “transport message” in NServiceBus. One of the mistakes I often see (and made myself) was misunderstanding the boundary of the unit of work NServiceBus applies to messages, especially around sending multiple&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/01/31/multiple-messages-and-transport-messages-in-nservicebus/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p><a href="http://andreasohlund.net/">Andreas Öhlund</a> posted recently on <a href="http://andreasohlund.net/2012/01/31/the-difference-between-messages-and-messages/">the concept of the “transport message”</a> in <a href="http://nservicebus.com/">NServiceBus</a>. One of the mistakes I often see (and made myself) was misunderstanding the boundary of the unit of work NServiceBus applies to messages, especially around sending multiple messages.</p>
<p>In many of our systems, we consume flat files from third party integration partners. We take these flat files and deserialize each line of the file into a distinct message, so we first tried to do something like this:</p>
<div id="gist-1710630" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">ProcessLineInFileMessage</span><span class="p">[]</span> <span class="n">messages</span> <span class="p">=</span> <span class="n">ConvertFileToMessages</span><span class="p">(</span><span class="n">file</span><span class="p">);</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="n">Bus</span><span class="p">.</span><span class="n">Send</span><span class="p">(</span><span class="n">messages</span><span class="p">);</span> <span class="c1">// Sends all logical messages in 1 transport message</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1710630/d52c2d1b28408f6fc0febf4b0cd58174d97e9c08/MultipleMessagesBad.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1710630#file_multiple_messages_bad.cs" style="float:right;margin-right:10px;color:#666">MultipleMessagesBad.cs</a>
            <a href="https://gist.github.com/1710630">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The problem we hit was that the unit of work boundary in NServiceBus is around the <em>transport</em> message, not the <em>logical</em> message. In a file of a million lines, that’s a million logical messages bundled together into one single transport message, and one transaction boundary! We had assumed that the overload for sending multiple messages was simply a helper method that encapsulated a “foreach”. Well, no, it doesn’t. All the messages are wrapped in an envelope known as a “transport message”, and it’s the transport message that defines the unit of work boundary (since that’s the physical message sitting in the queue).</p>
<p>Needless to say, we saw database connection timeouts pretty much immediately. Instead, we modified our use of the bus to instead send one logical message per physical transport message, with our friend the “foreach”:</p>
<div id="gist-1710651" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">ProcessLineInFileMessage</span><span class="p">[]</span> <span class="n">messages</span> <span class="p">=</span> <span class="n">ConvertFileToMessages</span><span class="p">(</span><span class="n">file</span><span class="p">);</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="k">foreach</span> <span class="p">(</span><span class="n">var</span> <span class="n">message</span> <span class="k">in</span> <span class="n">messages</span><span class="p">)</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Bus</span><span class="p">.</span><span class="n">Send</span><span class="p">(</span><span class="n">message</span><span class="p">);</span> <span class="c1">// Send one logical message at a time</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1710651/4a22a2a115cc0b8a35c1c8d529fe4b35f58e831c/MultipleMessagesGood.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1710651#file_multiple_messages_good.cs" style="float:right;margin-right:10px;color:#666">MultipleMessagesGood.cs</a>
            <a href="https://gist.github.com/1710651">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>So when would you use the overloads for sending multiple messages? I’m not sure, but I’ll update if I find out!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=RbWF8MyhB9s:GzAJvQN1AaA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=RbWF8MyhB9s:GzAJvQN1AaA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=RbWF8MyhB9s:GzAJvQN1AaA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=RbWF8MyhB9s:GzAJvQN1AaA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=RbWF8MyhB9s:GzAJvQN1AaA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=RbWF8MyhB9s:GzAJvQN1AaA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/RbWF8MyhB9s" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/jimmybogard/2012/01/31/multiple-messages-and-transport-messages-in-nservicebus/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/cc359c5ccf90d7a24b5976316797b5ec?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Jimmy Bogard</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/jimmybogard/2012/01/31/multiple-messages-and-transport-messages-in-nservicebus/</feedburner:origLink></item>
	<item>
		<title>posh-git on Herding Code</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/jZPd5h-65KI/</link>
		<comments>http://lostechies.com/keithdahlby/2012/01/29/posh-git-on-herding-code/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 04:16:14 +0000</pubDate>
		<dc:creator>Keith Dahlby</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[posh-git]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/keithdahlby/?p=97]]></guid>
		<description><![CDATA[Earlier this month I had the opportunity to record an episode of Herding Code with GitHubbers Paul Betts and Phil Haack on the state of Git for Windows: Herding Code 132. Topics included discussions on why Git hates developers and&#160;&#8230; <a href="http://lostechies.com/keithdahlby/2012/01/29/posh-git-on-herding-code/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>Earlier this month I had the opportunity to record an episode of <a href="http://herdingcode.com/">Herding Code</a> with GitHubbers <a href="http://blog.paulbetts.org/">Paul Betts</a> and <a href="http://haacked.com/">Phil Haack</a> on the state of Git for Windows: <a title="Herding Code 132: Phil Haack, Keith Dahlby and Paul Betts on Git for Windows developers" href="http://herdingcode.com/?p=384">Herding Code 132</a>. Topics included discussions on why Git hates developers and why Mercurial proponents&#8217; claim that Git doesn&#8217;t preserve history is &#8220;nonsense.&#8221; I think the show turned out pretty well, and we got some hints as to what we can expect from GitHub for Windows. Thanks to the Herding Code guys for having us on!</p>
<p>Speaking of <a href="https://github.com/dahlbyk/posh-git">posh-git</a>, there are a bunch of new goodies in my master branch including tab expansion for remote branches, support for the Nuget console, and easier integration with <a href="https://github.com/JeremySkinner/posh-hg">posh-hg</a>. I&#8217;ll post more details with the upcoming v0.4 release.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=jZPd5h-65KI:VUBrR3NHPS4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=jZPd5h-65KI:VUBrR3NHPS4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=jZPd5h-65KI:VUBrR3NHPS4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=jZPd5h-65KI:VUBrR3NHPS4:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=jZPd5h-65KI:VUBrR3NHPS4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=jZPd5h-65KI:VUBrR3NHPS4:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/jZPd5h-65KI" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/keithdahlby/2012/01/29/posh-git-on-herding-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/43a5676a300a54ffdc903e49a2db9060?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Keith Dahlby</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/keithdahlby/2012/01/29/posh-git-on-herding-code/</feedburner:origLink></item>
	<item>
		<title>Don’t Rely Solely On jQuery’s “keyup” Event</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/qnL75XL2A3Y/</link>
		<comments>http://lostechies.com/derickbailey/2012/01/27/dont-rely-solely-on-jquerys-keyup-event/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 14:09:05 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Test Automation]]></category>
		<category><![CDATA[Validation]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/derickbailey/?p=787]]></guid>
		<description><![CDATA[A few days ago I pushed some changes to the form validation up to my WatchMeCode website. I was trying to fix a scenario where a browser cache would have some of the data in the purchase form already filled&#160;&#8230; <a href="http://lostechies.com/derickbailey/2012/01/27/dont-rely-solely-on-jquerys-keyup-event/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>A few days ago I pushed some changes to the form validation up to my <a href="http://watchmecode.net">WatchMeCode</a> website. I was trying to fix a scenario where a browser cache would have some of the data in the purchase form already filled in. In that scenario, a potential customer would have to modify each field, even though they were already filled in, in order for my  Backbone code to see the data in the field.</p>
<p>To &#8220;fix&#8221; the problem, I switched my code from a &#8220;change&#8221; event to a &#8220;keyup&#8221; event for the text boxes … bad idea!</p>
<h2>Browser Auto-Fill</h2>
<p>Most (if not all) browsers have an auto-fill feature, these days. I use it in Chrome a lot. It saves me a few seconds here and there and generally makes it easier for me to fill in the same repetitious information across websites.</p>
<p><img title="Screen Shot 2012-01-27 at 7.53.37 AM.png" src="http://lostechies.com/derickbailey/files/2012/01/Screen-Shot-2012-01-27-at-7.53.37-AM.png" border="0" alt="Screen Shot 2012 01 27 at 7 53 37 AM" width="423" height="361" /></p>
<p>But there&#8217;s a problem with auto-fill. It doesn&#8217;t fire the &#8220;keyup&#8221; event. It fires the &#8220;change&#8221; event for the things it fills in, but not &#8220;keyup&#8221;. This resulted in the data being truncated when it populated my Backbone model.</p>
<p>In the above screenshot, since i had typed &#8220;deri&#8221; in to the email address, the email that is stored in the Backbone model would only be &#8220;deri&#8221; &#8211; and that&#8217;s obviously not a valid email address.</p>
<h2>Easy To Fix</h2>
<p>There are a number of very easy ways to fix this.</p>
<ul>
<li>Validate the email address format</li>
<li>Use a combination of &#8220;blur&#8221;, &#8220;change&#8221;, and &#8220;keyup&#8221; events</li>
<li>Delay reading the data until the user clicks the &#8220;Purchase&#8221; button</li>
<li>Use a KVO (key-value observer) plugin like my Backbone.ModelBinding and let it deal with that for you</li>
</ul>
<p>For the quick-fix to get my site working properly, I went with the combination of &#8220;change&#8221; and &#8220;keyup&#8221; events. I&#8217;ll be re-writing my purchase form sometime soon, and will likely delay reading the values until they click the &#8220;Purchase&#8221; button. I&#8217;m also going to put in better email address validation to make sure it at least has an @ and a . in it, and I&#8217;ll likely use the HTML5 &#8220;email&#8221; field and see how that will help me, if at all.</p>
<h2>Not So Easy To Automate Testing</h2>
<p>When I originally made the change to use the &#8220;keyup&#8221; event, I did all my usual testing &#8211; which did not include using auto-fill. All of my testing passed, so I pushed the site live.</p>
<p>How do you test for bugs like this in an automated way? Is there even an automated way to test a browser&#8217;s auto-fill? I&#8217;d like to avoid mistakes like this in the future, and some automated testing around it would certainly be nice.</p>
<h2>Lesson Learned: Don&#8217;t Rely Solely On &#8220;keyup&#8221;</h2>
<p>There certainly are some great things you can do with the &#8220;keyup&#8221; event &#8211; like autocomplete, for one &#8211; but it&#8217;s not a great idea to only use this when there&#8217;s a chance that the browser&#8217;s auto-fill will be used.</p>
<p>And unfortunately for me, 2 of my customers ran in to this problem before I got it fixed. It makes it very hard to email someone the download instructions for their order. One of them contacted me and I corrected the order. But the other purchaser has not yet contacted me, and I don&#8217;t have enough information to figure out who they are, so I can&#8217;t track them down. I hope this person realizes that they didn&#8217;t get their order, they contact me. My contact info is all over the site… so … hopefully … the lessons learned for running an e-commerce site are sometimes painful.</p>
<p> </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=qnL75XL2A3Y:VqS3Oev8PCU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=qnL75XL2A3Y:VqS3Oev8PCU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=qnL75XL2A3Y:VqS3Oev8PCU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=qnL75XL2A3Y:VqS3Oev8PCU:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=qnL75XL2A3Y:VqS3Oev8PCU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=qnL75XL2A3Y:VqS3Oev8PCU:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/qnL75XL2A3Y" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/derickbailey/2012/01/27/dont-rely-solely-on-jquerys-keyup-event/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/e592bd1326b1e80188ed4c0bf26b9f75?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Derick Bailey</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/derickbailey/2012/01/27/dont-rely-solely-on-jquerys-keyup-event/</feedburner:origLink></item>
	<item>
		<title>Modularity And Security In Composite JavaScript Apps</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/qu95KQaPUwk/</link>
		<comments>http://lostechies.com/derickbailey/2012/01/26/modularity-and-security-in-composite-javascript-apps/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 13:44:16 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Backbone.Marionette]]></category>
		<category><![CDATA[Composite Apps]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Security]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/derickbailey/?p=782]]></guid>
		<description><![CDATA[In one of my current apps for a client, I have an activity based security system that determines what the user is allowed to do. The trick to this system is that all of the authorization checks happen on the&#160;&#8230; <a href="http://lostechies.com/derickbailey/2012/01/26/modularity-and-security-in-composite-javascript-apps/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>In one of my current apps for a client, I have <a href="http://lostechies.com/derickbailey/2011/05/24/dont-do-role-based-authorization-checks-do-activity-based-checks/">an activity based security system</a> that determines what the user is allowed to do. The trick to this system is that all of the authorization checks happen on the server, but the functionality that is being secured runs on the client, in JavaScript.</p>
<p>This is a bit of a problem. If I send all of the JavaScript that is uses to run any part of the system to the browser, then it&#8217;s possible that a clever user could enable it and do things they aren&#8217;t supposed to do. Of course, when they send a request back to the server, the server will verify they can do what they requested and block it… but the user should not be able to even try to do things they aren&#8217;t authorized to do, in the first place.</p>
<p>The solution that I&#8217;ve come up with, at a very high &#8220;functional area of the application&#8221; level, is simply not to send JavaScript to the browser if the user is not allowed to use it. This keeps me from having to do extra security checks in the browser, keeps the download for the user smaller and generally makes the app appear snappier because there is less code to run. More importantly, though, it keeps the user from being able to try things they can&#8217;t do.</p>
<h2>Modules To The Rescue</h2>
<p>I&#8217;m facilitating this through the use of modules in my JavaScript &#8211; both &#8220;module&#8221; as in the JavaScript module pattern, and &#8220;module&#8221; as in a packaged functional area of an application.</p>
<p>It&#8217;s worth noting, though, that I&#8217;m not using AMD (asynchronous module definitions), RequireJS, or any other &#8220;module&#8221; framework for JavaScript. The debate about the &#8220;right way&#8221; to do modules can rage on all it wants. I&#8217;m content with simple immediate functions and logical groupings of files while the more intelligent and invested people in the JS community figure that all out.</p>
<p>Each of the functional areas of my application is built in it&#8217;s own set of files. Each area has multiple files that it is composed within, but the files are grouped together for easy identification of what makes up a module. I also have an HTML template for each module which provides the needed &lt;script&gt; tags to include the code for that module.</p>
<h2>Only Render What Is Needed</h2>
<p>When I need a functional area of my site to be sent down to the user, I tell my server side template language to include the correct HTML file. For example, I&#8217;m doing this in an ASP.NET MVC application:</p>
<div id="gist-1680805" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>@{var user = (CustomPrincipal)User;}</div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>@if (user.Can(&quot;locations/manage&quot;))</div><div class='line' id='LC4'>{</div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;@Html.LocationManagementScripts()</div><div class='line' id='LC6'>}</div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'>@if (user.Can(&quot;locations/search&quot;))</div><div class='line' id='LC9'>{</div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;@Html.LocationSearchScripts()</div><div class='line' id='LC11'>}</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1680805/10ca9fb5b64e7ddfa3503719503ff0bbad141af0/1.cshtml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1680805#file_1.cshtml" style="float:right;margin-right:10px;color:#666">1.cshtml</a>
            <a href="https://gist.github.com/1680805">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>In this code, I&#8217;m checking to see if the current user is allowed to manage locations. If they are, the extension method &#8220;LocationManagementScripts&#8221; is called. This in turn renders the &#8220;LocationManagementScript.html&#8221; file at this point in my HTML layout. That file contains all of the &lt;script&gt; tags for the location management JavaScript app. In the same way, I&#8217;m checking to see if the user can search through locations, and running the same basic process if they can.</p>
<h2>Self-Initializing Modules</h2>
<p>When a functional module is included after passing one of these checks, it needs a way to get itself spun up and started so that it can do it&#8217;s magic. It may need to render something on to the screen. It may need to register itself with the application&#8217;s <a href="http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/">event aggregator</a>, or any of a number of other things. This is where my <a href="https://github.com/derickbailey/backbone.marionette">Backbone.Marionette</a> add-on comes in to play for my Backbone apps.</p>
<p>Marionette has an explicit concept of an &#8220;initializer&#8221; tied to it&#8217;s Application objects. When you create an instance of an Application object, you can call &#8220;app.addInitializer&#8221; and pass a callback function. The callback function represents everything that your module needs to do, to get itself up and running. All of these initializer functions &#8211; no matter how many you add &#8211; get fired when you call &#8220;app.start()&#8221;.</p>
<div id="gist-1680805" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">myApp</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Marionette</span><span class="p">.</span><span class="nx">Application</span><span class="p">();</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="nx">myApp</span><span class="p">.</span><span class="nx">addInitializer</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">options</span><span class="p">){</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">myView</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MyView</span><span class="p">({</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">model</span><span class="o">:</span> <span class="nx">options</span><span class="p">.</span><span class="nx">someModel</span></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="nx">MyApp</span><span class="p">.</span><span class="nx">mainRegion</span><span class="p">.</span><span class="nx">show</span><span class="p">(</span><span class="nx">myView</span><span class="p">);</span></div><div class='line' id='LC8'><span class="p">});</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="nx">myApp</span><span class="p">.</span><span class="nx">addInitializer</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">options</span><span class="p">){</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="k">new</span> <span class="nx">MyRouter</span><span class="p">();</span></div><div class='line' id='LC12'>&nbsp;&nbsp;<span class="k">new</span> <span class="nx">SomeOtherView</span><span class="p">().</span><span class="nx">render</span><span class="p">();</span></div><div class='line' id='LC13'><span class="p">});</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'><span class="nx">myApp</span><span class="p">.</span><span class="nx">start</span><span class="p">();</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1680805/b56ae912ba7e6fd1546034081d2865ff623e9a4a/2.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1680805#file_2.js" style="float:right;margin-right:10px;color:#666">2.js</a>
            <a href="https://gist.github.com/1680805">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Each functional area of my application has it&#8217;s own initializer function. When a functional area has been included in the rendered &lt;script&gt; tags, the initializer gets added and when the &#8220;start&#8221; method is called, the modules for that functional area are fired up and they do there thing.</p>
<h2>A Composite App, And Sub-Apps</h2>
<p>One of the tricks to making all of this work, is that I need to have a primary &#8220;app&#8221; object that all of my modules know about. In the above example, the &#8220;myApp&#8221; object is this. Each of the modules for each of the functional areas has direct knowledge of this object and can call public APIs on it &#8211; including the &#8220;addInitializer&#8221; method.</p>
<p>A better example of what a module definition and initializer might look like, would be this:</p>
<div id="gist-1680805" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">// --------------------</span></div><div class='line' id='LC2'><span class="c1">// app.js</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="nx">myApp</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Marionette</span><span class="p">.</span><span class="nx">Application</span><span class="p">();</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="nx">myApp</span><span class="p">.</span><span class="nx">addRegions</span><span class="p">({</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="nx">mainRegion</span><span class="o">:</span> <span class="s2">&quot;#main&quot;</span></div><div class='line' id='LC8'><span class="p">});</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="c1">// --------------------</span></div><div class='line' id='LC12'><span class="c1">// locationSearch.js</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">myApp</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">){</span></div><div class='line' id='LC15'>&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">SearchView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// ...</span></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>&nbsp;&nbsp;<span class="nx">myApp</span><span class="p">.</span><span class="nx">addRegions</span><span class="p">({</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">searchRegion</span><span class="o">:</span> <span class="s2">&quot;#search&quot;</span></div><div class='line' id='LC21'>&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC22'><br/></div><div class='line' id='LC23'>&nbsp;&nbsp;<span class="nx">myApp</span><span class="p">.</span><span class="nx">addInitializer</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">SearchView</span><span class="p">();</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">view</span><span class="p">.</span><span class="nx">render</span><span class="p">();</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">myApp</span><span class="p">.</span><span class="nx">searchRegion</span><span class="p">.</span><span class="nx">show</span><span class="p">(</span><span class="nx">view</span><span class="p">);</span></div><div class='line' id='LC27'>&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC28'><span class="p">})(</span><span class="nx">myApp</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">);</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'><br/></div><div class='line' id='LC31'><span class="c1">// --------------------</span></div><div class='line' id='LC32'><span class="c1">// index.html</span></div><div class='line' id='LC33'><br/></div><div class='line' id='LC34'><span class="o">&lt;</span><span class="nx">script</span> <span class="nx">language</span><span class="o">=</span><span class="s2">&quot;javascript&quot;</span><span class="o">&gt;</span></div><div class='line' id='LC35'>&nbsp;&nbsp;<span class="nx">myApp</span><span class="p">.</span><span class="nx">start</span><span class="p">();</span></div><div class='line' id='LC36'><span class="o">&lt;</span><span class="err">/script&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1680805/e61b4b20a7a216a4d8c9d6bfd95848edf35dff73/3.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1680805#file_3.js" style="float:right;margin-right:10px;color:#666">3.js</a>
            <a href="https://gist.github.com/1680805">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>In this example, I&#8217;m using the simple JavaScript module pattern to encapsulate my search functionality. I&#8217;m also providing an initializer for the module that instantiates a search view and shows it to the user using a <a href="http://lostechies.com/derickbailey/2011/12/12/composite-js-apps-regions-and-region-managers/">region manager</a>.</p>
<p>Each of these functional areas is basically a sub-application. Many sub-applications are used to compose a larger application and overall experience for the user. The composition of a larger application through various modules that are included / excluded based on some criteria are what really make this a composite application.</p>
<p>I also included the final call to &#8220;myApp.start()&#8221;, showing that I do this from my main HTML page and not from my JavaScript files. This provides a single point of entry for all of the registered modules, no matter which modules are registered. The &#8220;myApp&#8221; object really doesn&#8217;t care which modules are registered, honestly. It doesn&#8217;t need to care. It only needs to execute the initializers that happen to be present. If none are present because the user didn&#8217;t have permission to do anything, then nothing happens when this method is called and the user won&#8217;t see anything.</p>
<h2>Security: Don&#8217;t Let Them See It If They Can&#8217;t Do It</h2>
<p>If the security check to see if the user is allowed to use the location search feature fails, the rendered HTML won&#8217;t include the &lt;script&gt; tags for the &#8220;locationSearch.js&#8221; file. If this file is not sent down to the browser, then it will never register itself. If a module has not registered itself for initialization, it&#8217;s views won&#8217;t show up on the screen and the user won&#8217;t be able to try and use the feature. Further, the user won&#8217;t be able to &#8220;view source&#8221; on the page and find any stray JavaScript that they shouldn&#8217;t be able to use.</p>
<h2>It&#8217;s Not Always That Easy</h2>
<p>Of course there are other security concerns that are not this simple. When a functional area is closed off by authorization, it&#8217;s easy to keep things clean like this. We can compose the application at run time simply by including the right files and letting the code in those files register themselves for initialization. But when we have a functional area of the system that has finer grained authorization and permissions associated with it, things get a little more tricky.</p>
<p>I&#8217;m still learning and exploring this space. I have some ideas and am going to be implementing some of them soon. If anyone out there has any experience in handling finer grained security needs in JavaScript apps, I&#8217;d love to hear about it. Post links to your favorite resources for this, in the comments.</p>
<p> </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=qu95KQaPUwk:RAcw-0evDaE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=qu95KQaPUwk:RAcw-0evDaE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=qu95KQaPUwk:RAcw-0evDaE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=qu95KQaPUwk:RAcw-0evDaE:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=qu95KQaPUwk:RAcw-0evDaE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=qu95KQaPUwk:RAcw-0evDaE:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/qu95KQaPUwk" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/derickbailey/2012/01/26/modularity-and-security-in-composite-javascript-apps/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/e592bd1326b1e80188ed4c0bf26b9f75?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Derick Bailey</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/derickbailey/2012/01/26/modularity-and-security-in-composite-javascript-apps/</feedburner:origLink></item>
	<item>
		<title>Rant: That’s Not Rest.</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/PPJ7T6xec-U/</link>
		<comments>http://lostechies.com/derickbailey/2012/01/25/rant-thats-not-rest/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 13:22:34 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[REST]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/derickbailey/?p=770]]></guid>
		<description><![CDATA[I&#8217;ve asked for an explanation, definition, blog post, book, or any other source of material that can get me up to speed on REST in web development a number of times. It&#8217;s a popular subject these days, and there seems&#160;&#8230; <a href="http://lostechies.com/derickbailey/2012/01/25/rant-thats-not-rest/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>I&#8217;ve asked for an explanation, definition, blog post, book, or any other source of material that can get me up to speed on <a href="http://en.wikipedia.org/wiki/Representational_state_transfer">REST in web development</a> a number of times. It&#8217;s a popular subject these days, and there seems to be an abundance of knowledge and information out there.</p>
<p>Except that it&#8217;s all wrong. Every last bit of it.</p>
<p>… or, at least the handful of people who inevitably respond with comments like &#8220;no one does REST correctly&#8221; and &#8220;that&#8217;s not REST&#8221; would have me believe.</p>
<h2>Your Doing It Wrong</h2>
<p>Frankly, I&#8217;m tired of this pedantic, obnoxious response. The same people that continuously say &#8220;no one does it right&#8221; can&#8217;t seem to tell me why it&#8217;s wrong or point me to a resource that does do it right.</p>
<p><img title="ThatsNotREST.jpg" src="http://lostechies.com/derickbailey/files/2012/01/ThatsNotREST.jpg" border="0" alt="ThatsNotREST" width="500" height="503" /></p>
<p>Here&#8217;s the deal. If everyone does it wrong, then write a blog post or a book or a screencast or something that tells the world how to do it right. Until you can point people to what REST actually is, stop telling everyone that no one does REST right.</p>
<h2>My Journey Into Being Wrong</h2>
<p>I have work to do. I&#8217;m obviously going to do it wrong, but frankly, I don&#8217;t care at this point. My system will work just fine, just like every other &#8220;That&#8217;s not REST&#8221; system out there.</p>
<p>To help me learn how to be wrong, though, here are a few resources that I&#8217;m going to be checking out:</p>
<p><a href="http://getsomere.st/">Get Some REST</a>: A work in progress book from Steve Klabnik. I signed up for the mailing list and am eagerly waiting. There&#8217;s rumor that Steve might actually be doing it right.</p>
<p><a href="http://www.amazon.com/Restful-Web-Services-Leonard-Richardson/dp/0596529260">RESTFul Web Services</a> &#8211; I bought the Kindle version last night. It&#8217;s the book DHH says everyone should read and a few others also suggested it.</p>
<p><a href="http://www.amazon.com/REST-Practice-Hypermedia-Systems-Architecture/dp/0596805829">REST In Practice</a> &#8211; I&#8217;m probably going to buy this one, too. It seems to have the &#8220;30,000 foot view&#8221; that I&#8217;m also looking for, according to one reviewer on Amazon.</p>
<p>If anyone else has any resources for how I can learn non-REST, RESTful best practices, I&#8217;d love to know.</p>
<p>(end of rant)</p>
<p> </p>
<p><span style="font-size: 18px; font-weight: bold;">… Updates</span></p>
<p>Thanks to everyone who is responding in the comments here and via twitter. I&#8217;m going to update this post with the resources that people are pointing me to, from Twitter.</p>
<p>The majority of my experience with anything REST-like comes from working with Ruby on Rails, and mimicking it&#8217;s handling of HTTP verbs with Sinatra application. I know this isn&#8217;t anything near correct, which is why I&#8217;m looking to find good resources.</p>
<p>I&#8217;ve been getting a lot of &#8220;you don&#8217;t need to do pure REST&#8221; responses &#8211; which I agree with. One of my problems, though, is that I&#8217;m having a hard time figuring out what &#8220;pure&#8221; REST is so that I can know the difference between that and what I&#8217;m actually doing.</p>
<p><a href="https://twitter.com/#!/serialseb/status/162172630844186624">Sebastian Lambla points out</a> that it&#8217;s not the entire REST community that pulls this crap. There are plenty of people that are helping, providing good information and creating good resources to throw at people like me that are wanting to learn. My problem is not with the REST community as a whole, but with the people that behave in the way I&#8217;ve pointed out, here.</p>
<p>The most consistent advice I&#8217;m seeing so far, is to start with the books I&#8217;ve listed / purchases already. That will at least get me heading down a better direction.</p>
<h2>Resources From The Twitterverse</h2>
<p><a href="http://www.amazon.com/RESTful-Web-Services-Cookbook-Scalability/dp/0596801688">http://www.amazon.com/RESTful-Web-Services-Cookbook-Scalability/dp/0596801688</a></p>
<p><a href="http://code.google.com/p/implementing-rest/">http://code.google.com/p/implementing-rest/</a></p>
<p><a href="http://www.amazon.com/REST-Design-Rulebook-Mark-Masse/dp/1449310508">http://www.amazon.com/REST-Design-Rulebook-Mark-Masse/dp/1449310508</a></p>
<p>The #rest channel on <a href="http://freenode.net/">http://freenode.net/</a> IRC</p>
<p><a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm">http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm</a></p>
<p><a href="http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven">http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven</a></p>
<p><a href="http://amundsen.com/blog/">http://amundsen.com</a></p>
<p><a href="http://www.infoq.com/search.action?queryString=stefan+tilkov+rest&amp;searchOrder=relevance&amp;search=stefan+tilkov+rest">http://www.infoq.com/search.action?queryString=stefan+tilkov+rest&amp;searchOrder=relevance&amp;search=stefan+tilkov+rest</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=PPJ7T6xec-U:AyWUvRIHoS0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=PPJ7T6xec-U:AyWUvRIHoS0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=PPJ7T6xec-U:AyWUvRIHoS0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=PPJ7T6xec-U:AyWUvRIHoS0:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=PPJ7T6xec-U:AyWUvRIHoS0:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=PPJ7T6xec-U:AyWUvRIHoS0:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/PPJ7T6xec-U" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/derickbailey/2012/01/25/rant-thats-not-rest/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/e592bd1326b1e80188ed4c0bf26b9f75?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Derick Bailey</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/derickbailey/2012/01/25/rant-thats-not-rest/</feedburner:origLink></item>
	<item>
		<title>Speaking at San Diego DNUG tonight</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/iQWRqZ1vNuk/</link>
		<comments>http://lostechies.com/jimmybogard/2012/01/24/speaking-at-san-diego-dnug-tonight/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 17:01:48 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[Community]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/jimmybogard/2012/01/24/speaking-at-san-diego-dnug-tonight/]]></guid>
		<description><![CDATA[If you’re in the San Diego area tonight, I’ll be giving my talk on domain modeling. Details below: http://www.sandiegodotnet.com/ I’ve been told that there is free pizza. If not, I might be able score some stale bagels from my hotel’s&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/01/24/speaking-at-san-diego-dnug-tonight/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>If you’re in the San Diego area tonight, I’ll be giving my talk on domain modeling. Details below:</p>
<p><a href="http://www.sandiegodotnet.com/">http://www.sandiegodotnet.com/</a></p>
<p>I’ve been told that there is free pizza. If not, I might be able score some stale bagels from my hotel’s lobby, but no promises there.</p>
<p>Hope to see you there!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=iQWRqZ1vNuk:evRFrsBsAgw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=iQWRqZ1vNuk:evRFrsBsAgw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=iQWRqZ1vNuk:evRFrsBsAgw:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=iQWRqZ1vNuk:evRFrsBsAgw:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=iQWRqZ1vNuk:evRFrsBsAgw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=iQWRqZ1vNuk:evRFrsBsAgw:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/iQWRqZ1vNuk" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/jimmybogard/2012/01/24/speaking-at-san-diego-dnug-tonight/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/cc359c5ccf90d7a24b5976316797b5ec?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Jimmy Bogard</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/jimmybogard/2012/01/24/speaking-at-san-diego-dnug-tonight/</feedburner:origLink></item>
	<item>
		<title>Some Thoughts On Functional JavaScript</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/ocqOgyYF3Fg/</link>
		<comments>http://lostechies.com/derickbailey/2012/01/24/some-thoughts-on-functional-javascript/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 13:29:08 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[DSL]]></category>
		<category><![CDATA[Functional]]></category>
		<category><![CDATA[Javascript]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/derickbailey/?p=758]]></guid>
		<description><![CDATA[Just for fun I decided to put together a quick sample of some functional JavaScript &#8211; as in, functional programming done with JavaScript. I&#8217;m not really very familiar with functional languages other than playing with Haskell a bit and doing&#160;&#8230; <a href="http://lostechies.com/derickbailey/2012/01/24/some-thoughts-on-functional-javascript/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>Just for fun I decided to put together a quick sample of some functional JavaScript &#8211; as in, <a href="http://en.wikipedia.org/wiki/Functional_programming">functional programming</a> done with JavaScript. I&#8217;m not really very familiar with functional languages other than playing with Haskell a bit and doing some functional-style stuff in C# and Ruby. I wanted to see what a functional JavaScript code base might look like.</p>
<h2>A Functional Calculator</h2>
<p>To keep things simple, I created a calculator using nothing but functions. Each function takes other functions as parameters and returns a function. The only exception to this is the simple `value` function which takes a value and returns a function that itself will return the original value.</p>
<p>Here&#8217;s the basic code I wrote for my calculator, including the aforementioned `value` function:</p>
<div id="gist-1660207" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">// A raw value, represented as a function</span></div><div class='line' id='LC2'><span class="kd">var</span> <span class="nx">value</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">val</span><span class="p">){</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="k">return</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">val</span><span class="p">;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC6'><span class="p">}</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="c1">// A function to add two functions together</span></div><div class='line' id='LC9'><span class="kd">var</span> <span class="nx">add</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">){</span></div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="k">return</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">x</span><span class="p">()</span> <span class="o">+</span> <span class="nx">y</span><span class="p">();</span></div><div class='line' id='LC12'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC13'><span class="p">}</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'><span class="c1">// A function to subtract one function from another</span></div><div class='line' id='LC16'><span class="kd">var</span> <span class="nx">subtract</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">){</span></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="k">return</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">x</span><span class="p">()</span> <span class="o">-</span> <span class="nx">y</span><span class="p">();</span></div><div class='line' id='LC19'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC20'><span class="p">}</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'><span class="c1">// A function to multiply two functions</span></div><div class='line' id='LC23'><span class="kd">var</span> <span class="nx">multiply</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">){</span></div><div class='line' id='LC24'>&nbsp;&nbsp;<span class="k">return</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">x</span><span class="p">()</span> <span class="o">*</span> <span class="nx">y</span><span class="p">();</span></div><div class='line' id='LC26'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC27'><span class="p">}</span></div><div class='line' id='LC28'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1660207/96c7c067d3686ea9d97b75bfe931066cc2525499/funcalc.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1660207#file_funcalc.js" style="float:right;margin-right:10px;color:#666">funcalc.js</a>
            <a href="https://gist.github.com/1660207">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>I wrote all this through the use of some Jasmine tests, but as a quick example of how it can work, here&#8217;s a simple formula and result:</p>
<div id="gist-1660207" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="nx">one</span> <span class="o">=</span> <span class="nx">value</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span></div><div class='line' id='LC2'><span class="kd">var</span> <span class="nx">two</span> <span class="o">=</span> <span class="nx">value</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span></div><div class='line' id='LC3'><span class="kd">var</span> <span class="nx">three</span> <span class="o">=</span> <span class="nx">value</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span></div><div class='line' id='LC4'><span class="kd">var</span> <span class="nx">four</span> <span class="o">=</span> <span class="nx">value</span><span class="p">(</span><span class="mi">4</span><span class="p">);</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="c1">// (1 + 2) * 3 - 4 == 5</span></div><div class='line' id='LC7'><span class="kd">var</span> <span class="nx">resultFunc</span> <span class="o">=</span> <span class="nx">subtract</span><span class="p">(</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">add</span><span class="p">(</span><span class="nx">one</span><span class="p">,</span> <span class="nx">two</span><span class="p">),</span> <span class="nx">three</span><span class="p">),</span> <span class="nx">four</span><span class="p">);</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'><span class="c1">// but it&#39;s still a function right now, so evaluate it</span></div><div class='line' id='LC10'><span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="nx">resultFunc</span><span class="p">();</span></div><div class='line' id='LC11'><span class="nx">consol</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">result</span><span class="p">);</span> <span class="c1">// =&gt; 5</span></div><div class='line' id='LC12'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1660207/f6a3d87f8146b869d42b3436a288ca1746017c1e/sample.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1660207#file_sample.js" style="float:right;margin-right:10px;color:#666">sample.js</a>
            <a href="https://gist.github.com/1660207">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>My Thoughts And Reactions</h2>
<p>These are the interesting points that stand out in my head. I&#8217;m not sure if these are &#8220;correct&#8221; or not… it&#8217;s just what pops out in my mind.</p>
<p>If anyone has any corrections and additional thoughts or notes, I&#8217;d love to hear them &#8211; just drop me a line in the comments, below.</p>
<h4>Functions And Functions As State</h4>
<p>I&#8217;ve represented everything as a function &#8211; even the simple values of 1 through 4. And every function returns a function, except for the simple values, which return a function that returns the actual value.</p>
<p><img title="FunctionAllTheThings.jpg" src="http://lostechies.com/derickbailey/files/2012/01/FunctionAllTheThings.jpg" border="0" alt="FunctionAllTheThings" width="400" height="300" /></p>
<p>In the process of returning functions from functions, I&#8217;m representing the state of the application as a series of functions.</p>
<p>This one is a bit odd for me to wrap my head around. I&#8217;m not sure if this is really correct, or if it&#8217;s just an oversimplification of what&#8217;s really happening here. There are no data-structures, though, only functions. So it stands to reason that the state is stored as a series of functions that can be evaluated to produce a result.</p>
<p><strong>Update</strong>: The Wikipedia article on <a href="http://en.wikipedia.org/wiki/Functional_programming">functional programming</a> says that fp &#8220;avoids state&#8221;. So… am I wrong in thinking that I&#8217;ve represented the state with functions instead of data structures?</p>
<h4>Funcional Command Pattern</h4>
<p>The result isn&#8217;t actually evaluated until I invoke the`resultFunc` in the above example. But I have the `resultFunc`available as a variable and I can pass it around anywhere I want. This basically turns it into a command pattern implementation, only in a functional manner instead of an object manner.</p>
<h4>Compose-able And DSL Functions</h4>
<p>All of these functions are side effect free. None of them actually mutates any existing application state. Of course this is a really simple implementation, but that was one of my goals. I&#8217;ll need to play with some more complex ideas in order to really see how side effect free would play out.</p>
<p>One of the benefits of side effect free functions is that the code in the sample is far more declarative and compose-able than most code I write. Since each function does one very small and very simple thing, taking in other functions as parameters, it&#8217;s easy to build things.</p>
<div id="gist-1660207" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="nx">one</span> <span class="o">=</span> <span class="nx">value</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span></div><div class='line' id='LC2'><span class="kd">var</span> <span class="nx">two</span> <span class="o">=</span> <span class="nx">value</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span></div><div class='line' id='LC3'><span class="kd">var</span> <span class="nx">three</span> <span class="o">=</span> <span class="nx">value</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span></div><div class='line' id='LC4'><span class="kd">var</span> <span class="nx">four</span> <span class="o">=</span> <span class="nx">value</span><span class="p">(</span><span class="mi">4</span><span class="p">);</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="c1">// (1 + 2) * 3 - 4 == 5</span></div><div class='line' id='LC7'><span class="kd">var</span> <span class="nx">added</span> <span class="o">=</span> <span class="nx">add</span><span class="p">(</span><span class="nx">one</span><span class="p">,</span> <span class="nx">two</span><span class="p">)</span></div><div class='line' id='LC8'><span class="kd">var</span> <span class="nx">multiplied</span> <span class="o">=</span> <span class="nx">multiply</span><span class="p">(</span><span class="nx">added</span><span class="p">,</span> <span class="nx">three</span><span class="p">)</span></div><div class='line' id='LC9'><span class="kd">var</span> <span class="nx">resultFunc</span> <span class="o">=</span> <span class="nx">subtract</span><span class="p">(</span><span class="nx">multiplied</span><span class="p">,</span> <span class="nx">four</span><span class="p">);</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="c1">// the result is the same. </span></div><div class='line' id='LC12'><span class="c1">// it&#39;s been composed the same, functionally</span></div><div class='line' id='LC13'><span class="c1">// it was just put together with more variables in the mix</span></div><div class='line' id='LC14'><span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="nx">resultFunc</span><span class="p">();</span></div><div class='line' id='LC15'><span class="nx">consol</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">result</span><span class="p">);</span> <span class="c1">// =&gt; 5</span></div><div class='line' id='LC16'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1660207/57d657c16e82fd1f3de01449a9f42bc3b1ac5a57/compose.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1660207#file_compose.js" style="float:right;margin-right:10px;color:#666">compose.js</a>
            <a href="https://gist.github.com/1660207">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This is something that I&#8217;ve played with in other languages, and I like this aspect of functional programming. It&#8217;s a great way to build DSLs for example.</p>
<h4>Compared To Object / Prototypal</h4>
<p>Oscar Godson posted <a href="http://jsbin.com/atopek/edit#javascript">a version of my code alongside a prototypal version</a> for a comparison.</p>
<div id="gist-1660207" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cm">/**</span></div><div class='line' id='LC2'><span class="cm"> Prototypal</span></div><div class='line' id='LC3'><span class="cm"> from Oscar Godson: http://jsbin.com/atopek/edit#javascript</span></div><div class='line' id='LC4'><span class="cm">*/</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="kd">var</span> <span class="nx">Calc</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="c1">//could be a param</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="k">return</span> <span class="k">this</span><span class="p">;</span></div><div class='line' id='LC9'><span class="p">}</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="nx">Calc</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">add</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">x</span><span class="p">){</span></div><div class='line' id='LC12'>&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">sum</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">sum</span> <span class="o">+</span> <span class="nx">x</span><span class="p">;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;<span class="k">return</span> <span class="k">this</span><span class="p">;</span></div><div class='line' id='LC14'><span class="p">}</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'><span class="nx">Calc</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">multiply</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">x</span><span class="p">){</span></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">sum</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">sum</span> <span class="o">*</span> <span class="nx">x</span></div><div class='line' id='LC18'>&nbsp;&nbsp;<span class="k">return</span> <span class="k">this</span><span class="p">;</span></div><div class='line' id='LC19'><span class="p">}</span></div><div class='line' id='LC20'>&nbsp;&nbsp;</div><div class='line' id='LC21'><span class="nx">Calc</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">subtract</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">x</span><span class="p">){</span></div><div class='line' id='LC22'>&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">sum</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">sum</span> <span class="o">-</span> <span class="nx">x</span><span class="p">;</span></div><div class='line' id='LC23'>&nbsp;&nbsp;<span class="k">return</span> <span class="k">this</span></div><div class='line' id='LC24'><span class="p">}</span></div><div class='line' id='LC25'><br/></div><div class='line' id='LC26'><span class="nx">Calc</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">result</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC27'>&nbsp;&nbsp;<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">sum</span><span class="p">;</span></div><div class='line' id='LC28'><span class="p">}</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="k">new</span> <span class="nx">Calc</span><span class="p">().</span><span class="nx">add</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="nx">add</span><span class="p">(</span><span class="mi">2</span><span class="p">).</span><span class="nx">multiply</span><span class="p">(</span><span class="mi">3</span><span class="p">).</span><span class="nx">subtract</span><span class="p">(</span><span class="mi">4</span><span class="p">).</span><span class="nx">result</span><span class="p">())</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1660207/02afccd0e98abeb50dd74ce0d48e17223dad8cfc/prototypal.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1660207#file_prototypal.js" style="float:right;margin-right:10px;color:#666">prototypal.js</a>
            <a href="https://gist.github.com/1660207">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>I like seeing them side by side. It&#8217;s interesting the see all of the different ways that JavaScript can do the same thing. It also shows me just how ugly I think the functional version is. When you compare the actual formula in the comments to the functional JS code, the code is inside-out-backwards. The prototypal code with it&#8217;s method chaining is closer to the formula, though it&#8217;s still a bit off.</p>
<p>I think it&#8217;s easier to read and understand the chaining, though. It also reminds me of LINQ and monads &#8211; pipes and filters with a single result coming from the end of it.</p>
<h4>More Fun Functional Stuff To Try</h4>
<p>I haven&#8217;t done anything like memoizing or currying for these simple examples, but those are ideas I want to play with, too. I&#8217;ve written a currying function in JavaScript once or twice, and there&#8217;s plenty of examples in the good JS books. Memoizing is also available in Underscore.js but should be trivial to implement a naive version of it. I&#8217;m sure there are plenty of other things that would be fun to try, as well.</p>
<p><span style="font-size: 18px; font-weight: bold;">Code And Continuing Thoughts (Maybe)</span></p>
<p>If you&#8217;re interested in seeing this code, <a href="https://github.com/derickbailey/jsfuncalc">it&#8217;s on Github</a>.</p>
<p>I&#8217;m thinking about taking this a little further over time, just for fun. I&#8217;ve posted these comments in the read me (actually &#8211; they started there…) and I&#8217;ll continue to update the read me as I progress (assuming I actually do continue exploring this and don&#8217;t just forget about it like so many other things).</p>
<p>This is really pretty simple stuff&#8230; but it&#8217;s fun to write asimple exercise like this and think about what&#8217;s going on, why and see how it affects the way I think and approach software development.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=ocqOgyYF3Fg:ks_fJKFwXSI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=ocqOgyYF3Fg:ks_fJKFwXSI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=ocqOgyYF3Fg:ks_fJKFwXSI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=ocqOgyYF3Fg:ks_fJKFwXSI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=ocqOgyYF3Fg:ks_fJKFwXSI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=ocqOgyYF3Fg:ks_fJKFwXSI:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/ocqOgyYF3Fg" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/derickbailey/2012/01/24/some-thoughts-on-functional-javascript/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/e592bd1326b1e80188ed4c0bf26b9f75?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Derick Bailey</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/derickbailey/2012/01/24/some-thoughts-on-functional-javascript/</feedburner:origLink></item>
	<item>
		<title>CodeMash 2012 wrap up</title>
		<link>http://feedproxy.google.com/~r/LosTechies/~3/BPMqe4LEiLo/</link>
		<comments>http://lostechies.com/jimmybogard/2012/01/24/codemash-2012-wrap-up/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 05:02:43 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[Community]]></category>
        
		<guid isPermaLink="false"><![CDATA[http://lostechies.com/jimmybogard/2012/01/24/codemash-2012-wrap-up/]]></guid>
		<description><![CDATA[This year was my first to attend the bacon debauchery that is CodeMash. I had been suggested to go by pretty much everyone that I’ve met that has gone, and this year I was fortunate enough to be selected as&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/01/24/codemash-2012-wrap-up/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
		<content:encoded><![CDATA[<p>This year was my first to attend the bacon debauchery that is <a href="http://codemash.org/">CodeMash</a>. I had been suggested to go by pretty much everyone that I’ve met that has gone, and this year I was fortunate enough to be selected as a speaker.</p>
<p>My talk was on “Crafting Wicked Domain Models” that while was not recorded, all the slides and code can be found on my GitHub:</p>
<p><a href="https://github.com/jbogard/presentations">https://github.com/jbogard/presentations</a></p>
<p>Although that event wasn’t recorded, check out Claudio Lassala’s blog, where he <a href="http://lassala.net/2011/08/25/videos-from-houston-code-camp-2011/">recorded me doing the talk at the Houston Code Camp</a> a couple months before.</p>
<p>A couple of folks came up to me afterwards telling me that they could never code/refactor in front of a Live Studio Audience. But, since the example was straight out of a real project I had lived through, it made going through the code a lot easier.</p>
<p>The questions afterwards were great, too. I always get some discussion around “it’s great and all, but now I have a bunch more classes to deal with”. It’s a fair criticism, and one to keep an eye out for. The way I see it, if the code is more understandable and more representative of real-world concepts, it’s a win. If, however, I’m having a hard time thinking of the names of classes for which I’m building responsibilities around, it’s a bit of a smell that I’m just inventing abstractions.</p>
<p>CodeMash really was a blast. The people were fantastic, the location was great, the food was fantastic, the beer and bacon were plentiful, and as always, the conversations were unforgettable. Hope to see everyone there next year!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LosTechies?a=BPMqe4LEiLo:-uHwBvNH6Ck:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=BPMqe4LEiLo:-uHwBvNH6Ck:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=BPMqe4LEiLo:-uHwBvNH6Ck:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=BPMqe4LEiLo:-uHwBvNH6Ck:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/LosTechies?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LosTechies?a=BPMqe4LEiLo:-uHwBvNH6Ck:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LosTechies?i=BPMqe4LEiLo:-uHwBvNH6Ck:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LosTechies/~4/BPMqe4LEiLo" height="1" width="1"/>]]></content:encoded>
		<wfw:commentRss>http://lostechies.com/jimmybogard/2012/01/24/codemash-2012-wrap-up/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
            <media:content url="http://www.gravatar.com/avatar/cc359c5ccf90d7a24b5976316797b5ec?s=96&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G" medium="image">
            <media:title type="html">Jimmy Bogard</media:title>
        </media:content>
    		<feedburner:origLink>http://lostechies.com/jimmybogard/2012/01/24/codemash-2012-wrap-up/</feedburner:origLink></item>
</channel>
</rss><!-- 244 queries 1.818 seconds. --><!-- Dynamic page generated in 1.818 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-02-11 08:46:42 --><!-- Compression = gzip -->

