<?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:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>ThoughtStream.new :derick_bailey</title>
	
	<link>http://lostechies.com/derickbailey</link>
	<description>Better Than Yesterday</description>
	<lastBuildDate>Thu, 17 May 2012 12:57:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<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/DerickBailey" /><feedburner:info uri="derickbailey" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><geo:lat>31.514067</geo:lat><geo:long>-97.235465</geo:long><creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license><image><link>http://creativecommons.org/licenses/by/3.0/</link><url>http://creativecommons.org/images/public/somerights20.gif</url><title>Some Rights Reserved</title></image><feedburner:emailServiceId>DerickBailey</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Backbone.Syphon: Serialize Form Inputs To JavaScript Objects</title>
		<link>http://feedproxy.google.com/~r/DerickBailey/~3/rQaXw5vBoYo/</link>
		<comments>http://lostechies.com/derickbailey/2012/05/17/backbone-syphon-serialize-form-inputs-to-javascript-objects/#comments</comments>
		<pubDate>Thu, 17 May 2012 12:57:13 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Backbone.Syphon]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=934</guid>
		<description>Working with form elements in a Backbone view can become very tedious very quickly. You will either end up writing a lot of repetitive code to read values from the form, or end up using a key-value-observer or data-binding solution&amp;#160;&amp;#8230; &lt;a href="http://lostechies.com/derickbailey/2012/05/17/backbone-syphon-serialize-form-inputs-to-javascript-objects/"&gt;Continue&amp;#160;reading&amp;#160;&lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt;&lt;/a&gt;</description>
			<content:encoded><![CDATA[<p>Working with form elements in a Backbone view can become very tedious very quickly. You will either end up writing a lot of repetitive code to read values from the form, or end up using a key-value-observer or data-binding solution that automatically populates your model for you. While these are valid options and I highly recommend understanding how they work, there are times when these options are not the best choice for your application.</p>
<h2>jQuery</h2>
<p>The most basic option in serializing a form&#8217;s elements in to use jQuery&#8217;s <a href="http://api.jquery.com/serialize/">serialize</a> or <a href="http://api.jquery.com/serializeArray/">serializeArray</a> methods. These methods work well and make it easy to get data out of your form. But they have limitations that I don&#8217;t like:</p>
<p>The serialize method encodes the values in to a URL encoding. This works well if you plan on using the data as URL parameters in an HTTP GET or other similar manner. But if you want to use the data in your JavaScript code, it&#8217;s a bit of a pain to work with.</p>
<p>The serializeArray method return a JavaScript array that contains objects each with a name and value key. So, a text input with a &#8220;name&#8221; of &#8220;foo&#8221; would return [{name: "foo", value: "bar"}]. This works much better for dealing with the data in your JavaScript code, but is still severely constrained. You don&#8217;t have direct access to the key/value pairs based on the name of the input element. Instead, you have to search through the array of objects and find the one you want. </p>
<p>What I want, instead, is a way to serialize a form into a plain JavaScript object where the object&#8217;s attributes (or keys) are the input element ids, with the key&#8217;s value set to the value of the input element.</p>
<h2>Backbone.Syphon</h2>
<p><a href="https://github.com/derickbailey/backbone.syphon">Backbone.Syphon</a> aims to make it easy to serialize the form inputs of a Backbone.View in to a simple JSON object that contains all of the values from the form. All you need to do is call `Backbone.Syphon.serialize(view)` and it will return a JavaScript object to you with key / value pairs that match the input element ids and values.</p>
<p>For example, with this underscore template:</p>
<div id="gist-2710848" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;script </span><span class="na">id=</span><span class="s">&quot;my-form-template&quot;</span> <span class="na">type=</span><span class="s">&quot;text/html&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">form</span><span class="o">&gt;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">&quot;text&quot;</span> <span class="nx">id</span><span class="o">=</span><span class="s2">&quot;foo&quot;</span> <span class="nx">value</span><span class="o">=</span><span class="s2">&quot;bar&quot;</span><span class="o">&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">&quot;checkbox&quot;</span> <span class="nx">id</span><span class="o">=</span><span class="s2">&quot;chk&quot;</span> <span class="nx">checked</span><span class="o">&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="err">/form&gt;</span></div><div class='line' id='LC6'><span class="nt">&lt;/script&gt;</span></div></pre></div>
          </div>

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

<p>And this Backbone.View setup:</p>
<div id="gist-2710848" 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">events</span><span class="o">:</span> <span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s2">&quot;submit form&quot;</span><span class="o">:</span> <span class="s2">&quot;formSubmitted&quot;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="nx">formSubmitted</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">){</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">data</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Syphon</span><span class="p">.</span><span class="nx">serialize</span><span class="p">(</span><span class="k">this</span><span class="p">);</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">save</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">render</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="c1">// build the view&#39;s form, here</span></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC18'><span class="p">});</span></div></pre></div>
          </div>

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

<p>When you click the &#8220;save&#8221; button, the handler for the click calls `Backbone.Syphon.serialize(this)`. This returns an object that looks like this:</p>
<div id="gist-2710848" 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></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nx">foo</span><span class="o">:</span> <span class="s2">&quot;bar&quot;</span><span class="p">,</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nx">chk</span><span class="o">:</span> <span class="kc">true</span></div><div class='line' id='LC4'><span class="p">}</span></div></pre></div>
          </div>

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

<p>The large benefit of this output is that you can immediately pass it to a Backbone.Model instance via the `set` method. There&#8217;s no need to sift through it and pull out the bits you need, like the jQuery serialize methods. Of course you can use it for more than just Backbone models, too. Anywhere that you need the data from your form, you can get it with Syphon and use it right away.</p>
<p>I use this plugin in several of my projects already &#8211; well, precursors that were poorly written and not tested thoroughly, at least. But I plan on continuing to use this plugin in scenarios where I need to get data out of my forms. Data-binding plugins just don&#8217;t interest me anymore, as I&#8217;ve gone down that path and abandoned it. </p>
<h2>Serializes By Element Id</h2>
<p>There are a handful of limitations in this first release. One of the largest is that all of the input elements will be serialized by the `id` attribute of the element. Look at the example above again, and you&#8217;ll notice that the text input with an id of &#8220;foo&#8221; was serialized in to a `{foo: &#8220;bar&#8221;}` attribute on the resulting object. I&#8217;m sure this will change over time, and become something more configurable. But for now, this was all I needed as I tend to use the id of a field as the key for my object&#8217;s data.</p>
<p>For more information on the current limitations, <a href="https://github.com/derickbailey/backbone.syphon#current-limitation">see the documentation</a>.</p>
<h2>Some-what Pluggable Serialization</h2>
<p>I&#8217;ve built this plugin more than a few times at this point, starting with direct client needs and eventually turning in to the plugin that it is now. One of the lessons that I learned in rebuilding it a few times is that the process of serializing the input elements needs to be pluggable. I can&#8217;t limit how it reads the data from the input elements, because there are different needs for different situations. </p>
<p>With that in mind, I built in the idea of &#8220;<a href="https://github.com/derickbailey/backbone.syphon#register-your-own-input-readers">Input Readers</a>&#8220;. An Input Reader is a callback function that is registered against a particular input type, and is used to read the data from that type of input. </p>
<p>At the moment, there are only two built in input readers: the default, which handles nearly everything, and the checkbox reader. The default reader uses jQuery&#8217;s `val()` function to get the value out of the input. This works for nearly every type of input. But there are some cases where this isn&#8217;t what you want to do. For example, I generally use checkboxes as boolean values instead of an actual text value. To handle this, then, I created the built in checkbox input reader. It uses jQuery&#8217;s `prop()` function to read the state of the checkbox and tell me whether or not the checkbox is checked. If you look at the previous example again, you&#8217;ll see that the &#8220;chk&#8221; checkbox is returning a boolean value.</p>
<h2>Initial Build And Release As A Screencast</h2>
<p>There are several reasons that this initial release is so small and limited in scope and functionality. The largest of which is that it covers the majority of my needs, and like all of my plugins and add-ons, I figure out what I need and the common things that I do across projects first. Then when I have the majority of my cases covered, I start looking at what others need in order to get their projects up and running.</p>
<p>Another reason that this release is limited, is because I decided to record the creation of the plugin as a live screencast &#8211; a &#8220;prequel&#8221; of sorts, to the first &#8220;<a href="http://www.watchmecode.net/refactoring-javascript">Refactoring JavaScript</a>&#8221; screencast that I released last year. In this case, the end product of the Backbone.Syphon plugin is more of a side effect than the purpose of the screencast. The real focus of the screencast is the tools and processes that I use to get a plugin like this off the ground, built and delivered. </p>
<p>If you&#8217;re an audio/video learner and you want to see my process for creating something from nothing more than the ideas in my head, then you should check out Episode 7 of WatchMeCode: <a href="http://www.watchmecode.net/backbone-plugin">Building A Backbone Plugin (Live)</a>. And if you&#8217;re looking for a way to get up to speed with test-driven JavaScript, including a basic introduction to Jasmine and refactoring an existing application that is covered by Jasmine tests, then you might be interested in the <a href="http://www.watchmecode.net/jasmine-live">Jasmine Live! multi-pack</a> of screencasts.</p>
<h2>Documentation, Downloads, And Moving Forward</h2>
<p>If you&#8217;d like to get your hands on Syphon, head over to <a href="https://github.com/derickbailey/backbone.syphon">the Github repository</a>. There are links to the the downloads and a little bit of documentation to show how to use it (the same as what I showed here), as well as some discussion on the current limitations of the plugin.</p>
<p>I&#8217;m hoping to get some input and assistance from others that could use a plugin like this, moving forward. I&#8217;d like to see this project fleshed out so that it can be used in a broader set of scenarios, with more options for how the data is serialized from the input elements. At the same time, though, I&#8217;m going to be rather picky about how the implementation moves forward. I don&#8217;t want to end up with another 1,000 line blob of unmaintainable garbage, like my ModelBinding plugin. As it stands now, though, Syphon&#8217;s code is starting out on the right foot. I just need to keep it heading down a clean path as it moves forward.</p>
<p> </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/DerickBailey?a=rQaXw5vBoYo:xz8wBxTxNKA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=rQaXw5vBoYo:xz8wBxTxNKA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=rQaXw5vBoYo:xz8wBxTxNKA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=rQaXw5vBoYo:xz8wBxTxNKA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=rQaXw5vBoYo:xz8wBxTxNKA:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=rQaXw5vBoYo:xz8wBxTxNKA:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=rQaXw5vBoYo:xz8wBxTxNKA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=rQaXw5vBoYo:xz8wBxTxNKA:bcOpcFrp8Mo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=bcOpcFrp8Mo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/DerickBailey/~4/rQaXw5vBoYo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/derickbailey/2012/05/17/backbone-syphon-serialize-form-inputs-to-javascript-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://lostechies.com/derickbailey/2012/05/17/backbone-syphon-serialize-form-inputs-to-javascript-objects/</feedburner:origLink></item>
		<item>
		<title>Workflow In Backbone Apps: Triggering View Events From DOM Events</title>
		<link>http://feedproxy.google.com/~r/DerickBailey/~3/k7cQRiMo8b0/</link>
		<comments>http://lostechies.com/derickbailey/2012/05/15/workflow-in-backbone-apps-triggering-view-events-from-dom-events/#comments</comments>
		<pubDate>Tue, 15 May 2012 13:04:05 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Workflow]]></category>

		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=930</guid>
		<description>In my previous blog post, I talked about modeling an explicit workflow in JavaScript and Backbone application. The last code sample I showed had the implementation of the workflow object itself, but omitted all of the details of the views&amp;#160;&amp;#8230; &lt;a href="http://lostechies.com/derickbailey/2012/05/15/workflow-in-backbone-apps-triggering-view-events-from-dom-events/"&gt;Continue&amp;#160;reading&amp;#160;&lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt;&lt;/a&gt;</description>
			<content:encoded><![CDATA[<p>In my previous blog post, I talked about <a href="http://lostechies.com/derickbailey/2012/05/10/modeling-explicit-workflow-with-code-in-javascript-and-backbone-apps/">modeling an explicit workflow in JavaScript and Backbone application</a>. The last code sample I showed had the implementation of the workflow object itself, but omitted all of the details of the views that I was using.</p>
<p>Here&#8217;s the gist of that again:</p>
<div id="gist-2651039" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">orgChart</span> <span class="o">=</span> <span class="p">{</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nx">addNewEmployee</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC4'>&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='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">employeeDetail</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">getEmployeeDetail</span><span class="p">();</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">employeeDetail</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;complete&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">employee</span><span class="p">){</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">managerSelector</span> <span class="o">=</span> <span class="nx">that</span><span class="p">.</span><span class="nx">selectManager</span><span class="p">(</span><span class="nx">employee</span><span class="p">);</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">managerSelector</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;save&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">employee</span><span class="p">){</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">employee</span><span class="p">.</span><span class="nx">save</span><span class="p">();</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<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="nx">getEmployeeDetail</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">form</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EmployeeDetailForm</span><span class="p">();</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">form</span><span class="p">.</span><span class="nx">render</span><span class="p">();</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#wizard&quot;</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="nx">form</span><span class="p">.</span><span class="nx">el</span><span class="p">);</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">form</span><span class="p">;</span></div><div class='line' id='LC22'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC23'><br/></div><div class='line' id='LC24'>&nbsp;&nbsp;<span class="nx">selectManager</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">employee</span><span class="p">){</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">form</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">SelectManagerForm</span><span class="p">({</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">model</span><span class="o">:</span> <span class="nx">employee</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">form</span><span class="p">.</span><span class="nx">render</span><span class="p">();</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#wizard&quot;</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="nx">form</span><span class="p">.</span><span class="nx">el</span><span class="p">);</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">form</span><span class="p">;</span></div><div class='line' id='LC31'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC32'><span class="p">}</span></div><div class='line' id='LC33'><br/></div><div class='line' id='LC34'><span class="c1">// implementation details for EmployeeDetailForm go here</span></div><div class='line' id='LC35'><br/></div><div class='line' id='LC36'><span class="c1">// implementation details for SelectManagerForm go here</span></div><div class='line' id='LC37'><br/></div><div class='line' id='LC38'><span class="c1">// implementation details for Employee model go here</span></div></pre></div>
          </div>

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

<p>I&#8217;ve implemented various views and objects in various manners, in order to facilitate that workflow, but what it always comes down to is that the objects facilitating the workflow need to trigger events. </p>
<h2>A Basic Implementation</h2>
<p>The most common way of triggering an event from a Backbone.View is to have some DOM events handled, and from the event handler/callback method for that, trigger the event you want:</p>
<div id="gist-2653055" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">SomeView</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='LC2'><br/></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nx">events</span><span class="o">:</span> <span class="p">{</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s2">&quot;click .next&quot;</span><span class="o">:</span> <span class="s2">&quot;nextClicked&quot;</span><span class="p">,</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s2">&quot;click .previous&quot;</span><span class="o">:</span> <span class="s2">&quot;previousClicked&quot;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="nx">nextClicked</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">){</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s2">&quot;next&quot;</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'>&nbsp;&nbsp;<span class="nx">previousClicked</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">){</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s2">&quot;previous&quot;</span><span class="p">);</span></div><div class='line' id='LC16'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'><span class="p">});</span></div></pre></div>
          </div>

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

<p>This works well. I&#8217;ve done this more times than I can count. But there&#8217;s a lot of redundancy here. You can see that both of the DOM events that I&#8217;m handling have a callback method, and both of the callback methods do nothing more than trigger an event from the view itself so that the workflow can move forward.</p>
<p>We can do better than that… enter Marionette&#8217;s new &#8220;triggers&#8221;.</p>
<h2>Reducing Event Triggering To Configuration</h2>
<p>With the v0.8.2 release of <a href="https://github.com/derickbailey/backbone.marionette">Marionette</a>, I&#8217;ve introduced the idea of <a href="https://github.com/derickbailey/backbone.marionette#viewtriggers">view triggers</a> &#8211; a way to configure a DOM event to trigger a view event. So instead of having to write all of that redundant code, where the only real difference is the name of the callback method and the name of the event that&#8217;s triggered, we can write this:</p>
<div id="gist-2653055" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">SomeView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Marionette</span><span class="p">.</span><span class="nx">ItemView</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="c1">// ...</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="nx">triggers</span><span class="o">:</span> <span class="p">{</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s2">&quot;click .next&quot;</span><span class="o">:</span> <span class="s2">&quot;next&quot;</span><span class="p">,</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s2">&quot;click .previous&quot;</span><span class="o">:</span> <span class="s2">&quot;previous&quot;</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'><span class="p">});</span></div></pre></div>
          </div>

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

<p>Must shorter. Much cleaner.</p>
<p>You can see that the left side of the hash is a standard Backbone.View events setup where you specify the DOM event and element selector. Then on the right hand side, specify the name of the event that you want the view to trigger when that DOM event fires. Then, when I click on the &#8220;.next&#8221; element, the view will trigger a &#8220;next&#8221; event for me. Similarly, clicking the &#8220;.previous&#8221; element will trigger a &#8220;previous&#8221; event.</p>
<h2>Trigger From Any Marionette View</h2>
<p>Triggers can be used from any Marionette view. You&#8217;re not limited to the ItemView type that I showed. In fact, the trigger functionality is built in to the base Backbone.Marionette.View, so even if you are creating your own specialized view type, you only need to extend from that in order to get this functionality.</p>
<h2>Limitations</h2>
<p>Of course this isn&#8217;t a &#8220;complete&#8221; solution for handling events in your views, by any means. If you need to do anything more than trigger the event by itself, you should still use a standard event handler and callback method. At this point, there&#8217;s no way for you to configure any kind of data or objects to be passed through the event itself. But this may be added at some point in the future. I wrote this fairy quickly last night, and it suits my needs right now. </p>
<p>For simple scenarios where you just need to trigger an event from the view, though, this works well. I&#8217;m using it in several places where my workflow is just to display a button or a link, and have that clickable item move on to another part of the application.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/DerickBailey?a=k7cQRiMo8b0:AvuRscbm0AQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=k7cQRiMo8b0:AvuRscbm0AQ:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=k7cQRiMo8b0:AvuRscbm0AQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=k7cQRiMo8b0:AvuRscbm0AQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=k7cQRiMo8b0:AvuRscbm0AQ:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=k7cQRiMo8b0:AvuRscbm0AQ:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=k7cQRiMo8b0:AvuRscbm0AQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=k7cQRiMo8b0:AvuRscbm0AQ:bcOpcFrp8Mo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=bcOpcFrp8Mo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/DerickBailey/~4/k7cQRiMo8b0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/derickbailey/2012/05/15/workflow-in-backbone-apps-triggering-view-events-from-dom-events/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://lostechies.com/derickbailey/2012/05/15/workflow-in-backbone-apps-triggering-view-events-from-dom-events/</feedburner:origLink></item>
		<item>
		<title>Modeling Explicit Workflow With Code, In JavaScript And Backbone Apps</title>
		<link>http://feedproxy.google.com/~r/DerickBailey/~3/80NnIDpZiCI/</link>
		<comments>http://lostechies.com/derickbailey/2012/05/10/modeling-explicit-workflow-with-code-in-javascript-and-backbone-apps/#comments</comments>
		<pubDate>Thu, 10 May 2012 13:02:23 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[AntiPatterns]]></category>
		<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Principles and Patterns]]></category>
		<category><![CDATA[Workflow]]></category>

		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=928</guid>
		<description>A long time ago, in what seems to be a previous life at this point, I wrote a small blog post about modeling and creating an explicit return value from a dialog form in a Windows application. Fast forward a&amp;#160;&amp;#8230; &lt;a href="http://lostechies.com/derickbailey/2012/05/10/modeling-explicit-workflow-with-code-in-javascript-and-backbone-apps/"&gt;Continue&amp;#160;reading&amp;#160;&lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt;&lt;/a&gt;</description>
			<content:encoded><![CDATA[<p>A long time ago, in what seems to be a previous life at this point, I wrote a small blog post about modeling and <a href="http://lostechies.com/derickbailey/2009/05/19/result-lt-t-gt-directing-workflow-with-a-return-status-and-value/">creating an explicit return value from a dialog</a> form in a Windows application. Fast forward a lifetime (3 years) and I&#8217;m finding that this knowledge and experience is resurfacing itself on almost a daily basis with my work in Backbone and Marionette. In fact, I&#8217;ve used almost this exact pattern that I first used in WinForms, with several Backbone applications now. But I found it to be rather limiting in how I was able to implement it, because of the language and runtime differences. What I&#8217;ve settled on, though, is giving me the same benefit of explicit modeling, semantics, and encapsulating workflow: events.</p>
<h2>A Poorly Constructed Workflow</h2>
<p>It seems to be common in the JavaScript applications world, to have very poorly defined and constructed workflow in applications. We take one object and build some functionality. Then when the next part of the app needs to fire up, we call the object that runs it directly from the first object. Then when the next part of the app is requested, we call that object from the second one. And we continue on down this path ad-infinitum, creating a mess of tightly coupled concerns, tightly coupled concepts, tightly coupled objects, and a brittle and fragile system that is dependent entirely on the implementation details to understand the higher level concept.</p>
<p>For example, might have a human resources application that allows you to add a new employee and select a manager for the employee. After entering a name and email address, we would show the form to select the manager. When the user clicks save, we create the employee. A crude, but all too common implementation of this workflow might look something like this:</p>
<div id="gist-2651039" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">EmployeeInfoForm</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='LC2'>&nbsp;&nbsp;<span class="nx">events</span><span class="o">:</span> <span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s2">&quot;click .next&quot;</span><span class="o">:</span> <span class="s2">&quot;nextClicked&quot;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="nx">nextClicked</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">){</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">data</span> <span class="o">=</span> <span class="p">{</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">name</span><span class="o">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;.name&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">(),</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">email</span><span class="o">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;.email&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">()</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">};</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">employee</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Employee</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">selectManager</span><span class="p">(</span><span class="nx">employee</span><span class="p">);</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">selectManager</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">employee</span><span class="p">){</span></div><div class='line' id='LC20'>&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">SelectManagerForm</span><span class="p">({</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">model</span><span class="o">:</span> <span class="nx">employee</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC23'>&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='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;.wizard&quot;</span><span class="p">).</span><span class="nx">show</span><span class="p">(</span><span class="nx">view</span><span class="p">.</span><span class="nx">el</span><span class="p">);</span></div><div class='line' id='LC25'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC26'><br/></div><div class='line' id='LC27'>&nbsp;&nbsp;<span class="c1">// ...</span></div><div class='line' id='LC28'>&nbsp;&nbsp;<span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span> <span class="p">...</span> <span class="p">}</span></div><div class='line' id='LC29'>&nbsp;&nbsp;<span class="c1">// ... etc</span></div><div class='line' id='LC30'><span class="p">});</span></div><div class='line' id='LC31'><br/></div><div class='line' id='LC32'><span class="nx">SelectManagerForm</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='LC33'>&nbsp;&nbsp;<span class="nx">events</span><span class="o">:</span> <span class="p">{</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s2">&quot;click .save&quot;</span><span class="o">:</span> <span class="s2">&quot;saveClicked&quot;</span></div><div class='line' id='LC35'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC36'><br/></div><div class='line' id='LC37'>&nbsp;&nbsp;<span class="nx">saveClicked</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">){</span></div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span></div><div class='line' id='LC39'><br/></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">managerId</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;.manager&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">();</span></div><div class='line' id='LC41'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">set</span><span class="p">({</span><span class="nx">managerId</span><span class="o">:</span> <span class="nx">managerId</span><span class="p">});</span></div><div class='line' id='LC42'><br/></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">save</span><span class="p">();</span></div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// do something to close the wizard and move on  </span></div><div class='line' id='LC45'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC46'><br/></div><div class='line' id='LC47'>&nbsp;&nbsp;<span class="c1">// ...</span></div><div class='line' id='LC48'>&nbsp;&nbsp;<span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span></div><div class='line' id='LC49'>&nbsp;&nbsp;<span class="c1">// ... etc</span></div><div class='line' id='LC50'><span class="p">});</span></div></pre></div>
          </div>

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

<p>Can you quickly and easily describe the workflow in this example? If you can, then you&#8217;re a better person than I am. I have to spend time looking at the implementation details of both views in order to see what&#8217;s going on and why. I have to piece together the bits from multiple places and form a more coherent high level overview in my head. It&#8217;s not easy for me to see what&#8217;s going on because every time I look at another part of the code, I have to put together the pieces again to make sure I am not breaking someone from the other parts.</p>
<h2>Too Many Concerns</h2>
<p>We&#8217;ve mixed two different concerns in to very few objects, and we&#8217;ve taken those concerns and split them apart in some rather un-natural ways at that.</p>
<p>The first concern is the high level workflow:</p>
<ol>
<li>enter employee info</li>
<li>select manager</li>
<li>create employee</li>
</ol>
<p>The second concern is the implementation detail:</p>
<ol>
<li>Show the EmployeeInfoForm </li>
<li>Allow the user to enter a name and email address</li>
<li>When &#8220;next&#8221; is clicked, gather the name and email address of the employee. </li>
<li>Then show the SelectManagerForm with a list of possible managers to select from. </li>
<li>When &#8220;save&#8221; is clicked, grab the selected manager</li>
<li>Then take all of the employee information and create a new employee record on the server</li>
</ol>
<p>And I haven&#8217;t even gone through any of the secondary and third level workflow in this. What happens when the user hits cancel on the first screen? Or on the second? What about invalid email address validation? If we start adding in all of those steps to the list of implementation details, this list of steps to follow is going to get out of hand very quickly.</p>
<p>By implementing both the high level workflow and the implementation detail in the views &#8211; the details and implementation &#8211; we&#8217;ve destroyed our ability to see the high level workflow at a glance. That will cause problems for us as developers, because we will forget some of those details when changing the system, and we will break things.</p>
<h2>Modeling An Explicit Workflow In Code</h2>
<p>What we want to do, instead, is get back to that high level workflow with fewer bullet points and very little text in each point. But we don&#8217;t want to have to dig through all of the implementation details in order to get to it. We want to see the high level workflow in our code, separated from the implementation details. This makes it easier to change the workflow and to change any specific implementation detail without having to rework the entire workflow.</p>
<p>Wouldn&#8217;t it be nice if we could write this code, for example:</p>
<div id="gist-2651039" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">orgChart</span> <span class="o">=</span> <span class="p">{</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nx">addNewEmployee</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">employeeDetail</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">getEmployeeDetail</span><span class="p">();</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">employeeDetail</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;complete&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">employee</span><span class="p">){</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">managerSelector</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">selectManager</span><span class="p">(</span><span class="nx">employee</span><span class="p">);</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">managerSelector</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;save&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">employee</span><span class="p">){</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">employee</span><span class="p">.</span><span class="nx">save</span><span class="p">();</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<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="c1">// ...</span></div><div class='line' id='LC16'><span class="p">}</span></div></pre></div>
          </div>

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

<p>In this pseudo-code example, we can more clearly see the high level workflow. When we complete the employee info, we move on to the selecting a manager. When that completes, we save the employee with the data that we had entered. It all looks very clean and simple. We could even add in some of the secondary and third level workflow without creating too much mess. And more importantly, we could <a href="http://wekeroad.com/2012/04/05/cleaning-up-deep-callback-nesting-with-nodes-eventemitter">get rid of some of the nested callbacks</a> with better patterns and function separation.</p>
<p>But let&#8217;s see what this would really look like in code that we could execute:</p>
<div id="gist-2651039" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">orgChart</span> <span class="o">=</span> <span class="p">{</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nx">addNewEmployee</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">employeeDetail</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">getEmployeeDetail</span><span class="p">();</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">employeeDetail</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;complete&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">employee</span><span class="p">){</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">managerSelector</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">selectManager</span><span class="p">(</span><span class="nx">employee</span><span class="p">);</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">managerSelector</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;save&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">employee</span><span class="p">){</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">employee</span><span class="p">.</span><span class="nx">save</span><span class="p">();</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<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="c1">// ...</span></div><div class='line' id='LC16'><span class="p">}</span></div></pre></div>
          </div>

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

<p>Yeah &#8211; turns out this is code that we can actually run, and it can be implemented fairly easily with a couple of Backbone views and a model for the details:</p>
<div id="gist-2651039" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">orgChart</span> <span class="o">=</span> <span class="p">{</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nx">addNewEmployee</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC4'>&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='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">employeeDetail</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">getEmployeeDetail</span><span class="p">();</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">employeeDetail</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;complete&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">employee</span><span class="p">){</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">managerSelector</span> <span class="o">=</span> <span class="nx">that</span><span class="p">.</span><span class="nx">selectManager</span><span class="p">(</span><span class="nx">employee</span><span class="p">);</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">managerSelector</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;save&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">employee</span><span class="p">){</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">employee</span><span class="p">.</span><span class="nx">save</span><span class="p">();</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<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="nx">getEmployeeDetail</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">form</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EmployeeDetailForm</span><span class="p">();</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">form</span><span class="p">.</span><span class="nx">render</span><span class="p">();</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#wizard&quot;</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="nx">form</span><span class="p">.</span><span class="nx">el</span><span class="p">);</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">form</span><span class="p">;</span></div><div class='line' id='LC22'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC23'><br/></div><div class='line' id='LC24'>&nbsp;&nbsp;<span class="nx">selectManager</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">employee</span><span class="p">){</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">form</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">SelectManagerForm</span><span class="p">({</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">model</span><span class="o">:</span> <span class="nx">employee</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">form</span><span class="p">.</span><span class="nx">render</span><span class="p">();</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#wizard&quot;</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="nx">form</span><span class="p">.</span><span class="nx">el</span><span class="p">);</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">form</span><span class="p">;</span></div><div class='line' id='LC31'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC32'><span class="p">}</span></div><div class='line' id='LC33'><br/></div><div class='line' id='LC34'><span class="c1">// implementation details for EmployeeDetailForm go here</span></div><div class='line' id='LC35'><br/></div><div class='line' id='LC36'><span class="c1">// implementation details for SelectManagerForm go here</span></div><div class='line' id='LC37'><br/></div><div class='line' id='LC38'><span class="c1">// implementation details for Employee model go here</span></div></pre></div>
          </div>

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

<p>I&#8217;ve obviously omitted some of the details of the views and model, but you get the idea.</p>
<h2>The Benefits</h2>
<p>There are a number of benefits to writing code like this. It&#8217;s easy to see the high level workflow. We don&#8217;t have to worry about all of the implementation details for each of the views or the model when dealing with the workflow. We can change any of the individual view implementations when we need to, without affecting the rest of the workflow (<a href="http://lostechies.com/derickbailey/2011/09/22/dependency-injection-is-not-the-same-as-the-dependency-inversion-principle/">as long as the view conforms to the protocol that the workflow defines</a>). And there&#8217;s probably a handful of other benefits, as well. </p>
<p>But the largest single benefit of all these, in my experience, is being able to see the workflow at a glance. 6 months from now &#8211; or if you&#8217;re like me, 6 hours from now &#8211; you won&#8217;t remember that you have to trace through 5 different Views and three different custom objects and models, in order to piece together the workflow that you spun together in the sample at the very top of this post. But if you have a workflow as simple as the one that we just saw, where the workflow is more explicit within a higher level method, separated from the implementation details… well, then you&#8217;re more likely to pick up the code and understand the workflow quickly. </p>
<h2>The Drawbacks</h2>
<p>Everything has a price, right? But the price for this is fairly small. You will end up with a few more objects and a few more methods to keep track of. There&#8217;s a mild overhead associated with this in the world of browser based JavaScript, but that&#8217;s likely to be so small that you won&#8217;t notice.</p>
<p>The real cost, though, is that you&#8217;re going to have to learn new implementation patterns and styles of development in order to get this working, and that takes time. Sure, looking at an example like this is easy. But it&#8217;s a simple example and a simple implementation. When you get down to actually trying to write this style of code for yourself, in your project, with your 20 variations on the flow through the application, it will get more complicated, quickly. And there&#8217;s no simple answer for this complication in design, other than to say that you need to learn to break down the larger workflow in to smaller pieces that can look as simple as this one.</p>
<h2>But It&#8217;s Worth It</h2>
<p>In the end, in spite of potential drawbacks and learning curves, making an effort to explicitly model your workflow in your application is important. And it really doesn&#8217;t matter what language your writing your code in. I&#8217;ve shown these examples in JavaScript and Backbone because that&#8217;s what I&#8217;m using on a daily basis at this point. But I&#8217;ve been applying these same rules to C#/.NET, Ruby and other languages for years. The principles are the same, it&#8217;s just the implementation specifics that change.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/DerickBailey?a=80NnIDpZiCI:ItlYlA3Ogsw:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=80NnIDpZiCI:ItlYlA3Ogsw:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=80NnIDpZiCI:ItlYlA3Ogsw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=80NnIDpZiCI:ItlYlA3Ogsw:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=80NnIDpZiCI:ItlYlA3Ogsw:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=80NnIDpZiCI:ItlYlA3Ogsw:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=80NnIDpZiCI:ItlYlA3Ogsw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=80NnIDpZiCI:ItlYlA3Ogsw:bcOpcFrp8Mo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=bcOpcFrp8Mo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/DerickBailey/~4/80NnIDpZiCI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/derickbailey/2012/05/10/modeling-explicit-workflow-with-code-in-javascript-and-backbone-apps/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://lostechies.com/derickbailey/2012/05/10/modeling-explicit-workflow-with-code-in-javascript-and-backbone-apps/</feedburner:origLink></item>
		<item>
		<title>Help Me Improve The Marionette Wiki</title>
		<link>http://feedproxy.google.com/~r/DerickBailey/~3/TNw8vVD2B1g/</link>
		<comments>http://lostechies.com/derickbailey/2012/05/09/help-me-improve-the-marionette-wiki/#comments</comments>
		<pubDate>Wed, 09 May 2012 13:28:56 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Community]]></category>
		<category><![CDATA[Marionette]]></category>

		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=926</guid>
		<description>Documentation is difficult to write, and even more difficult to write well so that it covers all of the nuances and details that the people building the actual project tend to assume and forget. My Marionette project is no different.&amp;#160;&amp;#8230; &lt;a href="http://lostechies.com/derickbailey/2012/05/09/help-me-improve-the-marionette-wiki/"&gt;Continue&amp;#160;reading&amp;#160;&lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt;&lt;/a&gt;</description>
			<content:encoded><![CDATA[<p>Documentation is difficult to write, and even more difficult to write well so that it covers all of the nuances and details that the people building the actual project tend to assume and forget. My Marionette project is no different. I continue to receive feedback that it&#8217;s hard to know what each piece is really best suited for, questions on which View type is best suited for a scenario, and general inquiries for examples on how to implement specific behaviors and features.</p>
<p>And I need your help to answer these questions and improve the documentation and <a href="https://github.com/derickbailey/backbone.marionette/wiki">wiki</a>.</p>
<h2>The Marionette Wiki</h2>
<p>In addition to the technical documentation in the project&#8217;s readme, I&#8217;ve started up the wiki on the github project page. I&#8217;ve begun with two simple pages: an overview on the different view types, and page to let Marionette users show of their projects and websites that are using it.</p>
<p>Help me and the rest of the community of Marionette users improve the documentation for this project by providing examples, best practices, answers to frequently asked questions, and more. </p>
<p>Head over to the <a href="https://github.com/derickbailey/backbone.marionette/wiki">wiki</a> and start helping! </p>
<h3><strong><a href="https://github.com/derickbailey/backbone.marionette/wiki">https://github.com/derickbailey/backbone.marionette/wiki</a></strong></h3>
<h2>Topics To Cover</h2>
<p>Example topics that should be covered in the wiki:</p>
<ul>
<li>Using Marionette with RequireJS</li>
<li>Using JST templates with Marionette views</li>
<li>Using Handlebars templates with Marionette views</li>
<li>Using jQuery plugins and visual controls with Marionette</li>
<li>and much, much more</li>
</ul>
<p>I realize that some of these are items that I&#8217;ve blogged about here. One of my hopes and goals is to have many of the Marionette blog posts from this blog, copied in to the Wiki in a manner that makes sense for the wiki. I will try to do some of this myself, but it would also be wonderfully helpful if other members of the Marionette community could assist in doing this.</p>
<h2>I Need Your Help. Yes. YOU.</h2>
<p>I know I&#8217;m asking a lot &#8211; but I believe the community of Marionette users is up to it. It only takes a few minutes of time to improve one page or create a new one with some very basic information. Then another person can come along and make another improvement. Then another, and another. </p>
<p>Lastly: don&#8217;t think &#8220;oh, someone else will do it&#8221;. They won&#8217;t. Believe me. The distribution of responsibility is the single largest reason that people won&#8217;t take responsibility or put effort in to something. Everyone assumes that someone else will do it, and no one actually does it.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/DerickBailey?a=TNw8vVD2B1g:ut-7W5ChBXs:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=TNw8vVD2B1g:ut-7W5ChBXs:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=TNw8vVD2B1g:ut-7W5ChBXs:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=TNw8vVD2B1g:ut-7W5ChBXs:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=TNw8vVD2B1g:ut-7W5ChBXs:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=TNw8vVD2B1g:ut-7W5ChBXs:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=TNw8vVD2B1g:ut-7W5ChBXs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=TNw8vVD2B1g:ut-7W5ChBXs:bcOpcFrp8Mo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=bcOpcFrp8Mo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/DerickBailey/~4/TNw8vVD2B1g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/derickbailey/2012/05/09/help-me-improve-the-marionette-wiki/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://lostechies.com/derickbailey/2012/05/09/help-me-improve-the-marionette-wiki/</feedburner:origLink></item>
		<item>
		<title>Wrapping $.ajax In A Thin “Command” Framework For Backbone Apps</title>
		<link>http://feedproxy.google.com/~r/DerickBailey/~3/UPAozqmEBc0/</link>
		<comments>http://lostechies.com/derickbailey/2012/05/04/wrapping-ajax-in-a-thin-command-framework-for-backbone-apps/#comments</comments>
		<pubDate>Fri, 04 May 2012 14:45:16 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Backbone]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Semantics]]></category>
		<category><![CDATA[Underscore]]></category>

		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=922</guid>
		<description>A large number of my recent client applications, built on Backbone, have been using straight jQuery AJAX calls. Sometimes its in combination with Backbone&amp;#8217;s persistence method, but other times I end up using AJAX calls in place of Backbone calls.&amp;#160;&amp;#8230; &lt;a href="http://lostechies.com/derickbailey/2012/05/04/wrapping-ajax-in-a-thin-command-framework-for-backbone-apps/"&gt;Continue&amp;#160;reading&amp;#160;&lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt;&lt;/a&gt;</description>
			<content:encoded><![CDATA[<p>A large number of my recent client applications, built on Backbone, have been using straight jQuery AJAX calls. Sometimes its in combination with Backbone&#8217;s persistence method, but other times I end up using AJAX calls in place of Backbone calls.</p>
<h2>Non-CRUD-y Apps</h2>
<p>Now I don&#8217;t think that there&#8217;s anything wrong with Backbone&#8217;s &#8220;save&#8221;, &#8220;fetch&#8221; and &#8220;destroy&#8221; methods. They&#8217;re quite handy, and very well done. The deciding factor in using them is whether or not my application is CRUD-y or not. That is to say, many of my recent apps are taking more of a command approach to user interaction and data updates. Instead of just having simple Create, Read, Update and Delete forms on my apps, I tend to have smaller and more focused commands that are called.</p>
<p>For example, signing a terms of service agreement is a command to sign the agreement, not just a &#8220;create&#8221; on a resource. Sure, the implementation of that may be a &#8220;POST&#8221; on a resource location. But that doesn&#8217;t mean I need a full Backbone model, or need to think about the signature as a RESTful resource from my JavaScript code.</p>
<h2>Executing Commands vs Making AJAX Calls</h2>
<p>As a result of all this, I end up using $.ajax a lot… and it&#8217;s getting really ugly and obnoxious seeing this all over my code:</p>
<div id="gist-2595175" 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">signForm</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">ajax</span><span class="p">({</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nx">type</span><span class="o">:</span> <span class="s2">&quot;POST&quot;</span><span class="p">,</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nx">url</span><span class="o">:</span> <span class="s2">&quot;/some/form&quot;</span><span class="p">,</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nx">dataType</span><span class="o">:</span> <span class="s2">&quot;JSON&quot;</span><span class="p">,</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="nx">data</span><span class="o">:</span> <span class="nx">myData</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="nx">signForm</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">response</span><span class="p">){</span></div><div class='line' id='LC9'>&nbsp;&nbsp;<span class="c1">// handle success here</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">signForm</span><span class="p">.</span><span class="nx">fail</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">response</span><span class="p">){</span></div><div class='line' id='LC13'>&nbsp;&nbsp;<span class="c1">// handle failure here</span></div><div class='line' id='LC14'><span class="p">});</span></div></pre></div>
          </div>

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

<p>Not only is this ugly when you repeat the $.ajax calls all over the code, but it doesn&#8217;t really give the semantic benefit of knowing what the code is really doing. I&#8217;m not just making a call across an AJAX connection. I&#8217;m trying to execute a command &#8211; I&#8217;m trying to do something important, and I want my code to reflect that.</p>
<p>So I wrapped up my $.ajax calls in a thin &#8220;command&#8221; wrapper for my current client app (built with Backbone of course), and ended up with this instead:</p>
<div id="gist-2595175" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">// Register a command to use</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="nx">Backbone</span><span class="p">.</span><span class="nx">AjaxCommands</span><span class="p">.</span><span class="nx">register</span><span class="p">(</span><span class="s2">&quot;signForm&quot;</span><span class="p">,</span> <span class="p">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="nx">url</span><span class="o">:</span> <span class="s2">&quot;/some/form&quot;</span><span class="p">,</span></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="nx">type</span><span class="o">:</span> <span class="s2">&quot;POST&quot;</span></div><div class='line' id='LC7'><span class="p">});</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="c1">// somewhere else in the application, use the command</span></div><div class='line' id='LC11'><span class="c1">// --------------------------------------------------</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="kd">var</span> <span class="nx">signForm</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">AjaxCommands</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s2">&quot;signForm&quot;</span><span class="p">);</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'><span class="nx">signForm</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;success&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">response</span><span class="p">){</span></div><div class='line' id='LC16'>&nbsp;&nbsp;<span class="c1">// handle success here</span></div><div class='line' id='LC17'><span class="p">});</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'><span class="nx">signForm</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;error&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">response</span><span class="p">){</span></div><div class='line' id='LC20'>&nbsp;&nbsp;<span class="c1">// handle failure here</span></div><div class='line' id='LC21'><span class="p">});</span></div><div class='line' id='LC22'><br/></div><div class='line' id='LC23'><span class="c1">// execute the command and send this data with it</span></div><div class='line' id='LC24'><span class="nx">signForm</span><span class="p">.</span><span class="nx">execute</span><span class="p">(</span><span class="nx">myData</span><span class="p">);</span></div></pre></div>
          </div>

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

<p>Looking at this in comparison to the original $.ajax calls, there are a few less lines of code. Since I have a thin wrapper around the ajax calls and setup, I can make a few assumptions about the calls and provide a little bit of default configuration. </p>
<p>I can also move the registration of commands somewhere other than the execution of the commands, which is a huge win for readability and organization of the code when it comes time to execute a command.</p>
<p>Of course the handling of events for success, error and completed doesn&#8217;t give me any advantage over $.ajax and deferred objects. But the big benefit here and why I don&#8217;t mind the thin wraper around the done/fail/always deferred methods, is the semantic value of executing a named command vs configuring an ajax call.</p>
<p>And before you go in to a &#8220;semantics are not important&#8221; lapse of sanity, just think a little about this line that I once heard from <a href="https://twitter.com/#!/scichelli">Sharon Cichelli</a>:</p>
<blockquote>
<p>&#8220;<em>Semantics will continue to be important until we learn to communicate in something other than language.</em>&#8220;</p>
</blockquote>
<h2>Implementing The Command Structure</h2>
<p>This implementation relies on Backbone, Underscore and jQuery, so it&#8217;s really only useful in Backbone applications at this point. It&#8217;s also important to note that this code is not unit tested at all. I&#8217;m using it in my current client application and it is functional for me, but I don&#8217;t recommend taking this code as a great implementation, yet. I have plans on turning this into a real plugin, with full testing around it. But like most of my plugins and libraries, it started out as a quick hack in order to get something cleaned up in one of my applications.</p>
<div id="gist-2595175" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">// Backbone.AjaxCommands</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="nx">Backbone</span><span class="p">.</span><span class="nx">AjaxCommands</span> <span class="o">=</span> <span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">Backbone</span><span class="p">,</span> <span class="nx">$</span><span class="p">,</span> <span class="nx">_</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">Commands</span> <span class="o">=</span> <span class="p">{};</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Private data</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// ------------</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">commandList</span> <span class="o">=</span> <span class="p">{};</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Public API</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// ----------</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">Commands</span><span class="p">.</span><span class="nx">register</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">commandName</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">commandList</span><span class="p">[</span><span class="nx">commandName</span><span class="p">]</span> <span class="o">=</span> <span class="nx">options</span><span class="p">;</span></div><div class='line' id='LC17'>&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="nx">Commands</span><span class="p">.</span><span class="nx">get</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">commandName</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">options</span> <span class="o">=</span> <span class="nx">commandList</span><span class="p">[</span><span class="nx">commandName</span><span class="p">];</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{};</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">options</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">options</span><span class="p">);</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">command</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Commands</span><span class="p">.</span><span class="nx">Command</span><span class="p">(</span><span class="nx">commandName</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">command</span><span class="p">;</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">};</span></div><div class='line' id='LC26'><br/></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Command Type</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// -------------------</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">Commands</span><span class="p">.</span><span class="nx">Command</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">name</span><span class="p">;</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">};</span></div><div class='line' id='LC34'><br/></div><div class='line' id='LC35'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span><span class="nx">Commands</span><span class="p">.</span><span class="nx">Command</span><span class="p">.</span><span class="nx">prototype</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Events</span><span class="p">,</span> <span class="p">{</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">execute</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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='LC38'><br/></div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">config</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">getAjaxConfig</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">options</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span></div><div class='line' id='LC40'><br/></div><div class='line' id='LC41'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s2">&quot;before:execute&quot;</span><span class="p">);</span></div><div class='line' id='LC42'><br/></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">request</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">ajax</span><span class="p">(</span><span class="nx">config</span><span class="p">);</span></div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">request</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">response</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC45'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">that</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s2">&quot;success&quot;</span><span class="p">,</span> <span class="nx">response</span><span class="p">);</span></div><div class='line' id='LC46'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC47'><br/></div><div class='line' id='LC48'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">request</span><span class="p">.</span><span class="nx">fail</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">response</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">that</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s2">&quot;error&quot;</span><span class="p">,</span> <span class="nx">response</span><span class="p">);</span></div><div class='line' id='LC50'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC51'><br/></div><div class='line' id='LC52'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">request</span><span class="p">.</span><span class="nx">always</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">response</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC53'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">that</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s2">&quot;complete&quot;</span><span class="p">,</span> <span class="nx">response</span><span class="p">);</span></div><div class='line' id='LC54'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC55'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC56'><br/></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">getAjaxConfig</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC58'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">getUrl</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span></div><div class='line' id='LC59'><br/></div><div class='line' id='LC60'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">ajaxConfig</span> <span class="o">=</span> <span class="p">{</span></div><div class='line' id='LC61'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">type</span><span class="o">:</span> <span class="s2">&quot;GET&quot;</span><span class="p">,</span></div><div class='line' id='LC62'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">dataType</span><span class="o">:</span> <span class="s2">&quot;JSON&quot;</span><span class="p">,</span></div><div class='line' id='LC63'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">url</span><span class="o">:</span> <span class="nx">url</span></div><div class='line' id='LC64'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">};</span></div><div class='line' id='LC65'><br/></div><div class='line' id='LC66'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span><span class="nx">ajaxConfig</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span></div><div class='line' id='LC67'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">ajaxConfig</span><span class="p">.</span><span class="nx">data</span> <span class="o">=</span> <span class="nx">data</span><span class="p">;</span></div><div class='line' id='LC68'><br/></div><div class='line' id='LC69'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">ajaxConfig</span><span class="p">;</span></div><div class='line' id='LC70'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC71'><br/></div><div class='line' id='LC72'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">getUrl</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC73'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">options</span><span class="p">.</span><span class="nx">url</span><span class="p">;</span></div><div class='line' id='LC74'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC75'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC76'><br/></div><div class='line' id='LC77'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">Commands</span><span class="p">;</span></div><div class='line' id='LC78'><span class="p">})(</span><span class="nx">Backbone</span><span class="p">,</span> <span class="nx">$</span><span class="p">,</span> <span class="nx">_</span><span class="p">);</span></div></pre></div>
          </div>

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

<p>There ar ea couple of things you&#8217;ll want to note in this code:</p>
<p>The call to &#8220;register&#8221; a command stores the configuration that you provide. The call to &#8220;get&#8221; a command creates a new command instance and hands it back to you. This is done so that you can have single-use command objects and not have to worry about unbinding events from the command after it has completed. If I held on to a single command instance and just handed that back to you, you would be responsible for unbinding the event handlers after executing the command. </p>
<p>The wrapper events for &#8220;success&#8221;, &#8220;error&#8221;, and &#8220;complete&#8221; also don&#8217;t guarantee to fire if you subscribe to them after the command has been executed. So, it&#8217;s important to set up your event handlers before calling .execute on the command object.</p>
<p>I&#8217;ve also extracted the &#8220;getUrl&#8221; method specifically because I need to provide a very custom implementation of this method in my client&#8217;s application. I have another plugin that I&#8217;m looking at writing, which handles storage and retrieval of URLs in my Backbone apps, including :token replacement in the urls … but that&#8217;s another blog post and another plugin to write.</p>
<h2>A Timed Polling Mechanism</h2>
<p>One of the more interesting things I&#8217;m doing with this is using it as a timed polling mechanism. I set up a command and then I wrap the commands &#8220;execute&#8221; function in another method that I can call from a setTimeout. When the command executes, I check the response. If I find the data I need, I move on. If I don&#8217;t find the data I need, I call the wrapper function again, inside of a setTimeout.</p>
<div id="gist-2595175" 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">myCommand</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">AjaxCommands</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s2">&quot;myCommand&quot;</span><span class="p">);</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kd">var</span> <span class="nx">executeCommand</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nx">myCommand</span><span class="p">.</span><span class="nx">execute</span><span class="p">(</span><span class="nx">someData</span><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">myCommand</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;success&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">response</span><span class="p">){</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="nx">response</span><span class="p">.</span><span class="nx">someValueImChecking</span><span class="p">){</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// move on to the next thing, here</span></div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="p">}</span> <span class="k">else</span> <span class="p">{</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// poll again, 1 second from now</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">setTimeout</span><span class="p">(</span><span class="nx">executeCommand</span><span class="p">,</span> <span class="mi">1000</span><span class="p">);</span></div><div class='line' id='LC13'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC14'><span class="p">});</span></div></pre></div>
          </div>

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

<p>There&#8217;s lot of fun little tricks you can do with a simple object wrapper like this, that would be a little more cumbersome and awkward when directly using $.ajax. </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/DerickBailey?a=UPAozqmEBc0:kvmkmxrJN9Q:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=UPAozqmEBc0:kvmkmxrJN9Q:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=UPAozqmEBc0:kvmkmxrJN9Q:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=UPAozqmEBc0:kvmkmxrJN9Q:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=UPAozqmEBc0:kvmkmxrJN9Q:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=UPAozqmEBc0:kvmkmxrJN9Q:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=UPAozqmEBc0:kvmkmxrJN9Q:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=UPAozqmEBc0:kvmkmxrJN9Q:bcOpcFrp8Mo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=bcOpcFrp8Mo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/DerickBailey/~4/UPAozqmEBc0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/derickbailey/2012/05/04/wrapping-ajax-in-a-thin-command-framework-for-backbone-apps/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://lostechies.com/derickbailey/2012/05/04/wrapping-ajax-in-a-thin-command-framework-for-backbone-apps/</feedburner:origLink></item>
		<item>
		<title>View Helpers For Underscore Templates</title>
		<link>http://feedproxy.google.com/~r/DerickBailey/~3/yOsRcUZLXoQ/</link>
		<comments>http://lostechies.com/derickbailey/2012/04/26/view-helpers-for-underscore-templates/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 12:50:55 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Marionette]]></category>
		<category><![CDATA[Underscore]]></category>

		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=912</guid>
		<description>Underscore&amp;#8217;s template engine lets you run any arbitrary JavaScript code that you want, within a template. You could write an entire JavaScript application within an underscore template if you want. But this is a really bad idea. Templates should be&amp;#160;&amp;#8230; &lt;a href="http://lostechies.com/derickbailey/2012/04/26/view-helpers-for-underscore-templates/"&gt;Continue&amp;#160;reading&amp;#160;&lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt;&lt;/a&gt;</description>
			<content:encoded><![CDATA[<p>Underscore&#8217;s template engine lets you run any arbitrary JavaScript code that you want, within a template. You could write an entire JavaScript application within an underscore template if you want. But this is a really bad idea. Templates should be clean and simple. Some people go so far as to say &#8220;logic-less templates&#8221;, but I honestly don&#8217;t think that&#8217;s reasonable. I also don&#8217;t think it&#8217;s reasonable or responsible to have a bunch of JavaScript code littered throughout the template, though. </p>
<p>Fortunately, it&#8217;s easy to build helper methods for <a href="http://underscorejs.org/">Underscore</a> <a href="http://underscorejs.org/#template">templates</a> (and they end up looking a lot like Rails view helpers).</p>
<h2>Logic In Templates: A Bad Idea</h2>
<p>On a client project recently, I had a JavaScript object that needed to be bound to my template. The object looked somewhat like this:</p>
<div id="gist-2489539" 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">data</span> <span class="o">=</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nx">type</span><span class="o">:</span> <span class="s2">&quot;something&quot;</span><span class="p">,</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;whatever&quot;</span><span class="p">,</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nx">addresses</span><span class="o">:</span> <span class="p">[</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">type</span><span class="o">:</span> <span class="s2">&quot;read_address&quot;</span><span class="p">,</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">address</span><span class="o">:</span> <span class="s2">&quot;1/2/3&quot;</span><span class="p">,</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">value</span><span class="o">:</span> <span class="s2">&quot;10&quot;</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="p">{</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">type</span><span class="o">:</span> <span class="s2">&quot;write_address&quot;</span><span class="p">,</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">address</span><span class="o">:</span> <span class="s2">&quot;1/2/4&quot;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC14'>&nbsp;&nbsp;<span class="p">]</span></div><div class='line' id='LC15'><span class="p">}</span></div></pre></div>
          </div>

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

<p>I needed to take all of the &#8220;addresses&#8221; and find each specific address within the array, by name, so that I could get the right information in place at the right time in my template.</p>
<p>The first pass of code that I wrote to do this, looked like this:</p>
<div id="gist-2489539" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;script </span><span class="na">id=</span><span class="s">&quot;my-template&quot;</span> <span class="na">type=</span><span class="s">&quot;text/template&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">type</span><span class="o">:</span> <span class="o">&lt;%=</span> <span class="nx">type</span> <span class="o">%&gt;&lt;</span><span class="err">/p&gt;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">name</span><span class="o">:</span> <span class="o">&lt;%=</span> <span class="nx">name</span> <span class="o">%&gt;&lt;</span><span class="err">/p&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">read</span> <span class="nx">address</span><span class="o">:</span> <span class="o">&lt;%=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">addresses</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">addr</span><span class="p">){</span> <span class="k">return</span> <span class="nx">addr</span><span class="p">.</span><span class="nx">type</span> <span class="o">==</span> <span class="s2">&quot;read_address&quot;</span> <span class="p">}).</span><span class="nx">address</span> <span class="o">%&gt;</span> <span class="o">&lt;</span><span class="err">/p&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">read</span> <span class="nx">address</span> <span class="nx">value</span><span class="o">:</span> <span class="o">&lt;%=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">addresses</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">addr</span><span class="p">){</span> <span class="k">return</span> <span class="nx">addr</span><span class="p">.</span><span class="nx">type</span> <span class="o">==</span> <span class="s2">&quot;read_address&quot;</span> <span class="p">}).</span><span class="nx">value</span> <span class="o">%&gt;</span> <span class="o">&lt;</span><span class="err">/p&gt;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">write</span> <span class="nx">address</span><span class="o">:</span> <span class="o">&lt;%=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">addresses</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">addr</span><span class="p">){</span> <span class="k">return</span> <span class="nx">addr</span><span class="p">.</span><span class="nx">type</span> <span class="o">==</span> <span class="s2">&quot;write_address&quot;</span> <span class="p">}).</span><span class="nx">address</span> <span class="o">%&gt;</span> <span class="o">&lt;</span><span class="err">/p&gt;</span></div><div class='line' id='LC7'><span class="nt">&lt;/script&gt;</span></div></pre></div>
          </div>

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

<p>And this worked, but it&#8217;s a bad idea. I have a lot of duplication in this template and I have a lot of logic and processing of data in the template, too. This is going to make for a very painful setup when it comes to maintenance &#8211; especially if / when I have to change how the address is looked up.</p>
<h2>View Helper Methods: A Better Idea</h2>
<p>In asking around on twitter for some suggestions on how to clean this up, I received several responses. Out of everything that everyone said, though, I liked the response I got from <a href="https://twitter.com/#!/davetheninja">DaveTheNinja</a> because of it&#8217;s simplicity.</p>
<p>The basic idea is to recognize that the data I send to my template is a JavaScript object. Since this is an object, it can have methods. If I can have methods on my object, why not put methods on it as view helper methods?</p>
<div id="gist-2489539" 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">viewHelpers</span> <span class="o">=</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nx">getAddress</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">type</span><span class="p">){</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">address</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">addresses</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">addr</span><span class="p">)</span> <span class="p">{</span> </div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">addr</span><span class="p">.</span><span class="nx">type</span> <span class="o">==</span> <span class="nx">type</span> </div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">address</span><span class="p">;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<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'><span class="nx">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="nx">viewHelpers</span><span class="p">);</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="nx">_</span><span class="p">.</span><span class="nx">template</span><span class="p">(</span><span class="nx">myTemplate</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span></div></pre></div>
          </div>

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

<p>The helper method &#8211; &#8220;getAddress&#8221; &#8211; is pretty much the same code. But now it&#8217;s consolidated in to a single location and I&#8217;m able to give better formatting for it which makes it easier to read and understand.</p>
<p>You can also see that I&#8217;ve kept the data separate from the objects until just before rendering the template. I&#8217;m using Underscore&#8217;s &#8220;extend&#8221; method to copy all of the methods from my &#8220;viewHelpers&#8221; object on to my data object.</p>
<p>Then when I pass the data object to my template, I can call my &#8220;getAddress&#8221; method like this:</p>
<div id="gist-2489539" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;script </span><span class="na">id=</span><span class="s">&quot;my-template&quot;</span> <span class="na">type=</span><span class="s">&quot;text/template&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">type</span><span class="o">:</span> <span class="o">&lt;%=</span> <span class="nx">type</span> <span class="o">%&gt;&lt;</span><span class="err">/p&gt;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">name</span><span class="o">:</span> <span class="o">&lt;%=</span> <span class="nx">name</span> <span class="o">%&gt;&lt;</span><span class="err">/p&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">read</span> <span class="nx">address</span><span class="o">:</span> <span class="o">&lt;%=</span> <span class="nx">getAddress</span><span class="p">(</span><span class="s2">&quot;read_address&quot;</span><span class="p">).</span><span class="nx">address</span> <span class="o">%&gt;</span> <span class="o">&lt;</span><span class="err">/p&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">read</span> <span class="nx">address</span> <span class="nx">value</span><span class="o">:</span> <span class="o">&lt;%=</span> <span class="nx">getAddress</span><span class="p">(</span><span class="s2">&quot;read_address&quot;</span><span class="p">).</span><span class="nx">value</span> <span class="o">%&gt;</span> <span class="o">&lt;</span><span class="err">/p&gt;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">write</span> <span class="nx">address</span><span class="o">:</span> <span class="o">&lt;%=</span> <span class="nx">getAddress</span><span class="p">(</span><span class="s2">&quot;write_address&quot;</span><span class="p">).</span><span class="nx">address</span> <span class="o">%&gt;</span> <span class="o">&lt;</span><span class="err">/p&gt;</span></div><div class='line' id='LC7'><span class="nt">&lt;/script&gt;</span></div></pre></div>
          </div>

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

<p>And we&#8217;re done! I now have view helper methods that I can call from within my templates.</p>
<h2>Using This With Backbone</h2>
<p>I use this technique with Backbone a lot. The code that I showed above is really no different than what I do with my Backbone views, either. When you get to the point where you need to render your template, just extend the view helper methods on to your data before doing the actual rendering:</p>
<div id="gist-2489539" 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="s2">&quot;#my-template&quot;</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'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">data</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">toJSON</span><span class="p">();</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="nx">viewHelpers</span><span class="p">);</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&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">$</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="nx">data</span><span class="p">);</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</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='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;<span class="p">}</span></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/2489539/977a299ae934ccceee2c294fb1dbdab7cbb59d07/5.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2489539#file_5.js" style="float:right;margin-right:10px;color:#666">5.js</a>
            <a href="https://gist.github.com/2489539">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Using This With Backbone.Marionette</h2>
<p>I&#8217;ve added this functionality in to <a href="https://github.com/derickbailey/backbone.marionette">Backbone.Marionette</a> as well. ItemView, CompositeView and Layout will all look for a `templateHelpers` attribute on a view definition. If it finds it, it will mix that object in to the data that is used to render the template: </p>
<div id="gist-2489539" 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">Marionette</span><span class="p">.</span><span class="nx">ItemView</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nx">template</span><span class="o">:</span> <span class="s2">&quot;#my-template&quot;</span><span class="p">,</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nx">templateHelpers</span><span class="o">:</span> <span class="nx">viewHelpers</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="p">});</span></div><div class='line' id='LC7'><br/></div></pre></div>
          </div>

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

<p>You can specify a reference to the viewHelpers object as shown in this example, provide an object literal as the templateHelpers directly, or specify a function that returns an object with the helper methods that you want. For more information see <a href="http://derickbailey.github.com/backbone.marionette/#marionette-view/view-templatehelpers">the documentation for Marionette</a>.</p>
<h2>Other Template Engines</h2>
<p>This basic technique will probably work with other template engines, too. Some engines like Handlebars, though, have this idea baked in to them directly. But if you&#8217;re using Underscore&#8217;s templates, then this is a good way to keep your templates clean while still providing the logic you need.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/DerickBailey?a=yOsRcUZLXoQ:MNdDMzUY23Y:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=yOsRcUZLXoQ:MNdDMzUY23Y:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=yOsRcUZLXoQ:MNdDMzUY23Y:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=yOsRcUZLXoQ:MNdDMzUY23Y:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=yOsRcUZLXoQ:MNdDMzUY23Y:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=yOsRcUZLXoQ:MNdDMzUY23Y:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=yOsRcUZLXoQ:MNdDMzUY23Y:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=yOsRcUZLXoQ:MNdDMzUY23Y:bcOpcFrp8Mo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=bcOpcFrp8Mo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/DerickBailey/~4/yOsRcUZLXoQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/derickbailey/2012/04/26/view-helpers-for-underscore-templates/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://lostechies.com/derickbailey/2012/04/26/view-helpers-for-underscore-templates/</feedburner:origLink></item>
		<item>
		<title>Executing A Project-Specific Node/NPM Package A-la “bundle exec”</title>
		<link>http://feedproxy.google.com/~r/DerickBailey/~3/WDkZFSzj9aE/</link>
		<comments>http://lostechies.com/derickbailey/2012/04/24/executing-a-project-specific-nodenpm-package-a-la-bundle-exec/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 12:52:13 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[NodeJS]]></category>
		<category><![CDATA[NPM]]></category>

		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=906</guid>
		<description>It&amp;#8217;s no secret that I love NodeJS, though I really haven&amp;#8217;t blogged about it much (if at all). Frankly, I think Node and NPM are going to be eating Ruby, RubyGems and Bundler&amp;#8217;s $25,000 lunch &amp;#8211; at least on OSX.&amp;#160;&amp;#8230; &lt;a href="http://lostechies.com/derickbailey/2012/04/24/executing-a-project-specific-nodenpm-package-a-la-bundle-exec/"&gt;Continue&amp;#160;reading&amp;#160;&lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt;&lt;/a&gt;</description>
			<content:encoded><![CDATA[<p>It&#8217;s no secret that I love NodeJS, though I really haven&#8217;t blogged about it much (if at all). Frankly, I think Node and NPM are going to be eating Ruby, RubyGems and Bundler&#8217;s $25,000 lunch &#8211; at least on OSX. But there&#8217;s at least one piece of bundler that I miss from NPM: the ability to say &#8216;bundle exec&#8217; and have it run a specific gem&#8217;s bin file (binary / executable) for the current project gem configuration and version.</p>
<p>If I run &#8220;npm install express&#8221; from within a folder, the &#8220;express&#8221; package will be installed in to my ./node_modules folder.</p>
<p><img title="Screen Shot 2012-04-17 at 6.28.41 PM.png" src="http://lostechies.com/derickbailey/files/2012/04/Screen-Shot-2012-04-17-at-6.28.41-PM.png" alt="Screen Shot 2012 04 17 at 6 28 41 PM" width="600" height="222" border="0" /></p>
<p>But I can&#8217;t say &#8220;express foo&#8221; on the command line at this point, to run express. And I can&#8217;t say &#8220;npm express foo&#8221; or &#8220;npm exec express foo&#8221; or anything like that. This ability is sadly missing from NPM. There are a few ways to get around this, though.</p>
<h2>Global Modules</h2>
<p>When you install a Node module with the `-g` option, it installs in to the global module list. Global modules can have their bin files run from the command line just like any other command. So if you install the &#8220;express&#8221; module, for example, with the global flag then you&#8217;ll be able to say &#8220;express foo&#8221; from the command line and watch it build a fresh new project for you in the &#8220;foo&#8221; folder.</p>
<p><img title="Screen Shot 2012-04-17 at 6.30.23 PM.png" src="http://lostechies.com/derickbailey/files/2012/04/Screen-Shot-2012-04-17-at-6.30.23-PM.png" alt="Screen Shot 2012 04 17 at 6 30 23 PM" width="456" height="600" border="0" /></p>
<p>This is nice for packages that you want to have available for any application you&#8217;re building. I use global packages a lot because some of the tools that you can find in node packages are quite useful, even outside the context of a node project. But not every package should be installed in the global package repository for your computer.</p>
<h2>Run Bin From Relative Path</h2>
<p>You can also run bin files from node modules by using the relative path of ./node_modules/.bin/ to find the command.</p>
<p><img title="Screen Shot 2012-04-17 at 6.32.44 PM.png" src="http://lostechies.com/derickbailey/files/2012/04/Screen-Shot-2012-04-17-at-6.32.44-PM.png" alt="Screen Shot 2012 04 17 at 6 32 44 PM" width="442" height="125" border="0" /></p>
<p>This is a bit tedious, though it is functional. I&#8217;ve build a few simple command / shell scripts to execute some of my installed modules this way, for some of my projects. That makes it a bit easier overall, but you have to set up a shell script for each package you want.</p>
<h2>Run Bin From Generic Shell Script</h2>
<p>Since all of the bin files for all of the node packages are installed in to ./node_modules/.bin/ for a given project, why not build a generic &#8220;npm_exec&#8221; shell script and just forward all the parameters?</p>
<p>Easy enough:</p>
<div id="gist-2409904" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>./node_modules/.bin/<span class="nv">$@</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2409904/10d0fa0fece1b941cc24bb4e98a2cc014c9dbd8d/npm_exec.sh" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2409904#file_npm_exec.sh" style="float:right;margin-right:10px;color:#666">npm_exec.sh</a>
            <a href="https://gist.github.com/2409904">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Just &#8220;chmod +x npm_exec&#8221; to make this file executable, and away we go.</p>
<p>Now I can run any arbitrary package binary I want. I could even put this little shell script in my /usr/local/bin folder and it would be available for all of my node projects, anywhere on my box.</p>
<p><img title="Screen Shot 2012-04-17 at 6.35.03 PM.png" src="http://lostechies.com/derickbailey/files/2012/04/Screen-Shot-2012-04-17-at-6.35.03-PM.png" alt="Screen Shot 2012 04 17 at 6 35 03 PM" width="332" height="124" border="0" /></p>
<p>This is a nice little solution, even if it&#8217;s a work around for something that should be built in to npm.</p>
<h2>NPM Scripts In package.json</h2>
<p>This last option is one that I&#8217;m using more and more often for project specific, recurring needs.</p>
<p>When you create an express app, it builds a package.json file for you. One of the pieces that it sticks in there is the &#8220;scripts&#8221; setting which contains a &#8220;start&#8221; setting, by default.</p>
<div id="gist-2409904" 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></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;application-name&quot;</span><span class="p">,</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nt">&quot;version&quot;</span><span class="p">:</span> <span class="s2">&quot;0.0.1&quot;</span><span class="p">,</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nt">&quot;private&quot;</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="nt">&quot;scripts&quot;</span><span class="p">:</span> <span class="p">{</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&quot;start&quot;</span><span class="p">:</span> <span class="s2">&quot;start app&quot;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="nt">&quot;dependencies&quot;</span><span class="p">:</span> <span class="p">{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&quot;express&quot;</span><span class="p">:</span> <span class="s2">&quot;3.0.0alpha1&quot;</span><span class="p">,</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&quot;jade&quot;</span><span class="p">:</span> <span class="s2">&quot;*&quot;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC12'><span class="p">}</span></div><div class='line' id='LC13'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2409904/0b91aca167f7265fa87142819a3a8c133b216be1/package.json" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2409904#file_package.json" style="float:right;margin-right:10px;color:#666">package.json</a>
            <a href="https://gist.github.com/2409904">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>It turns out you can use these &#8220;scripts&#8221; from the npm command line. When you call &#8220;npm start&#8221;, npm will execute the &#8220;scripts&#8221;/&#8221;start&#8221; configuration for you. By default, this is just a call to &#8220;node app&#8221; which runs the app.js file in node and gets your express.js app up and running.</p>
<p>You can do more with &#8220;scripts&#8221;, too. You can add your own named script, in fact.</p>
<div id="gist-2409904" 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></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="err">...</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nt">&quot;scripts&quot;</span><span class="p">:</span> <span class="p">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&quot;start&quot;</span><span class="p">:</span> <span class="s2">&quot;node app&quot;</span><span class="p">,</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&quot;foo&quot;</span><span class="p">:</span> <span class="s2">&quot;node -e &#39;console.log(1+2)&#39;&quot;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&nbsp;&nbsp;<span class="err">...</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/2409904/466d0c3f0f63050a961e39f2077fd1322249b45f/scripts.json" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2409904#file_scripts.json" style="float:right;margin-right:10px;color:#666">scripts.json</a>
            <a href="https://gist.github.com/2409904">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>But you can&#8217;t just &#8220;npm foo&#8221;. The &#8220;start&#8221; script is recognized by npm explicitly. For other non-standard script names, you have to use the &#8220;run-script&#8221; command from node: npm run-script foo</p>
<p><img title="Screen Shot 2012-04-17 at 6.38.18 PM.png" src="http://lostechies.com/derickbailey/files/2012/04/Screen-Shot-2012-04-17-at-6.38.18-PM.png" alt="Screen Shot 2012 04 17 at 6 38 18 PM" width="505" height="195" border="0" /></p>
<p>The other thing that this does for us, is give us direct access to all of the bin files that our node packages have installed. So within a &#8220;script&#8221; configuration, we can call any arbitrary package&#8217;s bin file.</p>
<p>For example, if I want to use the node-supervisor package to restart my app whenever any files change, I can set up my package.json file to look like this:</p>
<div id="gist-2409904" 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></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;application-name&quot;</span><span class="p">,</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nt">&quot;version&quot;</span><span class="p">:</span> <span class="s2">&quot;0.0.1&quot;</span><span class="p">,</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nt">&quot;private&quot;</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="nt">&quot;scripts&quot;</span><span class="p">:</span> <span class="p">{</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&quot;start&quot;</span><span class="p">:</span> <span class="s2">&quot;supervisor app&quot;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="nt">&quot;dependencies&quot;</span><span class="p">:</span> <span class="p">{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&quot;express&quot;</span><span class="p">:</span> <span class="s2">&quot;3.0.0alpha1&quot;</span><span class="p">,</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&quot;jade&quot;</span><span class="p">:</span> <span class="s2">&quot;*&quot;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC12'>&nbsp;&nbsp;<span class="nt">&quot;devDependencies&quot;</span><span class="p">:</span> <span class="p">{</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&quot;supervisor&quot;</span><span class="p">:</span> <span class="s2">&quot;*&quot;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC15'><span class="p">}</span></div><div class='line' id='LC16'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2409904/714c9092686c825f7b0a83f55f3df94d43eb59f1/supervisor.json" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2409904#file_supervisor.json" style="float:right;margin-right:10px;color:#666">supervisor.json</a>
            <a href="https://gist.github.com/2409904">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This will install the &#8220;supervisor&#8221; package for development only, and set up the &#8220;start&#8221; script to run &#8220;supervisor app&#8221;. Now from the command line, I can&#8217;t run &#8220;supervisor app&#8221; directly:</p>
<p><img title="Screen Shot 2012-04-17 at 6.40.56 PM.png" src="http://lostechies.com/derickbailey/files/2012/04/Screen-Shot-2012-04-17-at-6.40.56-PM.png" alt="Screen Shot 2012 04 17 at 6 40 56 PM" width="448" height="60" border="0" /></p>
<p>But I can run &#8220;node start&#8221; and node will pick up the ./node_modules/.bin/ folder for me, allowing supervisor to be executed:</p>
<p><img title="Screen Shot 2012-04-17 at 6.41.49 PM.png" src="http://lostechies.com/derickbailey/files/2012/04/Screen-Shot-2012-04-17-at-6.41.49-PM.png" alt="Screen Shot 2012 04 17 at 6 41 49 PM" width="542" height="495" border="0" /></p>
<p>This works well for recurring / repetitive tasks within a project. But if you want ad-hoc package commands, you&#8217;re going to be in for a little more work and will likely have to fall back to one of the other solutions I&#8217;ve mentioned.</p>
<h2>Other Options?</h2>
<p>I really do miss the &#8220;bundle exec&#8221; feature of bundler. I honestly don&#8217;t know why npm doesn&#8217;t have an equivalent built in to it. Maybe it just needs someone to come along and contribute this feature. Maybe this feature was rejected. I don&#8217;t know. I haven&#8217;t looked in to it. But I wanted to get something in place now, so that I don&#8217;t have to install all of my project specific packages in the global package list.</p>
<p>There might be better option than the ones I&#8217;ve listed, as well. So, what am I missing? Is there something built in to npm to make this easier? Are there other ways that you&#8217;ve worked around this? Or am I going to stick with my &#8220;npm_exec&#8221; script and using npm &#8220;scripts&#8221; in my package.json fil?</p>
<p> </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/DerickBailey?a=WDkZFSzj9aE:LeC2LCOnJ6A:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=WDkZFSzj9aE:LeC2LCOnJ6A:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=WDkZFSzj9aE:LeC2LCOnJ6A:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=WDkZFSzj9aE:LeC2LCOnJ6A:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=WDkZFSzj9aE:LeC2LCOnJ6A:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=WDkZFSzj9aE:LeC2LCOnJ6A:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=WDkZFSzj9aE:LeC2LCOnJ6A:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=WDkZFSzj9aE:LeC2LCOnJ6A:bcOpcFrp8Mo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=bcOpcFrp8Mo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/DerickBailey/~4/WDkZFSzj9aE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/derickbailey/2012/04/24/executing-a-project-specific-nodenpm-package-a-la-bundle-exec/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://lostechies.com/derickbailey/2012/04/24/executing-a-project-specific-nodenpm-package-a-la-bundle-exec/</feedburner:origLink></item>
		<item>
		<title>WatchMeCode: Using LiveReload In Web Development</title>
		<link>http://feedproxy.google.com/~r/DerickBailey/~3/MaoAhTkFqaE/</link>
		<comments>http://lostechies.com/derickbailey/2012/04/23/watchmecode-using-livereload-in-web-development/#comments</comments>
		<pubDate>Mon, 23 Apr 2012 12:38:59 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Screencast]]></category>
		<category><![CDATA[WatchMeCode]]></category>
		<category><![CDATA[Web Sockets]]></category>

		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=908</guid>
		<description>From all of the screencasts that I&amp;#8217;ve produced at WatchMeCode so far, the number one question that I get is: What tool do you use to refresh your browser when you change a file in your editor? It&amp;#8217;s LiveReload, and&amp;#160;&amp;#8230; &lt;a href="http://lostechies.com/derickbailey/2012/04/23/watchmecode-using-livereload-in-web-development/"&gt;Continue&amp;#160;reading&amp;#160;&lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt;&lt;/a&gt;</description>
			<content:encoded><![CDATA[<p>From all of the screencasts that I&#8217;ve produced at <a href="http://www.watchmecode.net/">WatchMeCode</a> so far, the number one question that I get is:</p>
<blockquote>
<p><em>What tool do you use to refresh your browser when you change a file in your editor?</em></p>
</blockquote>
<p>It&#8217;s <a href="http://livereload.com/">LiveReload</a>, and I&#8217;ve put together <a href="http://www.watchmecode.net/livereload">a 10 minute introduction</a> that shows how to get up and running with the GUI application and the Guard plugin, how to use it to compile your assets such as SASS or CoffeeScript, and more.</p>
<p><iframe src="http://www.youtube-nocookie.com/embed/uLh2rmSpa48?rel=0" width="560" height="315" frameborder="0"></iframe></p>
<p>Personally, I can&#8217;t live without this tool anymore. It takes the &#8220;refresh&#8221; button out of my development effort and greatly simplifies my workflow.</p>
<p>Be sure to checkout my other screencasts (paid and free) at <a href="http://www.watchmecode.net/">WatchMeCode.net</a> as well.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/DerickBailey?a=MaoAhTkFqaE:9O9RH11Dgzo:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=MaoAhTkFqaE:9O9RH11Dgzo:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=MaoAhTkFqaE:9O9RH11Dgzo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=MaoAhTkFqaE:9O9RH11Dgzo:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=MaoAhTkFqaE:9O9RH11Dgzo:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=MaoAhTkFqaE:9O9RH11Dgzo:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=MaoAhTkFqaE:9O9RH11Dgzo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=MaoAhTkFqaE:9O9RH11Dgzo:bcOpcFrp8Mo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=bcOpcFrp8Mo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/DerickBailey/~4/MaoAhTkFqaE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/derickbailey/2012/04/23/watchmecode-using-livereload-in-web-development/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://lostechies.com/derickbailey/2012/04/23/watchmecode-using-livereload-in-web-development/</feedburner:origLink></item>
		<item>
		<title>Decoupling Backbone Apps From WebSockets</title>
		<link>http://feedproxy.google.com/~r/DerickBailey/~3/aIjMpdTn__c/</link>
		<comments>http://lostechies.com/derickbailey/2012/04/19/decoupling-backbone-apps-from-websockets/#comments</comments>
		<pubDate>Thu, 19 Apr 2012 12:48:11 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Marionette]]></category>
		<category><![CDATA[Pusher]]></category>
		<category><![CDATA[SignalR]]></category>
		<category><![CDATA[Socket.IO]]></category>
		<category><![CDATA[Web Sockets]]></category>

		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=897</guid>
		<description>I&amp;#8217;ve been doing a lot of work with web sockets lately, in my Backbone applications. And I&amp;#8217;ve fallen in to a pattern that I really like, where my Backbone application doesn&amp;#8217;t actually know anything about web sockets. All it knows&amp;#160;&amp;#8230; &lt;a href="http://lostechies.com/derickbailey/2012/04/19/decoupling-backbone-apps-from-websockets/"&gt;Continue&amp;#160;reading&amp;#160;&lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt;&lt;/a&gt;</description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a lot of work with web sockets lately, in my Backbone applications. And I&#8217;ve fallen in to a pattern that I really like, where my Backbone application doesn&#8217;t actually know anything about web sockets. All it knows is that I&#8217;m using an event aggregator (like I always do) and that it publishes and subscribes to various events. The web sockets, then, sit off to the side. They&#8217;re decoupled from my actual application and I can replace the socket implementation without having to worry about my app&#8217;s functionality.</p>
<h2>A Web Socket Library</h2>
<p>Now you could write your own web sockets implementation. But I don&#8217;t recommend this. It&#8217;s notoriously difficult to get it to work across all of the browsers that you&#8217;ll have to support and frankly, not worth your time. Instead I&#8217;d recommend using an existing socket library. I&#8217;ve used these and can highly recommend all of them:</p>
<ul>
<li><a href="http://socket.io/">Socket.IO</a>: The defacto standard for web sockets libraries. It primarily runs on NodeJS, but there are back-end servers for several other languages available as well. It falls back to long-polling, AJAX, and several other implementations that work in nearly every browser.</li>
<li><a href="http://signalr.net/">SignalR</a>: A long-polling, web sockets, AJAX adaptive system for .NET. It&#8217;s Socket.IO for .NET.</li>
<li><a href="http://pusher.com/">Pusher</a>: A third-party web sockets server. This is great for hosted sites (like Heroku) and for high scalability, fast. I learned how to use web sockets and converted a very chatty app from AJAX calls to PusherApp in less than an hour, a few years ago. There are libraries for PusherApp in <a href="http://pusher.com/docs/rest_libraries">many different development languages</a>, like Ruby, .NET, Python, etc. I&#8217;ve only used the Ruby library, but it was dirt simple. They also have an article on working with <a href="http://blog.pusher.com/2011/6/21/backbone-js-now-realtime-with-pusher/">Backbone and Pusher</a>.</li>
</ul>
<p>And yes, I realize that there are more libraries than just these. I&#8217;ve only used these, so they are the ones I&#8217;m mentioning. And I love all three of these web socket libraries. If I was forced to pick a favorite, it would be Socket.IO but only because I love working with NodeJS. But I do have an affinity for Pusher, since it&#8217;s the place that got me started with Websockets.</p>
<p>All three of these libraries are easy to use, and can easily be adapted to this example, though. Don&#8217;t feel like you&#8217;re going to make a wrong choice either. After all, the purpose of this article is to show you how the choice you&#8217;ve made doesn&#8217;t matter that much.</p>
<p><span style="font-size: 18px; font-weight: bold;">Decoupling Web Sockets</span></p>
<p>When I build apps with web sockets for communication, I don&#8217;t want my application to actually know anything about the sockets. I don&#8217;t want to mix that responsibility all throughout the code, for a number of reasons. If my need for sockets changes, for example, having many places to update the use of them would become a pain. If I want to get rid of sockets entirely or use another form of communication in addition to web sockets, having to go and add a bunch of extra code in all of the places that need the new communication would be awful. There&#8217;s also a high likelihood of code duplication, hidden problems and workflow, and hard to test code when we couple our web sockets implementation to our application code.</p>
<p>The simple solution that I&#8217;ve found for this is to abstract the web sockets away from our application. There are several ways to do this, one of which is to replace the Backbone.sync method in a Backbone app with something like <a href="https://github.com/logicalparadox/backbone.iobind">Backbone.IOBind</a>. This plugin replaces the sync method with a version that communicates over web sockets. That means your fetch, save and destroy methods would all move over web sockets instead of AJAX calls. If you&#8217;re using Socket.IO and you want to have your data access methods working this way, I recommend checking out Backbone.IOBind.</p>
<p>Another way to do this, though, is to move the web sockets code away from the application entirely. We can easily set up an adapter objects that translates between our web sockets implementation and our actual application.</p>
<h2>A SocketAdapter</h2>
<p>There are two basic things that you&#8217;ll need to build a socket adapter in the manner that I like. a web sockets setup and an <a href="http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/">event aggregator</a>. Pick a web sockets library &#8211; any of them will do. For this example, I&#8217;ll be using Socket.IO. As for an event aggregator, I&#8217;ve got my default setup with <a href="https://github.com/derickbailey/backbone.marionette">Backbone.Marionette</a> which provides one for us. Or you can pick any of a number of other pub/sub libraries for JavaScript.</p>
<p>Here&#8217;s the trick to decoupling the application code from the web sockets library: all of the events that the web socket publishes will be forwarded to the event aggregator, and your application will only pay attention to the event aggregator. For example:</p>
<div id="gist-2405897" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>&nbsp;&nbsp;<span class="nx">HIT</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'>&nbsp;&nbsp;<span class="c1">// ... other settings here</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="c1">// Socket.io Adapter</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="c1">// -----------------</span></div><div class='line' id='LC9'>&nbsp;&nbsp;</div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="c1">// This adapter forwards all socket.io events</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="c1">// to the application&#39;s event aggregator. The</span></div><div class='line' id='LC12'>&nbsp;&nbsp;<span class="c1">// intention of this is to decouple the specific</span></div><div class='line' id='LC13'>&nbsp;&nbsp;<span class="c1">// websockets implementation from the application&#39;s</span></div><div class='line' id='LC14'>&nbsp;&nbsp;<span class="c1">// use of it.</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'>&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">socket</span><span class="p">;</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">initialize</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">socket</span> <span class="o">=</span> <span class="nx">io</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="nx">HIT</span><span class="p">.</span><span class="nx">socketUrl</span><span class="p">);</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;connect&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">SocketAdapter</span><span class="p">.</span><span class="nx">connected</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">HIT</span><span class="p">.</span><span class="nx">vent</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s2">&quot;socket:connected&quot;</span><span class="p">);</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC25'><br/></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;someData&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">){</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">HIT</span><span class="p">.</span><span class="nx">vent</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s2">&quot;someData&quot;</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;disconnect&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">SocketAdapter</span><span class="p">.</span><span class="nx">connected</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">HIT</span><span class="p">.</span><span class="nx">vent</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s2">&quot;socket:disconnected&quot;</span><span class="p">);</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC34'><br/></div><div class='line' id='LC35'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;error&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">){</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;ERROR: &quot;</span><span class="p">,</span> <span class="nx">err</span><span class="p">);</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC38'><br/></div><div class='line' id='LC39'>&nbsp;&nbsp;<span class="p">};</span></div><div class='line' id='LC40'><br/></div><div class='line' id='LC41'>&nbsp;&nbsp;<span class="nx">HIT</span><span class="p">.</span><span class="nx">addInitializer</span><span class="p">(</span><span class="nx">initialize</span><span class="p">);</span></div><div class='line' id='LC42'><br/></div><div class='line' id='LC43'><br/></div><div class='line' id='LC44'>&nbsp;&nbsp;<span class="c1">// Handle the event, start the app and the socket adapter</span></div><div class='line' id='LC45'><br/></div><div class='line' id='LC46'>&nbsp;&nbsp;<span class="nx">HIT</span><span class="p">.</span><span class="nx">vent</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;someData&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">){</span></div><div class='line' id='LC47'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">alert</span><span class="p">(</span><span class="s2">&quot;some data came through!&quot;</span><span class="p">);</span></div><div class='line' id='LC48'>&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC49'><br/></div><div class='line' id='LC50'>&nbsp;&nbsp;<span class="nx">HIT</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/2405897/06c5abf2d52dd9509dcf67f09d2a27257cbd1dea/1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2405897#file_1.js" style="float:right;margin-right:10px;color:#666">1.js</a>
            <a href="https://gist.github.com/2405897">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>In this case we&#8217;re listening for an event called &#8220;someData&#8221; on the socket library. When that event fires, we&#8217;re just forwarding the event in to our application&#8217;s event aggregator. Our application listens for the necessary events from it&#8217;s event aggregator, and does it&#8217;s thing.</p>
<p>From there, you just extend the adapter to listen to your specific events, and continue to forward them on. In some cases, you can even create very generic socket event handlers so that you don&#8217;t have to constantly add new ones. In other cases, you might need to add a very specific event handler and have it do some manipulation of the data that it receives before forwarding it on to the application. No matter the case, though, you now have a single place in your application where this needs to happen.</p>
<p>Yes, it&#8217;s really that simple.</p>
<h2>But, Why?</h2>
<p>Decoupling and all that, of course. But really, it just makes our lives as developers easier. Having the web sockets and application code decoupled like this does add a little bit of overhead in both the run time execution of the code, and in the mental effort to keep track of all the moving parts. But the slicing of the individual parts down in to single responsibilities makes it easier to understand each part, and in some cases can make it easier to see the machine as a whole.</p>
<p>We&#8217;ve also made the choice of a web sockets library somewhat irrelevant. Did you pick Socket.IO and realize that you now need something hosted so that you can scale faster? No problem &#8211; just replace the socket adapter with an implementation that uses Pusher. Did you choose Pusher and now realize that you don&#8217;t want to pay for a hosted solution in your .NET system? No problem, just replace the Pusher implementation with SignalR.</p>
<p>When we&#8217;re testing our applications, we don&#8217;t have to worry about mocking or stubbing the web sockets, either. Rather than going through the pain of dealing with the web sockets API that we&#8217;ve chosen, we can simply fire off events from our application&#8217;s event aggregator and verify the results of firing those events. Removing the web sockets library from our tests will make the tests easier to work with, cleaner to read and write, and probably run a little faster too.</p>
<p>On that note, we can also simulate web sockets events in to our application. In a recent client project, for example, I had to simulate a list of devices with addresses receiving events from Socket.IO in my development environment. This project involved some hardware integration, and the only place where the hardware was available was a server in Germany. I didn&#8217;t have access to it here in Texas, except by deploying the app and running it there. So in my development environment, I used a setInterval to publish an event through my event aggregator every 1 second. The events I published contained valid but slightly random data to simulate what I would be receiving from the actual web sockets server. With this in place, I was able to write my app and deploy it to the real server with a fair amount of confidence. It worked nearly every time (except that time where we changed how the web sockets data was being pre-processed by our adapter).</p>
<h2>Only One Of Many Options</h2>
<p>There are likely a thousand or more ways that you can integrate a web sockets system in to your application. I&#8217;ve shown you how to do one of them and linked to another (Backbone.IOBind). But don&#8217;t think that you have to do it the way that I like to do it. This is my preference based on the style of event-driven architecture that I like to write. The main point that you should take away from this article is not that you can build an event aggregator adapter for web sockets, but that you should decouple your web sockets from your application in a manner that makes it easier to maintain and change both your application and the web sockets implementation, independently of each other.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/DerickBailey?a=aIjMpdTn__c:R_q78lPe-Jg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=aIjMpdTn__c:R_q78lPe-Jg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=aIjMpdTn__c:R_q78lPe-Jg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=aIjMpdTn__c:R_q78lPe-Jg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=aIjMpdTn__c:R_q78lPe-Jg:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=aIjMpdTn__c:R_q78lPe-Jg:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=aIjMpdTn__c:R_q78lPe-Jg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=aIjMpdTn__c:R_q78lPe-Jg:bcOpcFrp8Mo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=bcOpcFrp8Mo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/DerickBailey/~4/aIjMpdTn__c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/derickbailey/2012/04/19/decoupling-backbone-apps-from-websockets/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://lostechies.com/derickbailey/2012/04/19/decoupling-backbone-apps-from-websockets/</feedburner:origLink></item>
		<item>
		<title>Managing A Modal Dialog With Backbone And Marionette</title>
		<link>http://feedproxy.google.com/~r/DerickBailey/~3/ZzN2DdCOVXA/</link>
		<comments>http://lostechies.com/derickbailey/2012/04/17/managing-a-modal-dialog-with-backbone-and-marionette/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 13:02:21 +0000</pubDate>
		<dc:creator>Derick Bailey</dc:creator>
				<category><![CDATA[Backbone]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Marionette]]></category>
		<category><![CDATA[Twitter Bootstrap]]></category>

		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=891</guid>
		<description>My current client project is using Twitter Bootstrap, and I have to say I&amp;#8217;m loving it. It provides a simple way to get started with common layout and design elements, and includes a handful of UI widgets and features that&amp;#160;&amp;#8230; &lt;a href="http://lostechies.com/derickbailey/2012/04/17/managing-a-modal-dialog-with-backbone-and-marionette/"&gt;Continue&amp;#160;reading&amp;#160;&lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt;&lt;/a&gt;</description>
			<content:encoded><![CDATA[<p>My current client project is using <a href="http://twitter.github.com/bootstrap/">Twitter Bootstrap</a>, and I have to say I&#8217;m loving it. It provides a simple way to get started with common layout and design elements, and includes a handful of UI widgets and features that are easy to use as jQuery plugins. But enough about that… I want to manage a modal dialog that displays my Backbone view, which is a notoriously frustrating problem no matter what UI framework you&#8217;re using, it seems.</p>
<h2>The Problem: Moving DOM Elements</h2>
<p>The core of the problem that people run in to when using a modal dialog is that the modal plugin removes the DOM element that wraps the modal, from the DOM. It usually gets added to some special holding location where the modal plugin can guarantee that the element won&#8217;t be visible until the modal dialog is opened. I&#8217;m over-generalizing this a bit, but many of the modal dialogs work this way or in a similar manner.</p>
<p>The problem that this usually causes is that a Backbone view will lose it&#8217;s event handling when the DOM element gets moved around by the modal dialog. When the modal dialog removes the view&#8217;s `el` from the DOM, the `events` configuration is lost because the DOM element has been moved or removed from the DOM and jQuery had to let go of the events. When the `el` is re-added to the DOM for displaying it as a modal, then, the `events` are not re-attached.</p>
<p>This is usually caused by a developer using the Backbone view itself as the modal dialog &#8211; which is not what you want to do.</p>
<h2>The Solution: Don&#8217;t Modal A Backbone View</h2>
<p>No, really. That&#8217;s the solution.</p>
<p>I&#8217;m not saying you can&#8217;t use a Backbone view in a modal. I&#8217;m saying you shouldn&#8217;t try to modal the view itself. Instead, you should create a simple wrapper div for the modal plugin to use and then populate the contents of that div with the view&#8217;s `el`.</p>
<p>Let&#8217;s see an example of a Twitter Bootstrap modal dialog, displaying a Backbone view. First, the HTML and Underscore template:</p>
<div id="gist-2359717" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;script </span><span class="na">id=</span><span class="s">&quot;modal-view-template&quot;</span> <span class="na">type=</span><span class="s">&quot;text/html&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">div</span> <span class="kr">class</span><span class="o">=</span><span class="s2">&quot;modal-header&quot;</span><span class="o">&gt;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">h2</span><span class="o">&gt;</span><span class="nx">This</span> <span class="nx">is</span> <span class="nx">a</span> <span class="nx">modal</span><span class="o">!&lt;</span><span class="err">/h2&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="err">/div&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">div</span> <span class="kr">class</span><span class="o">=</span><span class="s2">&quot;modal-body&quot;</span><span class="o">&gt;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span><span class="nx">With</span> <span class="nx">some</span> <span class="nx">content</span> <span class="k">in</span> <span class="nx">it</span><span class="o">!&lt;</span><span class="err">/p&gt;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="err">/div&gt;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">div</span> <span class="kr">class</span><span class="o">=</span><span class="s2">&quot;modal-footer&quot;</span><span class="o">&gt;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">button</span> <span class="kr">class</span><span class="o">=</span><span class="s2">&quot;btn&quot;</span><span class="o">&gt;</span><span class="nx">cancel</span><span class="o">&lt;</span><span class="err">/button&gt;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">&lt;</span><span class="nx">button</span> <span class="kr">class</span><span class="o">=</span><span class="s2">&quot;btn-default&quot;</span><span class="o">&gt;</span><span class="nx">Ok</span><span class="o">&lt;</span><span class="err">/button&gt;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="o">&lt;</span><span class="err">/div&gt;</span></div><div class='line' id='LC12'><span class="nt">&lt;/script&gt;</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'><span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;modal&quot;</span><span class="nt">&gt;&lt;/div&gt;</span></div></pre></div>
          </div>

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

<p>Note that I have a &#8220;&lt;div id=&#8217;modal&#8217;&gt;&lt;/div&gt;&#8221; in there. This is explicitly for the Twitter Bootstrap modal dialog to use. This div is the modal. There&#8217;s no content in it, though. I will supply the content for the modal through a Backbone view, which looks like this:</p>
<div id="gist-2359717" 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">view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MyView</span><span class="p">();</span></div><div class='line' id='LC2'><span class="nx">view</span><span class="p">.</span><span class="nx">render</span><span class="p">();</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="kd">var</span> <span class="nx">$modalEl</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#modal&quot;</span><span class="p">);</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="nx">$modalEl</span><span class="p">.</span><span class="nx">html</span><span class="p">(</span><span class="nx">view</span><span class="p">.</span><span class="nx">el</span><span class="p">);</span></div><div class='line' id='LC7'><span class="nx">$modalEl</span><span class="p">.</span><span class="nx">modal</span><span class="p">();</span></div></pre></div>
          </div>

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

<p>How easy is that? I&#8217;m just rendering a view like I always do, stuffing the view&#8217;s `el` in to the &#8220;#modal&#8221; div, and then calling `.modal` on the jQuery selector object. And that&#8217;s it. My modal is done and my view&#8217;s `events` will still work.</p>
<h2>Managing A Modal With Backbone.Marionette</h2>
<p>Yes, it always comes back to my <a href="https://github.com/derickbailey/backbone.marionette">Marionette plugin</a>, right? :)</p>
<p>In this case, I want to manage my modal dialogs as I do any other area of my screen. I want a single object that keeps track of what view is currently displayed within the modal, handles rendering it that view for me, calls the `.modal` dialog method for me, and correctly closes the modal when the &#8220;cancel&#8221; or &#8220;close&#8221; button on my view is clicked.</p>
<p>This largely sounds like a Region object in Backbone.Marionette: a way to manage the contents of a particular DOM element, by rendering and displaying a Backbone View in to that DOM element. Only in this case, it&#8217;s a very specialized Region that needs to manipulate the DOM element as a Twitter Bootstrap modal.</p>
<p>To do this, I&#8217;m going to take advantage of Marionette&#8217;s &#8220;Region&#8221; and extend it with the behavior that I need for managing the modal:</p>
<div id="gist-2359717" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">ModalRegion</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Marionette</span><span class="p">.</span><span class="nx">Region</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">el</span><span class="o">:</span> <span class="s2">&quot;#modal&quot;</span><span class="p">,</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;&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='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">_</span><span class="p">.</span><span class="nx">bindAll</span><span class="p">(</span><span class="k">this</span><span class="p">);</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Marionette</span><span class="p">.</span><span class="nx">Region</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">arguments</span><span class="p">);</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&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;view:show&quot;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">showModal</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">getEl</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">selector</span><span class="p">){</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">$el</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="nx">selector</span><span class="p">);</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$el</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;hidden&quot;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">close</span><span class="p">);</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nx">$el</span><span class="p">;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">showModal</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">view</span><span class="p">){</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">view</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;close&quot;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">hideModal</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">$el</span><span class="p">.</span><span class="nx">modal</span><span class="p">(</span><span class="s1">&#39;show&#39;</span><span class="p">);</span></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;&nbsp;&nbsp;<span class="nx">hideModal</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="nx">$el</span><span class="p">.</span><span class="nx">modal</span><span class="p">(</span><span class="s1">&#39;hide&#39;</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'><br/></div></pre></div>
          </div>

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

<p>This is honestly more code than I wanted to write. There&#8217;s a few things that I need to clean up and add in my Region object to make this a little more elegant. But, it works! And that&#8217;s the important part.</p>
<p>The `constructor` function listens to the region&#8217;s &#8220;view&#8221;show&#8221; event, which is fired when a view&#8217;s contents are populated in to the `el` of the Region. In the event handler for this, I&#8217;m calling the `modal` plugin method on the `el` of the region.</p>
<p>I&#8217;m also setting up a &#8220;close&#8221; event handler on the view itself at this point. That way when I close the view from within my view&#8217;s code (a cancel button, or otherwise), the modal will shutdown. The handler for this simply hides the modal using the modal&#8217;s &#8216;hide&#8217; option.</p>
<p>As I said before, I want to reduce the amount of code that is required to make this happen. But for now, this is a functional modal dialog region. I&#8217;m using it in my application, like this:</p>
<div id="gist-2359717" 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">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">addRegions</span><span class="p">({</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nx">main</span><span class="o">:</span> <span class="s2">&quot;#main-content&quot;</span><span class="p">,</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="nx">modal</span><span class="o">:</span> <span class="nx">ModalRegion</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">// somewhere else in the app</span></div><div class='line' id='LC9'><span class="kd">var</span> <span class="nx">view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MyView</span><span class="p">();</span></div><div class='line' id='LC10'><span class="nx">App</span><span class="p">.</span><span class="nx">modal</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='LC11'><br/></div></pre></div>
          </div>

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

<p>Note that I&#8217;m registering my ModalRegion object in my Marionette.Application with my other regions. Instead of passing in a DOM selector, though, I passing in the ModalRegion type.</p>
<h2>More Than Just Twitter Bootstrap</h2>
<p>The examples I&#8217;ve shown here cover the use of Twitter Bootstrap, but are applicable to more than that. You should be able to easily modify the ModalRegion object to work with any JavaScript modal dialog. I&#8217;ve used the same basic solution for<a href="http://jqueryui.com/demos/dialog/"> jQuery UI modals</a>, and I expect that <a href="http://demos.kendoui.com/web/window/index.html">KendoUI</a> and other modals would work in a similar manner as well (though I may be wrong about that).</p>
<p>The best part about this is that you&#8217;ll only need to modify the region object. You shouldn&#8217;t have to worry about changing your entire application to use a different modal system, because we&#8217;ve encapsulated the modal system in to our ModalRegion object, and our application makes use of that to display the modal dialog with any Backbone.View that we want.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/DerickBailey?a=ZzN2DdCOVXA:Fp_7xqS9vU4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=ZzN2DdCOVXA:Fp_7xqS9vU4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=ZzN2DdCOVXA:Fp_7xqS9vU4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/DerickBailey?i=ZzN2DdCOVXA:Fp_7xqS9vU4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=ZzN2DdCOVXA:Fp_7xqS9vU4:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=ZzN2DdCOVXA:Fp_7xqS9vU4:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=I9og5sOYxJI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=ZzN2DdCOVXA:Fp_7xqS9vU4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/DerickBailey?a=ZzN2DdCOVXA:Fp_7xqS9vU4:bcOpcFrp8Mo"><img src="http://feeds.feedburner.com/~ff/DerickBailey?d=bcOpcFrp8Mo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/DerickBailey/~4/ZzN2DdCOVXA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/derickbailey/2012/04/17/managing-a-modal-dialog-with-backbone-and-marionette/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://lostechies.com/derickbailey/2012/04/17/managing-a-modal-dialog-with-backbone-and-marionette/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.845 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-05-17 07:19:07 -->

