<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Thomas Mayfield]]></title>
  <link href="http://zen-hacking.com/atom.xml" rel="self"/>
  <link href="http://zen-hacking.com/"/>
  <updated>2012-04-29T23:27:36-04:00</updated>
  <id>http://zen-hacking.com/</id>
  <author>
    <name><![CDATA[Thomas Mayfield]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Backbone Views and the Law of Demeter]]></title>
    <link href="http://zen-hacking.com/blog/2012/04/28/backbone-views-and-the-law-of-demeter/"/>
    <updated>2012-04-28T19:25:00-04:00</updated>
    <id>http://zen-hacking.com/blog/2012/04/28/backbone-views-and-the-law-of-demeter</id>
    <content type="html"><![CDATA[<p>I&#8217;ve been getting more and more excited about <a href="http://documentcloud.github.com/backbone/">Backbone.js</a> over the last few months. One of the greatest things about the framework is that it&#8217;s so unopinionated and modular that you can do anything between writing your whole app using Backbone idioms or applying just one or two pieces to an existing app. I&#8217;m particularly interested in how Backbone Views encourage cleaner separation between Javascript components on a page, without even using any other Backbone components. Let&#8217;s explore this a bit.</p>

<h2>Javascript Soup</h2>

<p>I was lucky enough to attend <a href="http://speakerdeck.com/u/sarahmei/p/using-backbonejs-with-rails">Sarah Mei&#8217;s great talk on Backbone.js</a> at RailsConf this year. She rather aptly described the phases a Rails app moves through in its usage of Javascript: from the &#8220;what Javascript?&#8221; view helpers like <code>link_to_remote</code> and friends, to adding just a bit of extra functionality in that one view with a few lines of real JS, to what she terms a &#8220;jQuery Explosion&#8221;. You know, when that little unobtrusive enhancement somehow became a five-hundred line stateful nightmare that&#8217;s hard to modify and harder to test? Her thesis is that this right here is Backbone&#8217;s sweet spot: non-trivial existing Javascript apps that are beginning to age and struggle due to lack of structure.</p>

<p>One of the patterns that comes out of poorly structured Javascript is violating the Law of Demeter. The Law of Demeter concerns loose coupling: it states (loosely) that each component of a program should only talk to its immediate &#8220;friends&#8221;- that is, other components of the program closely related to the current component. This has been generalized to &#8220;use only one dot&#8221; (meaning <code>a.doSomething()</code> is probably ok, but <code>a.b.doSomething()</code> is using too much knowledge of <code>a</code>&#8217;s implementation). In client-side Javascript, this can be applied by thinking of a page component&#8217;s DOM elements as out of the reach of other page components. Your navigation widget might communicate with your breadcrumb widget by event binding or direct method calls, but shouldn&#8217;t be reaching in and monkeying with the other&#8217;s DOM elements directly.</p>

<h2>How Backbone Helps</h2>

<p>Backbone Views encourage better patterns in a couple ways:</p>

<h4>Scoped Event Binding By Default</h4>

<p>Let&#8217;s look at a trivial Backbone View that represents a navigation bar.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">App</span><span class="p">.</span><span class="nx">NavigationView</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>
</span><span class='line'>  <span class="nx">el</span><span class="o">:</span> <span class="s1">&#39;#nav&#39;</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">events</span><span class="o">:</span> <span class="p">{</span>
</span><span class='line'>    <span class="s2">&quot;click li.item a&quot;</span><span class="o">:</span> <span class="s2">&quot;navigate&quot;</span>
</span><span class='line'>  <span class="p">},</span>
</span><span class='line'>  <span class="nx">navigate</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">evt</span><span class="p">){</span>
</span><span class='line'>    <span class="c1">// ...</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">})</span>
</span></code></pre></td></tr></table></div></figure>


<p>This view binds to the existing element on the page with the id <code>nav</code> - this element becomes the view&#8217;s root. All elements under it can be considered direct properties of the view, and only this view should directly manipulate them. The <code>events</code> property in the view&#8217;s configuration options here binds click events on elements matching the selector <code>li.item</code> to the view&#8217;s <code>navigate</code> method. <em>This is automatically scoped to only match elements that are descendents of the View&#8217;s root element</em>. We&#8217;ll only ever match events from our private DOM elements, and other Backbone components on the page won&#8217;t match events from our elements.</p>

<p>You can get the same effect with something like <code>$('#nav li.item a').click( /* nav  function */ )</code>, but this requires more discipline and doesn&#8217;t have the same effect of keeping all of our component&#8217;s event concerns in one place. It also establishes a common pattern of event handling across all our page components: convention over configuration.</p>

<h4>Scoped Selectors</h4>

<p>Setting up scoped event binding is great for maintaining our limited knowledge and encapsulation with regard to event handling, but once it&#8217;s time to actually modify those DOM elements, we need a way to do those operations with the same limitations.</p>

<p>Backbone provides a convenient way to make a query scoped to just the view&#8217;s elements: the <code>this.$</code> function. Nothing fancy going on here: in a Backbone View, the root element of the View&#8217;s DOM is bound to <code>this.el</code>, so <code>this.$</code> is just sugared-up <code>$(this.el).find</code>. Let&#8217;s take our navigation bar example from above again. We&#8217;ll make it set a current location field when you click on a navigation link.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">App</span><span class="p">.</span><span class="nx">NavigationView</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>
</span><span class='line'>  <span class="nx">el</span><span class="o">:</span> <span class="s1">&#39;#nav&#39;</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">events</span><span class="o">:</span> <span class="p">{</span>
</span><span class='line'>    <span class="s2">&quot;click li.item a&quot;</span><span class="o">:</span> <span class="s2">&quot;navigate&quot;</span>
</span><span class='line'>  <span class="p">},</span>
</span><span class='line'>  <span class="nx">navigate</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">evt</span><span class="p">){</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">location</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="nx">evt</span><span class="p">.</span><span class="nx">target</span><span class="p">).</span><span class="nx">text</span><span class="p">()</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;.location&#39;</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="nx">location</span><span class="p">)</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">})</span>
</span></code></pre></td></tr></table></div></figure>


<p>Nothing much surprising going on here: we grab the text from the link and plunk it down inside the element with class <code>location</code>. But consider what we got by using the view&#8217;s scoped selector instead of a document-wide query:</p>

<ul>
<li><p>We know we&#8217;re only modifying the DOM elements this view directly owns.</p></li>
<li><p>Our event handling code here doesn&#8217;t have to care if there&#8217;s more than one instance of NavigationView on the page. Multiple instances require extra care without a wrapping view- you&#8217;d need to know whose location element to modify and how to pick the right one.</p></li>
<li><p>This built-in knowledge of the component&#8217;s root also eliminates fragile traversals from the event&#8217;s element to the piece you want to modify. No more temptation to do stuff like <code>$(e.target).prev().children('.location')</code> in your event handlers, only to have them break when you alter your structure.</p></li>
<li><p>Even better, with this pattern, use of queries outside of <code>this.$</code> can serve as a useful warning sign that you&#8217;re stepping outside the limits of knowledge that your component should have.</p></li>
</ul>


<h2>We&#8217;re Just Getting Started</h2>

<p>Backbone offers a ton of ways to structure and modularize your Javascript code. You can represent your application&#8217;s state as Collections of as validated Models, write single-page apps with Routers, even render your HTML entirely client-side with various kind of JS templating - all of which can be pretty intimidating when you&#8217;re staring at years of jQuery soup and wondering where the hell to start. One of Backbone&#8217;s most important features, however, is that you don&#8217;t have to jump in whole hog. Teasing apart the code for individual page components into Backbone Views that respect the Law of Demeter has some immediate benefits and is a great jumping-off point for bringing some much-needed structure to your app&#8217;s Javascript.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Compiling Javascript templates with Guard]]></title>
    <link href="http://zen-hacking.com/blog/2012/04/02/compiling-javascript-templates-with-guard/"/>
    <updated>2012-04-02T21:02:00-04:00</updated>
    <id>http://zen-hacking.com/blog/2012/04/02/compiling-javascript-templates-with-guard</id>
    <content type="html"><![CDATA[<p>Yesterday, I released the first version of <a href="https://github.com/thegreatape/guard-templates">guard-templates</a>, my Guard plugin for compiling Javascript templates as you work.
<a href="https://github.com/guard/guard">Guard</a> is a nifty tool for watching a directory for changes and taking some kind of action upon the changed files - running tests, compiling SASS or Coffeescript, firing a Growl and about a billion other things.</p>

<p>The idea behind guard-templates is pretty simple: let your JS templates live in their own files in your project&#8217;s source tree and have Guard turn them into Javascript you can use, as soon as you save them. Some frameworks have mechanisms for this already (Rails&#8217;s Asset Pipeline, for example), but I wanted something I could drop into any project regardless of the technology being used. Let&#8217;s say you had the following <a href="http://handlebarsjs.com/">Handlebars</a> template:</p>

<figure class='code'><figcaption><span>javascripts/templates/post.handlebars  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt">&lt;div&gt;</span>{{example}}<span class="nt">&lt;/div&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>With guard-templates configured to watch .handlebars files, it&#8217;ll automatically compile to a simple JS string version of the file, ready to be included in the brower:</p>

<figure class='code'><figcaption><span>public/javascripts/templates.js  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">MyApp</span><span class="p">.</span><span class="nx">templates</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">post</span><span class="o">:</span> <span class="s1">&#39;&lt;div&gt;{{example}}&lt;/div&gt;&#39;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Bam, ready to use JS templating without gross inline string literals or Ajax requiring. But wait, there&#8217;s more! My favorite JS templating language is <a href="https://github.com/visionmedia/jade">Jade</a>, a HAML-ish language that supports precompilation to JS functions to avoid parsing the template itself at runtime.</p>

<figure class='code'><figcaption><span>javascripts/templates/post.jade</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='jade'><span class='line'><span class="nt">ul</span>
</span><span class='line'>  <span class="nt">li</span><span class="nc">.first</span>
</span><span class='line'>    <span class="nt">a</span>(<span class="na">href=</span><span class="s">&#39;#&#39;</span>) foo
</span></code></pre></td></tr></table></div></figure>


<p>After processing, this becomes a ready-to-call function:</p>

<figure class='code'><figcaption><span>public/javascripts/template.js</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">MyApp</span><span class="p">.</span><span class="nx">templates</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">post</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">anonymous</span><span class="p">(</span><span class="nx">locals</span><span class="p">,</span> <span class="nx">attrs</span><span class="p">,</span> <span class="nx">escape</span><span class="p">,</span> <span class="nx">rethrow</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// contents tuncated for brevity&#39;s sake</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>More options and details on usage are documented over on the <a href="https://github.com/thegreatape/guard-templates">project&#8217;s GitHub page</a>.
Drop me a line if it&#8217;s useful for you or if <a href="https://github.com/thegreatape/guard-templates/issues">something&#8217;s broken</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Converting JPEGs to multipage PDFs with ImageMagick]]></title>
    <link href="http://zen-hacking.com/blog/2011/05/05/converting-jpegs-to-multipage-pdfs-with-imagemagick/"/>
    <updated>2011-05-05T22:10:00-04:00</updated>
    <id>http://zen-hacking.com/blog/2011/05/05/converting-jpegs-to-multipage-pdfs-with-imagemagick</id>
    <content type="html"><![CDATA[<p>Because I always forget when I need this:</p>

<pre><code>convert -adjoin *.jpeg foo.pdf
</code></pre>
]]></content>
  </entry>
  
</feed>

