<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>QuirkeyBlog &#187; jQuery</title>
	<atom:link href="http://www.quirkey.com/blog/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.quirkey.com/blog</link>
	<description>A Developer with too little time.</description>
	<lastBuildDate>Mon, 01 Aug 2011 16:07:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Getting to know Sammy.js: Helpers</title>
		<link>http://www.quirkey.com/blog/2010/11/04/getting-to-know-sammy-js-helpers/</link>
		<comments>http://www.quirkey.com/blog/2010/11/04/getting-to-know-sammy-js-helpers/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 12:07:08 +0000</pubDate>
		<dc:creator>AQ</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Sammy]]></category>
		<category><![CDATA[Software/Scripts]]></category>

		<guid isPermaLink="false">http://www.quirkey.com/blog/?p=475</guid>
		<description><![CDATA[For part of this month&#8217;s writing I&#8217;m going to write focused posts about features of Sammy.js that people may not know about or have used. In the end I hope to compile these into part of the new Sammy.js site. A question came up on the mailing list recently about overriding previously defined routes after [...]]]></description>
			<content:encoded><![CDATA[<p><em>For part of this month&#8217;s writing I&#8217;m going to write focused posts about features of Sammy.js that people may not know about or have used. In the end I hope to compile these into part of the new Sammy.js site.</em></p>
<p>A question came up on the <a href="http://groups.google.com/group/sammyjs/browse_frm/thread/8234ef63d20c99ae/580e66ac59a483cf">mailing list recently about overriding previously defined routes</a> after struggling thinking of how to do this with the current codebase, the answer was simple: Use helpers!</p>
<p>Coming from Sinatra or Rails the idea of <code>helpers</code> might be associated with just &#8220;view helpers&#8221; &#8211; methods added to the controller which are passed down to the view to help refactor/encapsulate common view logic. In Sammy.js helpers are really a way of refactoring common logic out of routes and into a reusable named method.</p>
<span id="more-475"></span>
<h3>Refactorin&#8217;</h3>
<p>In any app you&#8217;ll have startup code or code that needs to be run for every or multiple pages (routes). In Sammy this might looks like:</p>
<div class="highlight"><pre><span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;#/&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">params</span><span class="p">.</span><span class="nx">grid</span><span class="p">)</span> <span class="p">{</span><br />
      <span class="k">this</span><span class="p">.</span><span class="nx">$element</span><span class="p">().</span><span class="nx">addClass</span><span class="p">(</span><span class="s1">&#39;grid&#39;</span><span class="p">);</span><br />
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span><br />
      <span class="k">this</span><span class="p">.</span><span class="nx">$element</span><span class="p">().</span><span class="nx">removeClass</span><span class="p">(</span><span class="s1">&#39;grid&#39;</span><span class="p">);</span><br />
    <span class="p">}</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">partial</span><span class="p">(</span><span class="s1">&#39;index.html&#39;</span><span class="p">);</span><br />
  <span class="p">});</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;#/user&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">params</span><span class="p">.</span><span class="nx">grid</span><span class="p">)</span> <span class="p">{</span><br />
      <span class="k">this</span><span class="p">.</span><span class="nx">$element</span><span class="p">().</span><span class="nx">addClass</span><span class="p">(</span><span class="s1">&#39;grid&#39;</span><span class="p">);</span><br />
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span><br />
      <span class="k">this</span><span class="p">.</span><span class="nx">$element</span><span class="p">().</span><span class="nx">removeClass</span><span class="p">(</span><span class="s1">&#39;grid&#39;</span><span class="p">);</span><br />
    <span class="p">}</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">partial</span><span class="p">(</span><span class="s1">&#39;user.html&#39;</span><span class="p">);</span><br />
  <span class="p">});</span><br />
  <br />
<span class="p">})</span><br />
</pre></div>
<p>A contrived example, but you can tell we&#8217;re doing almost the exactly same thing in each of the routes. A common JS convention and something you could do pretty easily here, would just be to extract the functionality out into a closure/function and call it for each route.</p>
<div class="highlight"><pre><span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <br />
  <span class="kd">var</span> <span class="nx">displayGrid</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">$element</span><span class="o">,</span> <span class="nx">display</span><span class="p">)</span> <span class="p">{</span><br />
    <span class="k">if</span> <span class="p">(</span><span class="nx">display</span><span class="p">)</span> <span class="p">{</span><br />
      <span class="nx">$element</span><span class="p">.</span><span class="nx">addClass</span><span class="p">(</span><span class="s1">&#39;grid&#39;</span><span class="p">);</span><br />
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span><br />
      <span class="nx">$element</span><span class="p">.</span><span class="nx">removeClass</span><span class="p">(</span><span class="s1">&#39;grid&#39;</span><span class="p">);</span><br />
    <span class="p">}</span><br />
  <span class="p">};</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;#/&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="nx">displayGrid</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">$element</span><span class="p">()</span><span class="o">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">params</span><span class="p">.</span><span class="nx">grid</span><span class="p">);</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">partial</span><span class="p">(</span><span class="s1">&#39;index.html&#39;</span><span class="p">);</span><br />
  <span class="p">});</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;#/user&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="nx">displayGrid</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">$element</span><span class="p">()</span><span class="o">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">params</span><span class="p">.</span><span class="nx">grid</span><span class="p">);</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">partial</span><span class="p">(</span><span class="s1">&#39;user.html&#39;</span><span class="p">);</span><br />
  <span class="p">});</span><br />
  <br />
<span class="p">})</span><br />
</pre></div>
<p>This works, it prevents global leakage by declaring it inside the app, and it gets rid of a number of lines of repetitive code. However, we&#8217;re still passing a bunch of route attributes into the closure. This is where helpers can shine.</p>
<div class="highlight"><pre><span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">helpers</span><span class="p">({</span><br />
    <span class="nx">displayGrid</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
      <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">params</span><span class="p">.</span><span class="nx">grid</span><span class="p">)</span> <span class="p">{</span><br />
        <span class="k">this</span><span class="p">.</span><span class="nx">$element</span><span class="p">().</span><span class="nx">addClass</span><span class="p">(</span><span class="s1">&#39;grid&#39;</span><span class="p">);</span><br />
      <span class="p">}</span> <span class="k">else</span> <span class="p">{</span><br />
        <span class="k">this</span><span class="p">.</span><span class="nx">$element</span><span class="p">().</span><span class="nx">removeClass</span><span class="p">(</span><span class="s1">&#39;grid&#39;</span><span class="p">);</span><br />
      <span class="p">}</span><br />
  <span class="p">});</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;#/&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">displayGrid</span><span class="p">();</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">partial</span><span class="p">(</span><span class="s1">&#39;index.html&#39;</span><span class="p">);</span><br />
  <span class="p">});</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;#/user&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">displayGrid</span><span class="p">();</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">partial</span><span class="p">(</span><span class="s1">&#39;user.html&#39;</span><span class="p">);</span><br />
  <span class="p">});</span><br />
  <br />
<span class="p">});</span><br />
</pre></div>
<p>This looks a lot cleaner to me. You might not love <code>this</code> but it makes some things really magical. By making <code>displayGrid</code> a helper it automagically has access to the context of the current route. That means that just like the route&#8217;s callback, it has access to everything in the <code>EventContext</code> including <code>params</code>, <code>redirect</code>, <code>render</code> and <em>other helpers</em>. This is important, because it basically allows you to extract common code from routes into helpers and then refactor those helpers even further into <em>other</em> helpers.</p>
<h3>stOOPid inheritance and mixins</h3>
<p>Helpers are actually just methods on the app&#8217;s <code>EventContext</code> prototype, and event though there&#8217;s no classical inheritance with <code>super</code>&#8217;s and all that jazz we can do some fun <span class="caps">OOP</span>-like magic.</p>
<p>In the example from the mailing list, we wanted to override an application&#8217;s existing route with a new route. Though the way routes are stored and handled makes it difficult on the app level, doing it on the helper level is EZ:</p>
<div class="highlight"><pre><span class="c">// initializing a new app</span><br />
<span class="kd">var</span> <span class="nx">app</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">helpers</span><span class="p">({</span><br />
    <span class="c">// define a helper to load the index as the default behavior</span><br />
    <span class="nx">loadIndex</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
      <span class="k">this</span><span class="p">.</span><span class="nx">partial</span><span class="p">(</span><span class="s1">&#39;index.html&#39;</span><span class="p">);</span><br />
    <span class="p">}</span><br />
  <span class="p">});</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;#/&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">loadIndex</span><span class="p">();</span><br />
  <span class="p">});</span><br />
<span class="p">});</span><br />
<br />
<span class="c">// override the specific helper, and hence the index route</span><br />
<span class="c">// we can also do this by calling `helpers()`</span><br />
<span class="nx">app</span><span class="p">.</span><span class="nx">helper</span><span class="p">(</span><span class="s1">&#39;loadIndex&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <span class="k">this</span><span class="p">.</span><span class="nx">partial</span><span class="p">(</span><span class="s1">&#39;specialindex.html&#39;</span><span class="p">);</span><br />
<span class="p">});</span><br />
</pre></div>
<p><code>use()</code> is how Sammy includes a plugin in an app. A plugin is really just another app function, though, that get&#8217;s evaluated at the time you <code>use()</code> it. In fact, when you call <code>$.sammy()</code> you&#8217;re just creating a new instance of <code>Sammy.Application</code> and then calling <code>use()</code> on it with the app function. This is a simple way of doing &#8220;mixin&#8221; style inheritance. A plugin can do anything an app can, including and <em>especially</em> helpers. So with multiple apps we be all like tag team:  <a href="http://www.youtube.com/watch?v=Z-FPimCmbX8"><span class="caps">OOP</span> there it is</a></p>
<div class="highlight"><pre><span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="s1">&#39;#hiphop&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <span class="k">this</span><span class="p">.</span><span class="nx">helpers</span><span class="p">({</span><br />
    <span class="nx">playCatchPhrase</span><span class="o">:</span> <span class="nx">$</span><span class="p">.</span><span class="nx">noop</span><br />
    <span class="c">// noop there it is </span><br />
    <span class="c">// this is sort of like an abstract method</span><br />
  <span class="p">});</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;#/catchphrase&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">playCatchPhrase</span><span class="p">();</span><br />
  <span class="p">})</span><br />
<span class="p">});</span><br />
<br />
<span class="kd">var</span> <span class="nx">atlanta</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <span class="k">this</span><span class="p">.</span><span class="nx">helper</span><span class="p">(</span><span class="s1">&#39;playCatchPhrase&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="nx">alert</span><span class="p">(</span><span class="s1">&#39;whoomp there it is!&#39;</span><span class="p">);</span><br />
  <span class="p">});</span><br />
<span class="p">};</span><br />
<br />
<span class="kd">var</span> <span class="nx">la</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <span class="k">this</span><span class="p">.</span><span class="nx">helper</span><span class="p">(</span><span class="s1">&#39;playCatchPhrase&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="nx">alert</span><span class="p">(</span><span class="s1">&#39;do the hump!&#39;</span><span class="p">);</span><br />
  <span class="p">});</span><br />
<span class="p">};</span><br />
<br />
<span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="s1">&#39;#hiphop&#39;</span><span class="p">).</span><span class="nx">use</span><span class="p">(</span><span class="nx">la</span><span class="p">);</span><br />
<span class="c">// get &#39;#/catchphrase&#39; //=&gt; &#39;do the hump!&#39;</span><br />
</pre></div>
<p>Thats still pretty simple, though combining these ideas is actually pretty powerful. Almost all of Sammy&#8217;s existing plugins are based on this simple approach. I&#8217;m hoping soon, it can get even more abstract and complex. Why not a <code>Sammy.Auth</code> that handles displaying/posting/and doing all the basic authentication? You could use helpers for most of the dirty work and then one could just override the defaults in their specific apps. You could even create <em>another</em> set of plugins that were different displays or authentication systems that override the simple base methods. Do it! ( <em>so I dont have to</em> )</p>]]></content:encoded>
			<wfw:commentRss>http://www.quirkey.com/blog/2010/11/04/getting-to-know-sammy-js-helpers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sammy 0.5: Something for Everyone</title>
		<link>http://www.quirkey.com/blog/2010/02/15/sammy-0-5-something-for-everyone/</link>
		<comments>http://www.quirkey.com/blog/2010/02/15/sammy-0-5-something-for-everyone/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 19:06:24 +0000</pubDate>
		<dc:creator>AQ</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Sammy]]></category>
		<category><![CDATA[Software/Scripts]]></category>

		<guid isPermaLink="false">http://www.quirkey.com/blog/?p=411</guid>
		<description><![CDATA[I&#8217;m pleased to announce the next major release of Sammy.js, 0.5.0. I&#8217;ll go over what I think are the most import changes, but you can check out the HISTORY or just cut to the chase and download it. jQuery 1.4.1 One of the big events I was waiting for in order to release this new [...]]]></description>
			<content:encoded><![CDATA[<p><img src="/blog/uploads/sammy-something-for-everyone.jpg" alt="Something for everyone" /></p>
<p>I&#8217;m pleased to announce the next major release of <a href="http://code.quirkey.com/sammy">Sammy.js</a>, 0.5.0. I&#8217;ll go over what I think are the most import changes, but you can check out the <a href="http://github.com/quirkey/sammy/blob/master/HISTORY.md"><span class="caps">HISTORY</span></a> or just cut to the chase and <a href="http://github.com/quirkey/sammy/tarball/v0.5.0">download it</a>.</p>
<h3>jQuery 1.4.1</h3>
<p>One of the big events I was waiting for in order to release this new version was the <a href="http://jquery14.com">release of jQuery 1.4.1</a>. Besides being a good leap and jump faster then previous versions, the latest jQuery added support for cross-browser bubbling of the <code>submit</code> event. This might not sound like a big deal, but Sammy handles <code>post/put/delete</code> routes by binding to forms and their <code>submit</code> events. In previous versions of Sammy, if a form was dynamically added to the page (i.e. by a <code>partial</code> or <code>get</code> route), you (or the app) would fire a <code>changed</code> event, which would tell the app to search for all newly added forms and bind to their <code>submit</code> events. Using the power of jQuery 1.4.1, Sammy can now just listen for submit events that will bubble up to the applications element and from their dispatch the route. In general this is a lot cleaner and will make it easier for beginners as it just <em>works</em>.</p>
<h3>Chain of fools</h3>
<p>From user requests, and to fulfill my own desires, I&#8217;ve tried make Sammy more adaptable to different styles of coding/app creation. The first thing I realized why not make Sammy app definitions chain-able? It&#8217;s a style of development that jQuery developers are already used to, and it looks pretty swanky, too. With 0.5, all the app modifying methods are now chain-able:</p>
<div class="highlight"><pre>  <br />
<br />
<span class="c">// classic style:</span><br />
<span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <span class="k">this</span><span class="p">.</span><span class="nx">use</span><span class="p">(</span><span class="nx">Sammy</span><span class="p">.</span><span class="nx">Mustache</span><span class="p">);</span><br />
  <span class="k">this</span><span class="p">.</span><span class="nx">use</span><span class="p">(</span><span class="nx">Sammy</span><span class="p">.</span><span class="nx">Session</span><span class="p">);</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">before</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="c">// &#8230;</span><br />
  <span class="p">});</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;#/&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="c">// &#8230;</span><br />
  <span class="p">});</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">bind</span><span class="p">(</span><span class="s1">&#39;run&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="c">// &#8230;</span><br />
  <span class="p">});</span><br />
  <br />
<span class="p">})</span><br />
<br />
<span class="c">// New chain style:</span><br />
<span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">()</span><br />
  <span class="p">.</span><span class="nx">use</span><span class="p">(</span><span class="nx">Sammy</span><span class="p">.</span><span class="nx">Mustache</span><span class="p">)</span><br />
  <span class="p">.</span><span class="nx">use</span><span class="p">(</span><span class="nx">Sammy</span><span class="p">.</span><span class="nx">Session</span><span class="p">)</span><br />
  <span class="p">.</span><span class="nx">before</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span> <br />
    <span class="c">// .</span><br />
  <span class="p">})</span><br />
  <span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;#/&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="c">// &#8230;</span><br />
  <span class="p">})</span><br />
  <span class="p">.</span><span class="nx">bind</span><span class="p">(</span><span class="s1">&#39;run&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="c">// &#8230;</span><br />
  <span class="p">});</span><br />
    <br />
</pre></div>
<h3>Sammy()</h3>
<p>Once just a namespace, <code>Sammy()</code> is now a function (and its aliased to $.sammy). The <code>Sammy()</code> method provides access to the new Sammy.apps object and hence allows you to access the different Sammy applications you&#8217;ve added to the page without having to define additional global vars. Finding and extending is all based on @element_selector@s so those should be unique per-application. It also provides a quicker syntax for defining an app bound to an <code>element_selector</code>.</p>
<div class="highlight"><pre><span class="c">// Old way</span><br />
<span class="c">// we&#39;re forced to make app a global so we can access it </span><br />
<span class="c">// outside of this closure</span><br />
<span class="nx">app</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <span class="k">this</span><span class="p">.</span><span class="nx">element_selector</span> <span class="o">=</span> <span class="s1">&#39;#myapp&#39;</span><span class="o">;</span><br />
  <span class="c">//&#8230;</span><br />
<span class="p">})</span><br />
<span class="c">// equivalent to</span><br />
<span class="nx">app</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Sammy</span><span class="p">.</span><span class="nx">Application</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <span class="k">this</span><span class="p">.</span><span class="nx">element_selector</span> <span class="o">=</span> <span class="s1">&#39;#myapp&#39;</span><span class="o">;</span><br />
  <span class="c">//&#8230;</span><br />
<span class="p">});</span><br />
<br />
<span class="c">// <span class="caps">NEW</span> <span class="caps">WAY</span></span><br />
<span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="s1">&#39;#myapp&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <span class="c">//&#8230;</span><br />
<span class="p">});</span><br />
<span class="c">// equivalent to Sammy(&#39;#myapp&#39;, function() {})</span><br />
<br />
<span class="c">// No need to assign it, the app can be looked up by element selector</span><br />
<span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="s1">&#39;#myapp&#39;</span><span class="p">)</span> <span class="c">//=&gt; Sammy.Application</span><br />
</pre></div>
<p>There are some cool use-cases for this, including being able to iterate over all the Sammy apps in play and extend them.</p>
<h3><code>any</code> way you want it</h3>
<p>Sammy 0.5 adds the new <code>any</code> <em>pseudo</em>-verb which allows you to easily add a route that will match against any verb:</p>
<div class="highlight"><pre>  <br />
<span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">any</span><span class="p">(</span><span class="s1">&#39;#/&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;I&#39;m up for anything&quot;</span><span class="p">);</span><br />
  <span class="p">});</span><br />
  <br />
  <span class="c">// also equivalent</span><br />
  <span class="k">this</span><span class="p">.</span><span class="nx">route</span><span class="p">(</span><span class="s1">&#39;#/&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{});</span><br />
  <br />
<span class="p">});</span><br />
</pre></div>
<p>This was actually the first step of another way to define Sammy applications. The new <code>mapRoutes()</code> method allows you to define any number of routes as 2D Array.</p>
<div class="highlight"><pre><span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">runIndex</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="k">this</span><span class="p">.</span><span class="nx">partial</span><span class="p">(</span><span class="s1">&#39;index.template&#39;</span><span class="p">);</span><br />
  <span class="p">};</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">mapRoutes</span><span class="p">([</span><br />
    <span class="c">// each element in the array represents the arguments sent to route()</span><br />
    <span class="p">[</span><span class="s1">&#39;get&#39;</span><span class="o">,</span> <span class="s1">&#39;#/user&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
      <span class="k">this</span><span class="p">.</span><span class="nx">partial</span><span class="p">(</span><span class="s1">&#39;user.template&#39;</span><span class="p">);</span><br />
    <span class="p">}]</span><span class="o">,</span><br />
    <span class="c">// leaving out the verb assumes the verb is &#39;any&#39;</span><br />
    <span class="p">[</span><span class="sr">/\/alert$/</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
      <span class="nx">alert</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">params</span><span class="p">[</span><span class="s1">&#39;alert&#39;</span><span class="p">]);</span><br />
    <span class="p">}]</span><span class="o">,</span><br />
    <span class="c">// passing a string instead of a callback assigns the method with that</span><br />
    <span class="c">// name from the application.</span><br />
    <span class="p">[</span><span class="s1">&#39;get&#39;</span><span class="o">,</span> <span class="s1">&#39;#/&#39;</span><span class="o">,</span> <span class="s1">&#39;runIndex&#39;</span><span class="p">]</span><br />
  <span class="p">]);</span><br />
  <br />
<span class="p">});</span><br />
</pre></div>
<p>I personally like the method syntax better, but there are some cool uses for this &#8211; You could even load your routes from multiple files and add them to the application using <span class="caps">JSON</span>.</p>
<h3>Filters</h3>
<p><code>before</code> filters are very useful for extracting common functionality out of your routes. However, I&#8217;ve found myself writing this pretty often:</p>
<div class="highlight"><pre><span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">before</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="c">// only run if the path is not the index</span><br />
    <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">path</span> <span class="o">!=</span> <span class="s1">&#39;#/&#39;</span><span class="p">)</span> <span class="p">{</span><br />
      <span class="c">//&#8230; do something</span><br />
    <span class="p">}</span><br />
  <span class="p">})</span><br />
  <br />
<span class="p">});</span><br />
</pre></div>
<p>Taking some hints from Rails, I&#8217;ve added options to <code>before</code> filters. The above example could be rewritten:</p>
<div class="highlight"><pre><span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">before</span><span class="p">({</span><span class="nx">except</span><span class="o">:</span> <span class="s1">&#39;#/&#39;</span><span class="p">}</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
    <span class="c">//&#8230; do something</span><br />
  <span class="p">})</span><br />
  <br />
<span class="p">});</span><br />
</pre></div>
<p>The filters can match against <code>path</code> (as a string or RegExp) and <code>verb</code> and you can define them in the positive (<code>only</code>) or negative (<code>except</code>). The method that does the matching is exposed as <code>contextMatchesOptions</code> so you can use it outside of <code>before()</code></p>
<p>I&#8217;ve also found myself doing a lot of wrapping entire route callbacks in an asynchronous check for something (usually a login). The new <code>around</code> filters make this easy. <code>around</code> takes a single argument which is a neatly packed function that contains the full execution path of the route being accessed. This includes additional filters and the route callback itself. This also means you can entirely halt the execution by never running the passed callback.</p>
<div class="highlight"><pre><span class="nx">$</span><span class="p">.</span><span class="nx">sammy</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
  <br />
  <span class="k">this</span><span class="p">.</span><span class="nx">around</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span><br />
    <span class="c">// `this` here is the Sammy.EventContext</span><br />
    <span class="c">// just like before, etc</span><br />
    <span class="nx">$</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/session&#39;</span><span class="o">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><br />
      <span class="nx">callback</span><span class="p">();</span><br />
    <span class="p">});</span><br />
  <span class="p">});</span><br />
<br />
<span class="p">})</span><br />
</pre></div>
<h3>Lots more</h3>
<p>Theres actually a fair amount more to cover in Sammy 0.5, but I think I&#8217;ll be doing more short &#8216;Whats new&#8217; posts soon. Check the <a href="http://github.com/quirkey/sammy/blob/master/HISTORY.md"><span class="caps">HISTORY</span></a> for a full rundown of changes.</p>
<h3>Thanks</h3>
<p>Special thanks goes out to everyone who helped with ideas, commits and testing for this release including Lena Herrmann, Jinzhu, dpree, Jeff O&#8217;Connell, Todd Willey, Tim Caswell, and Frank Prößdorf (endor).</p>]]></content:encoded>
			<wfw:commentRss>http://www.quirkey.com/blog/2010/02/15/sammy-0-5-something-for-everyone/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Sammy.js: Tutorial &#8211; The JSON Store, Part I</title>
		<link>http://www.quirkey.com/blog/2009/10/12/sammy-js-tutorial-the-json-store-part-i/</link>
		<comments>http://www.quirkey.com/blog/2009/10/12/sammy-js-tutorial-the-json-store-part-i/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 15:00:21 +0000</pubDate>
		<dc:creator>AQ</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Sammy]]></category>

		<guid isPermaLink="false">http://www.quirkey.com/blog/?p=378</guid>
		<description><![CDATA[Its a week late, but as promised I&#8217;ve started working on a series of tutorials for Sammy.js. The first series is based around a simple E-commerce front end with Sammy, &#8216;The JSON Store&#8217;. I&#8217;m starting out really basic, so if you&#8217;ve already dabbled with Sammy, this might be old hat, but you should breeze through [...]]]></description>
			<content:encoded><![CDATA[	<p>Its a week late, but as promised <a href="http://code.quirkey.com/sammy/tutorials/json_store_part1.html" title="">I&#8217;ve started working on a series of tutorials for Sammy.js.</a> The first series is based around a simple E-commerce front end with Sammy, &#8216;The <span class="caps">JSON </span>Store&#8217;. I&#8217;m starting out <em>really</em> basic, so if you&#8217;ve already dabbled with Sammy, this might be old hat, but you should breeze through quickly as you could learn a trick or two. For the more intermediate and advanced users, don&#8217;t worry, I have some ideas and things in progress for you as well.</p>

	<p>Quick note, I decided to use github pages instead of this here blog to post the tutorials. In the end, its just easier to post lots of inline code <span class="caps">AND</span> it will be easier to maintain with updates as the Sammy <span class="caps">API</span> changes.</p>

	<p>I hope you enjoy it! I&#8217;m always appreciative of feedback and constructive criticism, so fire away.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://www.quirkey.com/blog/2009/10/12/sammy-js-tutorial-the-json-store-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sammy 0.3.0 Released: I gotta right to swing!</title>
		<link>http://www.quirkey.com/blog/2009/09/30/sammy-0-3-0-released-i-gotta-right-to-swing/</link>
		<comments>http://www.quirkey.com/blog/2009/09/30/sammy-0-3-0-released-i-gotta-right-to-swing/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 14:57:57 +0000</pubDate>
		<dc:creator>AQ</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Sammy]]></category>
		<category><![CDATA[Software/Scripts]]></category>

		<guid isPermaLink="false">http://www.quirkey.com/blog/?p=371</guid>
		<description><![CDATA[I&#8217;m very happy to announce the next major release of Sammy.js. I&#8217;ve been working on this for over a month and the result has been a lot more changes then I expected but over all a smaller, cleaner core code base. Here&#8217;s a quick run through of the biggest changes. See HISTORY or the Commits [...]]]></description>
			<content:encoded><![CDATA[<p><img src="/blog/uploads/igottarighttoswing.jpg" alt="Sammy 0.3.0: I gotta right to swing!" /></p>


	<p>I&#8217;m very happy to announce the next major release of <a href="http://code.quirkey.com/sammy">Sammy.js.</a> I&#8217;ve been working on this for over a month and the result has been a lot more changes then I expected but over all a smaller, cleaner core code base. Here&#8217;s a quick run through of the biggest changes. See <a href="http://github.com/quirkey/sammy/blob/master/HISTORY"><span class="caps">HISTORY</span></a> or the <a href="http://github.com/quirkey/sammy/commits/master">Commits</a> for a full list and more detail.</p>
<span id="more-371"></span>
	<h3>Plugins</h3>

	<p>The biggest change, that resulted in a lot of little changes is <code>Sammy.Application#use()</code> which allows you to package and resuse application level code into &#8216;Plugins&#8217;. See the <a href="http://code.quirkey.com/sammy/docs/plugins.html">documentation on plugins for more detail.</a> This also marks the start of an official selection of plugins, with two as part of this release and more on the way.</p>

<p>The default template system that used $.srender internally has been extracted out into a plugin. To enable <em>almost</em> the exact same functionality:</p>

<pre class="textmate-source"><span class="source source_js source_js_jquery"><span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> somewhere in your HTML (after sammy.js): 
</span><span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> &lt;script src="plugins/sammy.template.js" type="text/javascript"&gt;&lt;/script&gt;
</span><span class="keyword keyword_operator keyword_operator_js">$</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>sammy<span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="storage storage_type storage_type_js">function</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
  <span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> include the Sammy.Template plugin
</span>  <span class="variable variable_language variable_language_js">this</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>use<span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span>Sammy<span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>Template<span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
  
<span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span></span></pre>

	<h3>Events</h3>

	<p>Sammy.Application&#8217;s bind() and trigger() methods now use jQuery&#8217;s namespacing to bind to Events. This means that Sammy.Application can now easily catch events that bubble to it like &#8216;click&#8217; or &#8216;focus&#8217;. The big awesomeness is that the callbacks for these bind() methods are always evaluated within a Sammy.EventContext so that even events triggered from outside the app can be handled in the same way as internal events.</p>

  <pre class="textmate-source"><span class="source source_js source_js_jquery"><span class="storage storage_type storage_type_js">var</span> app <span class="keyword keyword_operator keyword_operator_js">=</span> <span class="keyword keyword_operator keyword_operator_js">$</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>sammy<span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="storage storage_type storage_type_js">function</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>

    <span class="variable variable_language variable_language_js">this</span><span class="support support_function support_function_js support_function_js_jquery">.bind</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>custom-event<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">, </span><span class="storage storage_type storage_type_js">function</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
      <span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> this == Sammy.EventContext
  </span>    <span class="support support_function support_function_js">alert</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>Custom Event<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span> <span class="keyword keyword_operator keyword_operator_js">+</span> <span class="variable variable_language variable_language_js">this</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>params<span class="meta meta_brace meta_brace_square meta_brace_square_js">[</span><span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>wha<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_brace meta_brace_square meta_brace_square_js">]</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
    <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>

  <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>

  <span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> the app is by default bound to 'body'
  </span><span class="support support_class support_class_js support_class_js_jquery">$</span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">(</span><span class="meta meta_selector meta_selector_jquery"><span class="punctuation punctuation_definition punctuation_definition_selector punctuation_definition_selector_begin punctuation_definition_selector_begin_js">'</span><span class="meta meta_selector meta_selector_css"><span class="entity entity_name entity_name_tag entity_name_tag_css">body</span></span><span class="punctuation punctuation_definition punctuation_definition_selector punctuation_definition_selector_end punctuation_definition_selector_end_js">'</span></span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">)</span><span class="support support_function support_function_js support_function_js_jquery">.trigger</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>custom-event<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">, </span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>wha: <span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>ZUH?!<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
  <span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span>=&gt; system alert ZUH?!
  </span></span></pre>

	<h3>Cleanup</h3>

	<p>With the advent of plugins, Sammy Core is now even smaller (8.9k minified). This is also possible because I&#8217;ve removed the former &#8216;classical&#8217; inheritence code in favor of prototypal inheritence, which is a big win in terms of cleanlieness and global scope leakage.</p>


	<h3>See Also</h3>


	<p>I mentioned this on twitter, but I&#8217;m working on some new tutorials and screencasts about Sammy. If you have ideas/suggestions, dont hesitate to let me know. The first one should be up next week. I&#8217;m going to try to do them bi-weekly, we&#8217;ll see how that goes <img src='http://www.quirkey.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>


	<p>Again, I&#8217;ve tried to be as thorough with inline <span class="caps">API</span> docs as I could so any changes should be in the clear. Thanks again to everyone who helped with this release and has supported Sammy recently!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.quirkey.com/blog/2009/09/30/sammy-0-3-0-released-i-gotta-right-to-swing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sammy.js, CouchDB, and the new web architecture</title>
		<link>http://www.quirkey.com/blog/2009/09/15/sammy-js-couchdb-and-the-new-web-architecture/</link>
		<comments>http://www.quirkey.com/blog/2009/09/15/sammy-js-couchdb-and-the-new-web-architecture/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 13:44:38 +0000</pubDate>
		<dc:creator>AQ</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Sammy]]></category>

		<guid isPermaLink="false">http://www.quirkey.com/blog/?p=359</guid>
		<description><![CDATA[UPDATE: My slides and a protected instance (will open up for play soon) or swinger, available here I&#8217;ll be honest. When I created Sammy.js, It was more of an academic exercise then a means to an end. I had ideas for how it could be useful, and had some immediate uses for it, but the [...]]]></description>
			<content:encoded><![CDATA[	<p><em><span class="caps">UPDATE</span>: My slides and a protected instance (will open up for play soon) or swinger, available <a href="http://c.ixxr.net/swinger/_design/swinger/index.html#/preso/jqcon_09/display/1" title="">here</a> </em></p>

	<p>I&#8217;ll be honest. When I created <a href="http://code.quirkey.com/sammy" title="">Sammy.js,</a> It was more of an academic exercise then a means to an end. I had ideas for how it could be useful, and had some immediate uses for it, but the initial thought was &#8216;recreating Sinatra&#8217;s <span class="caps">API</span> in JavaScript&#8217;. Little did I know &#8211; with a little introduction and the work of some clever individuals, its real value would be revealed.</p>

	<p><span id="more-359"></span></p>

	<p>We&#8217;re at the dawn of a new age on the web, or maybe or horizon, or a precipice; the metaphor doesn&#8217;t matter, the point is, things are changing. Specifically, as browsers become more and more powerful, as developers we&#8217;re given new opportunities to rethink how we architect web applications. Whether or not some of the predictions of &#8216;the death of the traditional desktop&#8217; are correct &#8211; whether or not the near future desktop is just the browser &#8211; we now have the power, without proprietary technologies (flash, etc) to create truly desktop like experiences on the web. Thats nothing mind-blowing, we&#8217;ve seen it coming for a little while now.</p>

	<p>Ok, then: let me <em>blow your mind.</em></p>

	<p>Because of the great power that modern browsers provide us as Javascript developers a new paradigm of web development is arising. Lets take a look at the current/traditional model:</p>

	<p><img src="http://img.skitch.com/20090915-6xfj7cp1kx6w884tqw29yk2k4.jpg" alt="Old Way" /></p>

	<p>Here we have a traditional stack. The &#8216;power&#8217; of the application lies on the server: our development platform of choice (Rails, <span class="caps">PHP</span>, etc) sits on top of our database of choice (probably relational, probably <span class="caps">SQL</span>). This server platform renders finished <span class="caps">HTML</span> and maybe decorates those pages with the use of our JS library and our application specific code (Your $(hit)). In this model we&#8217;re using probably at least 3 different programming languages and almost fully on the speed and reliability of our servers for getting data to the user. There&#8217;s probably also at least 3 steps in either direction between data and the user:</p>

	<ul>
		<li>User clicks a link on a page or follows a <span class="caps">URL</span></li>
		<li>The request is sent to the server</li>
		<li>App receives request passes it on to Model/ORM layer</li>
		<li><span class="caps">ORM</span> requests the data from the Database</li>
		<li>Database returns the data to the <span class="caps">ORM</span></li>
		<li><span class="caps">ORM</span> returns the data in a parse-able form to the App</li>
		<li>App converts it into user-readable format and displays it to user</li>
	</ul>

	<p>Obviously, Rails and most other languages frameworks abstract a lot of this away, so we don&#8217;t really have to think in all these steps. They&#8217;re there, though.</p>

	<p><img src="http://img.skitch.com/20090915-c8trr5mh7ukbhg6eukptwsuq3r.jpg" alt="The New Way" /></p>

	<p>Welcome to the new world. <span class="caps">HTTP </span>Databases and <span class="caps">JSON </span>Storage. The simple act of making the database and the browser more powerful on either end has destroyed the need for the middle tier. In the new architecture, Our database (JSON/HTTP based: <a href="http://couchdb.org" title="">CouchDB</a>, <a href="http://getcloudkit.com" title="">Cloudkit</a>) serves data as <span class="caps">JSON</span> directly to the browser. On the browser side we create a much smaller/tighter &#8216;controller&#8217; layer with JavaScript. This handles the directing of the user to the right place, the displaying of the data to the user, and the conversion of user interaction into state + data. This middle piece is jQuery + Sammy.js.</p>

	<p>In my presentation at jQuery Conf this past weekend (<a href="http://c.ixxr.net/swinger/_design/swinger/index.html#/preso/jqcon_09/display/1" title="">slides available here</a>) I demonstrated this concept in a rather meta-tastic way. The presentation tool I used was called <a href="http://github.com/quirkey/swinger" title="">Swinger</a> and it was written using CouchDB, <a href="http://github.com/couchapp/couchapp" title="">CouchApp,</a> and Sammy.js. The application logic was contained in a single JS file that used a Sammy application to do all the routing, getting data in and out of CouchDB and displaying it in a particularly pretty way. The coolest part about this whole thing is that even beyond taking out the middle layer, this implementation removes the traditional static file serving model, too. With the power of CouchApp and _attachments, the javascript and <span class="caps">HTML</span> files needed to serve the application are served directly out of the database. Its like CouchDB and Sammy.js were made for each other. Think about the new steps for this architecture:</p>

	<ul>
		<li>User clicks a link on the page or follows a <span class="caps">URL</span></li>
		<li>Data is requested via <span class="caps">AJAX</span> from the server</li>
		<li>The server proxies to the database or processes the request directly</li>
		<li>The data is returned to Sammy.js as <span class="caps">JSON</span></li>
		<li>Sammy.js uses the data it needs and displays it to the user</li>
	</ul>

	<p>There are a number really amazing and exciting things to note here. First, notice how the application is written in JavaScript <span class="caps">AND</span> the data is JavaScript. The <span class="caps">JSON</span> store might actually be written in another language (CouchDB is Erlang, Cloudkit is Ruby) but we don&#8217;t really ever have to touch those. Personally, I really like JavaScript. Sure, I miss some of Ruby&#8217;s syntax and niceties, but I&#8217;ll give that up pretty quickly for something that still shares some of the similar good features (closures, loose typing) and has a ubiquitous deployment environment (the browser). Second, the end result is probably similar if not better to the end user, as we eliminate the need to reload the page at every request, resulting in a better perceived speed.</p>

	<p>One of the provisions I gave for my talk is that &#8220;I&#8217;m less concerned with speed than awesomeness&#8221;. That rings pretty true here. I don&#8217;t have an architecture like this running in production and getting hit like twitter. I can&#8217;t really speak yet to how well it &#8216;scales&#8217; or how it preforms under load. <span class="caps">I CAN</span> tell you about how awesome it is and how easy and fun it was to develop swinger with Sammy. That&#8217;s really all <em>I</em> need to know right now.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://www.quirkey.com/blog/2009/09/15/sammy-js-couchdb-and-the-new-web-architecture/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
		<item>
		<title>jQueryConf 2009</title>
		<link>http://www.quirkey.com/blog/2009/09/14/jqueryconf-2009/</link>
		<comments>http://www.quirkey.com/blog/2009/09/14/jqueryconf-2009/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 22:32:33 +0000</pubDate>
		<dc:creator>AQ</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.quirkey.com/blog/?p=356</guid>
		<description><![CDATA[First of all jQueryConf was straight up awesome. The organizers did a great job and I met a staggering number of cool, intellegent people. I think the key (at least for me) of why it was so much fun, was it wasn&#8217;t a ruby conference. What I mean by that is it wasn&#8217;t just other [...]]]></description>
			<content:encoded><![CDATA[	<p>First of all jQueryConf was straight up awesome. The organizers did a great job and I met a staggering number of cool, intellegent people. I think the key (at least for me) of why it was so much fun, was it wasn&#8217;t a ruby conference. What I mean by that is it wasn&#8217;t just other Rubyists, with similar day jobs, and similar interests. Thats fun of course, but the thing with jQuery and JavaScript in general, is it is very few peoples&#8217; full time jobs. That means that there were devs from every neck of the web development woods. That meant for me, hearing a ton of new and different perspectives and getting a whiff of all the cool stuff going on <span class="caps">OUTSIDE</span> of the Ruby world.</p>

	<p>I think my talk was pretty successful and besides not getting through all my slides, it seems like I got a lot of people excited about CouchDB and Sammy.js. I&#8217;m going to write a pretty in-depth recap of the talk and post it soon.</p>

	<p>Also, It sounds like there will be a lot more jQuery and JavaScript conferences in the near future. I can&#8217;t wait.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://www.quirkey.com/blog/2009/09/14/jqueryconf-2009/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sammy.js hits a new milestone (0.2.0)</title>
		<link>http://www.quirkey.com/blog/2009/06/12/sammyjs-hits-a-new-milestone-020/</link>
		<comments>http://www.quirkey.com/blog/2009/06/12/sammyjs-hits-a-new-milestone-020/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 17:40:15 +0000</pubDate>
		<dc:creator>AQ</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Sammy]]></category>

		<guid isPermaLink="false">http://www.quirkey.com/blog/?p=325</guid>
		<description><![CDATA[Thanks to the support and help of an awesome community, Sammy.js hit 0.2.0 on Monday. You can see the full release notes here and the API doc has been updated to reflect the changes. My favorite feature from this update might be the simplest. Rendering partials now uses a special swap() method on Sammy.Application. By [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to the support and help of an awesome community, <a href="http://code.quirkey.com/sammy">Sammy.js</a> hit 0.2.0 on Monday. You can see the <a href="http://groups.google.com/group/sammyjs/browse_thread/thread/add71f66703fe74f">full release notes here</a> and the <a href="http://code.quirkey.com/sammy/docs/api.html">API doc</a> has been updated to reflect the changes.</p>

<p>My favorite feature from this update might be the simplest. </p>

<p>Rendering partials now uses a special <code>swap()</code> method on <code>Sammy.Application</code>. By default it just uses jQuery's <code>html()</code> method to swap out the old content of <code>$element</code> with the new content. The power though, is you easily override this to provide a cool transition when a new partial is rendered.</p>

<pre class="textmate-source"><span class="source source_js source_js_jquery"><span class="storage storage_type storage_type_js">var</span> app <span class="keyword keyword_operator keyword_operator_js">=</span> <span class="keyword keyword_operator keyword_operator_js">$</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>sammy<span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="storage storage_type storage_type_js">function</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
  
  <span class="meta meta_function meta_function_js"><span class="support support_class support_class_js">this</span>.<span class="entity entity_name entity_name_function entity_name_function_js">swap</span> <span class="keyword keyword_operator keyword_operator_js">=</span> <span class="storage storage_type storage_type_function storage_type_function_js">function</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_begin punctuation_definition_parameters_begin_js">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_js">content</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_end punctuation_definition_parameters_end_js">)</span></span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
    <span class="storage storage_type storage_type_js">var</span> width <span class="keyword keyword_operator keyword_operator_js">=</span> <span class="support support_class support_class_js support_class_js_jquery">$</span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">(</span><span class="meta meta_selector meta_selector_jquery"><span class="support support_class support_class_js">window</span></span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">)</span><span class="support support_function support_function_js support_function_js_jquery">.width</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
    <span class="variable variable_language variable_language_js">this</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span><span class="keyword keyword_operator keyword_operator_js">$</span>element<span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span>
      <span class="support support_function support_function_js support_function_js_jquery">.wrapInner</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>&lt;div class="inner"&gt;&lt;/div&gt;<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span>
      <span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span><span class="support support_function support_function_js support_function_js_jquery">children</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_js">(</span><span class="meta meta_selector meta_selector_jquery"><span class="punctuation punctuation_definition punctuation_definition_selector punctuation_definition_selector_begin punctuation_definition_selector_begin_js">'</span><span class="meta meta_selector meta_selector_css"><span class="entity entity_other entity_other_attribute-name entity_other_attribute-name_class entity_other_attribute-name_class_css"><span class="punctuation punctuation_definition punctuation_definition_attribute-name punctuation_definition_attribute-name_css">.</span>inner</span></span><span class="punctuation punctuation_definition punctuation_definition_selector punctuation_definition_selector_end punctuation_definition_selector_end_js">'</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_js">)</span>
        <span class="support support_function support_function_js support_function_js_jquery">.animate</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>queue: <span class="constant constant_language constant_language_boolean constant_language_boolean_false constant_language_boolean_false_js">false</span><span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">, </span>left: <span class="string string_quoted string_quoted_double string_quoted_double_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">"</span>-<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">"</span></span> <span class="keyword keyword_operator keyword_operator_js">+</span> width<span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">, </span><span class="constant constant_numeric constant_numeric_js">400</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span>
        <span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span><span class="support support_function support_function_js support_function_js_jquery">remove</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_js">()</span>
        <span class="support support_function support_function_js support_function_js_jquery">.end</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span>
      <span class="support support_function support_function_js support_function_js_jquery">.html</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span>content<span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span>
      <span class="support support_function support_function_js support_function_js_jquery">.css</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>left: width<span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span>
      <span class="support support_function support_function_js support_function_js_jquery">.animate</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>left: <span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>0px<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">, </span>width: width<span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">, </span><span class="constant constant_numeric constant_numeric_js">400</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
  <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
  
  <span class="variable variable_language variable_language_js">this</span><span class="support support_function support_function_js support_function_js_jquery">.get</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>#/user/:name<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">, </span><span class="storage storage_type storage_type_js">function</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
    <span class="variable variable_language variable_language_js">this</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>partial<span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>name.erb<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">, </span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>name: <span class="variable variable_language variable_language_js">this</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>params<span class="meta meta_brace meta_brace_square meta_brace_square_js">[</span><span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>name<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_brace meta_brace_square meta_brace_square_js">]</span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
  <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
  
  <span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span>...
</span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span></span></pre>

<p>Now, when you go to '#/user/aq' or similar, it will use the swap method to do a nifty transition, instead of ye old <code>html()</code>.</p>

<p>Theres more examples and Sammy on the way. Come join the mailing list or hang out in irc to learn or contribute.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.quirkey.com/blog/2009/06/12/sammyjs-hits-a-new-milestone-020/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Switched to jQuery</title>
		<link>http://www.quirkey.com/blog/2009/02/25/switched-to-jquery/</link>
		<comments>http://www.quirkey.com/blog/2009/02/25/switched-to-jquery/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 14:45:04 +0000</pubDate>
		<dc:creator>AQ</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.quirkey.com/blog/?p=227</guid>
		<description><![CDATA[Its been a little while now, but ever since Duane Johnson gently nudged me in the direction of jQuery instead of Prototype.js, I haven&#8217;t really looked back. The pull was obvious, a lot of the smartest people in the Ruby community love jQuery. I couldn&#8217;t really get it, though. Chaining is cool, but its really [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a little while now, but ever since <a href="http://blog.inquirylabs.com/" title="">Duane Johnson</a> gently nudged me in the direction of <a href="http://jquery.com" title="">jQuery</a> instead of <a href="http://www.prototypejs.org/">Prototype.js</a>, I haven&#8217;t really looked back.</p>

	<p>The pull was obvious, a lot of the <a href="http://yehudakatz.com" title="">smartest</a> <a href="http://ozmm.org" title="">people</a> in the Ruby community love jQuery. I couldn&#8217;t really get it, though. Chaining is cool, but its really just a parlor trick. What hooked me was the community. Just like Ruby (and unlike Prototype for the most part) jQuery has a thriving community of bloggers, plugin authors, and mailing lists. Part of jQuery is that is inherently extensible. Sure, prototype.js has Object.addMethods() but jQuery allows for easy packaging and distribution of new functionality. I&#8217;m sure I&#8217;m not the first one to say this, but its very true.</p>
<span id="more-227"></span>

	<p>The one thing that kind of hung me up at first was how to organize my code. In Prototype, I used Class.create() a lot to encapsulate functionality for elements and areas of the site. Even using Class.create() with name-spacing like:</p>

<pre class="textmate-source"><span class="source source_js source_js_jquery"><span class="storage storage_type storage_type_js">var</span> MyApp <span class="keyword keyword_operator keyword_operator_js">=</span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{}</span>

MyApp<span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>Feature <span class="keyword keyword_operator keyword_operator_js">=</span> Class<span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>Create<span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
  <span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">...</span>
<span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span>
</span></pre>

	<p>I was kind of lost how to do this easily and effectively with jQuery. Do I create plugins for all of this functionality? I could, but that seemed messy to add to the global jQuery name-space when really what I wanted was an Object. <a href="http://ejohn.org/blog/simple-javascript-inheritance/">John Resig to the rescue</a>. 25 lines of code and now I have my Classes beautifully encapsulated. An interesting design pattern I happened upon with this, is using Class.extend() and jquery&#8217;s $.fn.data() to associate groups an instance of a Class with an element on-screen.</p>

	<p>For example:</p>

<pre class="textmate-source"><span class="source source_js source_js_jquery">
<span class="storage storage_type storage_type_js">var</span> Block <span class="keyword keyword_operator keyword_operator_js">=</span> Class<span class="support support_function support_function_js support_function_js_jquery">.extend</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
  
  <span class="meta meta_function meta_function_json meta_function_json_js"><span class="entity entity_name entity_name_function entity_name_function_js">init</span>: <span class="storage storage_type storage_type_function storage_type_function_js">function</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_begin punctuation_definition_parameters_begin_js">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_js">element</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_end punctuation_definition_parameters_end_js">)</span></span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
    <span class="variable variable_language variable_language_js">this</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span><span class="keyword keyword_operator keyword_operator_js">$</span>element <span class="keyword keyword_operator keyword_operator_js">=</span> <span class="support support_class support_class_js support_class_js_jquery">$</span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">(</span><span class="meta meta_selector meta_selector_jquery">element</span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>  
  <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span>
  <span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> ... 
</span>  <span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> methods that can operate on an instance of block
</span>  <span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> and its elements.
</span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span>

<span class="support support_class support_class_js support_class_js_jquery">$</span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">(</span><span class="meta meta_selector meta_selector_jquery"><span class="punctuation punctuation_definition punctuation_definition_selector punctuation_definition_selector_begin punctuation_definition_selector_begin_js">'</span><span class="meta meta_selector meta_selector_css"><span class="entity entity_other entity_other_attribute-name entity_other_attribute-name_class entity_other_attribute-name_class_css"><span class="punctuation punctuation_definition punctuation_definition_attribute-name punctuation_definition_attribute-name_css">.</span>block</span></span><span class="punctuation punctuation_definition punctuation_definition_selector punctuation_definition_selector_end punctuation_definition_selector_end_js">'</span></span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">)</span><span class="support support_function support_function_js support_function_js_jquery">.each</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="storage storage_type storage_type_js">function</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span> <span class="support support_class support_class_js support_class_js_jquery">$</span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">(</span><span class="meta meta_selector meta_selector_jquery"><span class="variable variable_language variable_language_js">this</span></span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">)</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span><span class="support support_constant support_constant_dom support_constant_dom_js">data</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>block<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">, </span><span class="meta meta_class meta_class_instance meta_class_instance_constructor"><span class="keyword keyword_operator keyword_operator_new keyword_operator_new_js">new</span> <span class="entity entity_name entity_name_type entity_name_type_instance entity_name_type_instance_js">Block</span></span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="variable variable_language variable_language_js">this</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">))</span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
</span></pre>

	<p>This is instead of:</p>

<pre class="textmate-source"><span class="source source_js source_js_jquery">
<span class="keyword keyword_operator keyword_operator_js">$</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>fn<span class="support support_function support_function_js support_function_js_jquery">.extend</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
  <span class="meta meta_function meta_function_json meta_function_json_js"><span class="entity entity_name entity_name_function entity_name_function_js">block</span>: <span class="storage storage_type storage_type_function storage_type_function_js">function</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_begin punctuation_definition_parameters_begin_js">(</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_end punctuation_definition_parameters_end_js">)</span></span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
    <span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> a bunch of anonymous or private methods
</span>  <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span>
<span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span>

<span class="support support_class support_class_js support_class_js_jquery">$</span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">(</span><span class="meta meta_selector meta_selector_jquery"><span class="punctuation punctuation_definition punctuation_definition_selector punctuation_definition_selector_begin punctuation_definition_selector_begin_js">'</span><span class="meta meta_selector meta_selector_css"><span class="entity entity_other entity_other_attribute-name entity_other_attribute-name_class entity_other_attribute-name_class_css"><span class="punctuation punctuation_definition punctuation_definition_attribute-name punctuation_definition_attribute-name_css">.</span>block</span></span><span class="punctuation punctuation_definition punctuation_definition_selector punctuation_definition_selector_end punctuation_definition_selector_end_js">'</span></span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">)</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>block<span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
</span></pre>


	<p>The benefit of the Class way, is that now you always can just fetch and operate on the instance of Block for a particular element.</p>

<pre class="textmate-source"><span class="source source_js source_js_jquery">
<span class="support support_class support_class_js support_class_js_jquery">$</span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">(</span><span class="meta meta_selector meta_selector_jquery"><span class="punctuation punctuation_definition punctuation_definition_selector punctuation_definition_selector_begin punctuation_definition_selector_begin_js">'</span><span class="meta meta_selector meta_selector_css"><span class="entity entity_other entity_other_attribute-name entity_other_attribute-name_class entity_other_attribute-name_class_css"><span class="punctuation punctuation_definition punctuation_definition_attribute-name punctuation_definition_attribute-name_css">.</span>block</span>:first</span><span class="punctuation punctuation_definition punctuation_definition_selector punctuation_definition_selector_end punctuation_definition_selector_end_js">'</span></span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_js">)</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span><span class="support support_constant support_constant_dom support_constant_dom_js">data</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>block<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span>someBlockMethod<span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
</span></pre>
	<p>I need to dive a little deeper to see if there's a way to easily plugin-ize this pattern as I seem to be using it a bunch.</p>

	<p>There are still a few small things I miss from Prototype, but for the most part either someone has ported them, or it pretty easy to do it yourself. For example, Function#bind():</p>

<pre class="textmate-source"><span class="source source_js source_js_jquery">
<span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> works like Prototype's Function#bind
</span><span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> in Prototype:
</span><span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span>     $('element').observe('click', this.clickMethod.bind(this));
</span><span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span> with jQuery + $.shove:
</span><span class="comment comment_line comment_line_double-slash comment_line_double-slash_js"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_js">//</span>     $('#element).click($.shove(this.clickMethod, this));
</span> 
<span class="keyword keyword_operator keyword_operator_js">$</span><span class="support support_function support_function_js support_function_js_jquery">.extend</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
  <span class="meta meta_function meta_function_json meta_function_json_js"><span class="entity entity_name entity_name_function entity_name_function_js">shove</span>: <span class="storage storage_type storage_type_function storage_type_function_js">function</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_begin punctuation_definition_parameters_begin_js">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_js">fn, object</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_end punctuation_definition_parameters_end_js">)</span></span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
    <span class="keyword keyword_control keyword_control_js">return</span> <span class="storage storage_type storage_type_js">function</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">()</span> <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">{</span>
      <span class="keyword keyword_control keyword_control_js">return</span> fn<span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span><span class="support support_function support_function_js">apply</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span>object<span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">, </span>arguments<span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
    <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span>
  <span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span>
<span class="meta meta_brace meta_brace_curly meta_brace_curly_js">}</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span></span></pre>

	<p>Here are some great resources that I&#8217;ve found have made the whole switch lovely:</p>

	<ul>
		<li><a href="http://www.learningjquery.com/" title="">Learning jQuery</a></li>
		<li><a href="http://docs.jquery.com/" title="">The jQuery Documentation</a></li>
		<li><a href="http://remysharp.com/downloads/jquery-api-browser.air.zip" title="">A desktop version of the jQuery docs</a></li>

		<li><a href="http://malsup.com/jquery/" title="">Reading Mike Alsup&#8217;s code</a></li>
	</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.quirkey.com/blog/2009/02/25/switched-to-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

