<?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>Web 3.0, 6 Bladed Razors, 7 Minute Abs &#187; jQuery</title>
	<atom:link href="http://www.zachleat.com/web/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zachleat.com/web</link>
	<description></description>
	<lastBuildDate>Fri, 30 Jul 2010 01:59:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Quick Performance Tip: jQuery and addClass</title>
		<link>http://www.zachleat.com/web/2009/06/30/quick-performance-tip-jquery-and-addclass/</link>
		<comments>http://www.zachleat.com/web/2009/06/30/quick-performance-tip-jquery-and-addclass/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 04:51:01 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/?p=226</guid>
		<description><![CDATA[Abstractions are helpful and dangerous. But the more we know about a library&#8217;s internals, the less danger we&#8217;ll be in later. Here&#8217;s an issue I ran into where I had assumed that jQuery would be optimized for this case, but it wasn&#8217;t. I&#8217;ll go over my bad assumption and how to workaround it. As of [...]]]></description>
			<content:encoded><![CDATA[<p>Abstractions are helpful and dangerous.  But the more we know about a library&#8217;s internals, the less danger we&#8217;ll be in later.  Here&#8217;s an issue I ran into where I had assumed that jQuery would be optimized for this case, but it wasn&#8217;t.  I&#8217;ll go over my bad assumption and how to workaround it.</p>
<p>As of jQuery 1.3.2, adding multiple HTML classes to an element using jQuery&#8217;s addClass method will add them one at a time, modifying the className property of an element for each class.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myElement'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myFirstClass mySecondClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here&#8217;s the original code inside of jQuery 1.3.2.  Note how the classNames string is split, and elem.className is changed for each split entry.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">add<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> elem<span style="color: #339933;">,</span> classNames <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>classNames <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s+/</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> className<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> elem.<span style="color: #660066;">nodeType</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>jQuery.<span style="color: #660066;">className</span>.<span style="color: #660066;">has</span><span style="color: #009900;">&#40;</span> elem.<span style="color: #660066;">className</span><span style="color: #339933;">,</span> className <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
            elem.<span style="color: #660066;">className</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>elem.<span style="color: #660066;">className</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> className<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #006600; font-style: italic;">// ...</span></pre></div></div>

<p>This may cause longer than needed delays, as reflow may occur after every class is added individually.  If absolutely necessary, you can always fall back to modifying the className yourself, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myElement'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">' myFirstClass mySecondClass'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Most likely, this isn&#8217;t a tip that will be needed, but it is useful to be aware of.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2009/06/30/quick-performance-tip-jquery-and-addclass/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Performance Caveat with jQuery Selectors and Live Events</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/</link>
		<comments>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/#comments</comments>
		<pubDate>Fri, 08 May 2009 13:00:04 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Event Delegation]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211</guid>
		<description><![CDATA[Prerequisite: Knowledge/Experience with jQuery Live Events (new in jQuery 1.3), and the concept of Event Delegation. When developing on the front end, it&#8217;s easy to prioritize correctness over performance. Performance is the step child that gets lost while you&#8217;re pulling your hair out worrying about cross browser compatibility. It&#8217;s very important to regularly benchmark your [...]]]></description>
			<content:encoded><![CDATA[<p><em>Prerequisite: Knowledge/Experience with <a href="http://docs.jquery.com/Events/live">jQuery Live Events</a> (new in jQuery 1.3), and the concept of <a href="http://icant.co.uk/sandbox/eventdelegation/">Event Delegation</a>.</em></p>
<p>When developing on the front end, it&#8217;s easy to prioritize correctness over performance.  Performance is the step child that gets lost while you&#8217;re pulling your hair out worrying about cross browser compatibility.  It&#8217;s very important to regularly benchmark your JavaScript code, using a <a href="http://getfirebug.com/js.html">profiler</a> or some form of benchmarking code paired with a cross browser logging utility (see <a href="http://getfirebug.com/lite.html">Firebug Lite</a>, <a href="http://developer.yahoo.com/yui/logger/">YUI Logger</a>, or <a href="http://log4javascript.org/">log4javascript</a>).</p>
<p>Event delegation is a great way to program for performance.  The <code>live</code> jQuery method was a great addition to the jQuery core, it makes event delegation really easy (see also the <code>closest</code> method).  Unfortunately, it isn&#8217;t quite what I expected.</p>
<p>For example, say you have a page containing approximately 500 custom tooltip components on it (not typical, but stick with me, this is to prove a point). How might one go about adding a simple live event to activate each tooltip when the user hovers over it?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span.myTooltip'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// activate tooltip</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>See the problem?  jQuery will actually run the selector on the document, resulting in unnecessary overhead. jQuery is only assigning a single event handler to top level of the document, why does it need to know what nodes it will be binding to before assigning the callback?</p>
<p>What can we do?  Let&#8217;s create a jQuery function, instead of a method, so it won&#8217;t query the document.  Try this on for size:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">live</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>selector<span style="color: #339933;">,</span> type<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    r.<span style="color: #660066;">selector</span> <span style="color: #339933;">=</span> selector<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>type <span style="color: #339933;">&amp;&amp;</span> fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        r.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> r<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Usage</h2>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Single event type</span>
$.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span.myTooltip'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// activate tooltip</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Multiple event types (you can call the jQuery live method on the return value from the function)</span>
$.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span.myTooltip'</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// activate tooltip</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseout'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// deactivate tooltip</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Also, as a side note, keep in mind that jQuery <code>live</code> <strong>doesn&#8217;t</strong> support space separated events, like <code>bind</code> does.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Will not work.</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span.myTooltip'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover mouseout'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Jonathan Sharp at The Ajax Experience</title>
		<link>http://www.zachleat.com/web/2009/02/20/jonathan-sharp-at-the-ajax-experience/</link>
		<comments>http://www.zachleat.com/web/2009/02/20/jonathan-sharp-at-the-ajax-experience/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 03:35:51 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[People]]></category>
		<category><![CDATA[Ajaxian]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[The Ajax Experience]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/?p=200</guid>
		<description><![CDATA[Former co-worker and all around good guy Jonathan Sharp gave a presentation at The Ajax Experience about a project that he developed, and subsequently passed onto me around 3Q2008.]]></description>
			<content:encoded><![CDATA[<p>Former co-worker and all around good guy <a href="http://jdsharp.us/">Jonathan Sharp</a> gave a presentation at The Ajax Experience about a project that he developed, and subsequently passed onto me around 3Q2008.  A custom built UI component library based on jQuery, used by 450+ developers on 80+ internal and external web applications.  You wouldn&#8217;t think it, but railroads have pretty large IT departments. This presentation was <a href="http://ajaxian.com/archives/jquery-ajax-experience-framework-videos">posted on Ajaxian</a> this week.  Enjoy!</p>
<p><embed src="http://services.brightcove.com/services/viewer/federated_f8/1596744118" bgcolor="#FFFFFF" flashVars="videoId=1828663222&#038;playerId=1596744118&#038;viewerSecureGatewayURL=https://console.brightcove.com/services/amfgateway&#038;servicesURL=http://services.brightcove.com/services&#038;cdnURL=http://admin.brightcove.com&#038;domain=embed&#038;autoStart=false&#038;" base="http://admin.brightcove.com" name="flashObj" width="486" height="412" seamlesstabbing="false" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2009/02/20/jonathan-sharp-at-the-ajax-experience/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scare Your Visitors with this JavaScript Gravatar Plugin</title>
		<link>http://www.zachleat.com/web/2009/01/08/scare-your-visitors-with-this-javascript-gravatar-plugin/</link>
		<comments>http://www.zachleat.com/web/2009/01/08/scare-your-visitors-with-this-javascript-gravatar-plugin/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 03:47:43 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Gravatar]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/?p=157</guid>
		<description><![CDATA[Here's a use case.  An unregistered visitor visits your blog, and decides that your <strong>content is so good that it merits a comment</strong>!  Congratulations, you've fooled them!  But since they're leaving a comment, why not show them a preview of their gravatar?]]></description>
			<content:encoded><![CDATA[<h2><a href="/javascript/gravatar/index.html">See the Demo</a></h2>
<h2><a href="/javascript/gravatar/jquery.gravatar.js">Download the Source Code</a></h2>
<p><br/><br />
<br/></p>
<p><img src="http://www.zachleat.com/web/wp-content/uploads/2009/01/blog-comment.png" alt="Tournology Blog Comment Form" title="blog-comment" width="484" height="266" class="size-full wp-image-158" /></p>
<p>Here&#8217;s a use case.  An unregistered visitor visits your blog, and decides that your <strong>content is so good that it merits a comment</strong>!  Congratulations, you&#8217;ve fooled them!  Now you can <a href="http://www.centernetworks.com/twply-twitter-replies-auction">twply their account details for $1200 on Sitepoint</a>!  Just kidding.  But generally, when you visit a blog&#8217;s commenting section (such as the <a href="http://www.tournology.com/blog/">Tournology Blog</a> shown above), you&#8217;ll see a simple form to authenticate you&#8217;re not a spammer, generally including (among other things) an e-mail address field.</p>
<p><br/><br />
<br/></p>
<p><img src="http://www.zachleat.com/web/wp-content/uploads/2009/01/blog-comment-after.png" alt="Tournology Blog Comment Form With Gravatar" title="blog-comment-after" width="484" height="266" class="size-full wp-image-160" /></p>
<p>Well, since they&#8217;re typing their e-mail address, wouldn&#8217;t it be cool if we could <strong>show them their gravatar</strong> right there, inline with the blog comment form?  Well, that&#8217;s now possible with my new <strong><a href="http://www.zachleat.com/javascript/gravatar/jquery.gravatar.js">JavaScript Gravatar Plugin</a></strong>!  It doesn&#8217;t have any server side language dependencies.</p>
<p><br/><br />
<br/></p>
<p><img src="http://www.zachleat.com/web/wp-content/uploads/2009/01/gravatar-signup.png" alt="Gravatar Signup Page" title="gravatar-signup" width="483" height="265" class="size-full wp-image-162" /></p>
<p>Hell, <a href="http://en.gravatar.com/">gravatar.com</a> could even use this to <strong>improve the user experience of registering your e-mail account</strong>.  Right now it does a full page refresh and doesn&#8217;t even show you a preview!</p>
<p><br/></p>
<h2><a href="/javascript/gravatar/index.html">See the Demo</a></h2>
<h2><a href="/javascript/gravatar/jquery.gravatar.js">Download the Source Code</a></h2>
<p><br/></p>
<h2>Licensing</h2>
<p>
Licensed under the <a href="http://sam.zoy.org/wtfpl/">WTFPL</a>, as highly recommended by <a href="http://foohack.com/">Isaac Schleuter</a> (<a href="/web/2007/04/05/google-using-yui-grids-css/">see discussion</a>).
</p>
<p><br/></p>
<h2>JavaScript Dependencies:</h2>
<ul>
<li>Requires <a href="http://jquery.com">jQuery</a> (Feel free to port and post a link!)</li>
<li>Requires <a href="http://pajhome.org.uk/crypt/md5/md5.js">md5.js</a></li>
</ul>
<p><br/></p>
<h2>Example Usage:</h2>
<p>Easiest form, onblur of email text input field:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#emailTextField'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">after</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">gravatar</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Showing all options, again onblur of email text input field.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#email'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">gravatar</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// integer size: between 1 and 512, default 80 (in pixels)</span>
        size<span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span>
        <span style="color: #006600; font-style: italic;">// maximum rating (in order of raunchiness, least to most): g (default), pg, r, x</span>
        rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'pg'</span><span style="color: #339933;">,</span>
        <span style="color: #006600; font-style: italic;">// url to define a default image (can also be one of: identicon, monsterid, wavatar)</span>
        image<span style="color: #339933;">:</span> <span style="color: #3366CC;">'identicon'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2009/01/08/scare-your-visitors-with-this-javascript-gravatar-plugin/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Selecting XML Nodes with JavaScript (Peril of getElementsByTagName)</title>
		<link>http://www.zachleat.com/web/2008/05/10/selecting-xml-with-javascript/</link>
		<comments>http://www.zachleat.com/web/2008/05/10/selecting-xml-with-javascript/#comments</comments>
		<pubDate>Sat, 10 May 2008 23:27:02 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/?p=132</guid>
		<description><![CDATA[Parsing XML in the browser can be a tricky beast.  There are many different wrong ways to do it, which can leave you cold and naked in a snowstorm if you're not careful.  So, let's put on the metaphorical electric one-sie of standards based code and let the power of Edison heat our JavaScript code like the innards of a <a href="http://starwars.wikia.com/wiki/Tauntaun">tauntaun</a>.]]></description>
			<content:encoded><![CDATA[<p><em>There are two popular camps for ajax data formats right now: XML and JSON.  Both have their (dis-)advantages.  The purpose of this article is  to show you how to effectively parse XML in the browser.</em></p>
<h3>Super Fast Beginner&#8217;s Primer</h3>
<ul>
<li>Case 1: <strong>Node</strong> (or un-namespaced node, null-namespaced node): a node without a prefix, such as child here:<br />
<code>&lt;child/&gt;</code></li>
<li>Case 2: <strong>Default namespaced node</strong>: a node without a prefix, but a parent node (or itself) has a xmlns attribute, like both root and child here:<br />
<code>&lt;root xmlns="http://example.com/"&gt;&lt;child/&gt;&lt;/root&gt;</code></li>
<li>Case 3: <strong>Namespaced node</strong>: a node with a prefix, and a parent node (or itself) declaring a xmlns with that prefix attached, like both child and root here:<br />
<code>&lt;prefix:root prefix:xmlns="http://example.com/"&gt;&lt;prefix:child/&gt;&lt;/root&gt;</code></li>
</ul>
<h3>/End Primer</h3>
<p>Parsing XML in the browser can be a tricky beast.  There are many different wrong ways to do it, which can leave you cold and naked in a snowstorm if you&#8217;re not careful.  So, let&#8217;s put on the metaphorical electric one-sie of standards based code and let the power of Edison heat our JavaScript code like the innards of a <a href="http://starwars.wikia.com/wiki/Tauntaun">tauntaun</a>.</p>
<p>If there is one thing you can take away from this article, its that the problems with XML in JavaScript have already been solved, and there is library code out there to do the job for you.  But libraries aren&#8217;t a substitute for knowledge (abstraction is a dangerous thing during education), so let&#8217;s learn <strong>why</strong> these problems are occurring so we can wrinkle our gray matter and increase our productivity at the same time.</p>
<h2>Use Cases</h2>
<p>These are the main use cases that takes place when selecting a node inside of an XML document:</p>
<ol>
<li><strong>Case 1</strong>: Selecting un-namespaced nodes (or nodes in the null namespace):

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;root<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;child</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This one is easy.  If you can guarantee that your XML will never have any namespaces, you&#8217;re home free.  Take your get out of jail free card and run for the hills.  Using this assumption, you can query nodes inside of your XML Document object using nothing other than <code>getElementsByTagName()</code>.  Lucky bastard.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// assume oDocEl is the documentElement inside of an XML Document </span>
<span style="color: #003366; font-weight: bold;">var</span> correctForCase1 <span style="color: #339933;">=</span> oDocEl.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'child'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li><strong>Case 2</strong>: Selecting default namespaced nodes:

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;root</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://example.com/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;child</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Tread lightly, this is about to get serious.  In most cases, historically I had thought that using the solution described for Case 1 would be sufficient in this case.  I had learned awhile back that Internet Explorer treats node names (including namespace prefix and local name together) as one string.  So, the method for Case 1 should work for Internet Explorer, especially in the case of node sans prefix.  In Firefox, you&#8217;d have to use getElementsByTagNS(), but that would be just a simple wrapper.</p>
<p>Then I met an Internet Explorer exception.  The only unique thing about this installation of Internet Explorer 7 was that it had MSXML 6 installed, when all the other computers I had tested on were using MSXML 3.  The obvious conclusion here is that MSXML 6 won&#8217;t select child nodes for Case 2.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> incorrectForCase2 <span style="color: #339933;">=</span> oDocEl.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'child'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here&#8217;s the right way to select nodes for Case 2.  Fair warning, to keep the code examples here simple, this solution requires Sarissa (sarissa.js and sarissa_ieemu_xpath.js) to be included on the page prior to usage.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// assume oDoc is an XML Document object.</span>
oDoc.<span style="color: #660066;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;SelectionNamespaces&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;xmlns:whatever='http://example.com/'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> oDocEl <span style="color: #339933;">=</span> oDoc.<span style="color: #660066;">documentElement</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> correctForCase2A <span style="color: #339933;">=</span> oDocEl.<span style="color: #660066;">selectNodes</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'whatever:child'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> correctForCase2B <span style="color: #339933;">=</span> oDocEl.<span style="color: #660066;">selectSingleNode</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'whatever:child'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note how we&#8217;ve mapped what was the default namespace (without a prefix) to be a namespace WITH a prefix during the node selection.</p>
<p>It should be noted that when the resultant XML has a namespace attached (Case 2 and 3), Firefox works fine using getElementsByTagNameNS.  IE doesn&#8217;t include support for that method, however, so we&#8217;re forced to find a more complete solution.
</li>
<li><strong>Case 3</strong>: Select a non-default namespaced node:

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;root</span> <span style="color: #000066;">prefix:xmlns</span>=<span style="color: #ff0000;">&quot;http://example.com/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;prefix:child</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>As I mentioned in Case 2, normally (pre-MSXML 6), you&#8217;d be able to perform a <code>getElementsByTagName('prefix:child')</code> in IE and use getElementsByTagNameNS in Firefox as usual.  But that has changed now.  We need to set up the SelectionNamespaces property for IE, and we&#8217;ll use Sarissa to take it cross-browser for us.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// assume oDoc is an XML Document object.</span>
oDoc.<span style="color: #660066;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;SelectionNamespaces&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;xmlns:whatever='http://example.com/'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> oDocEl <span style="color: #339933;">=</span> oDoc.<span style="color: #660066;">documentElement</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> correctForCase3A <span style="color: #339933;">=</span> oDocEl.<span style="color: #660066;">selectNodes</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'whatever:child'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> correctForCase3B <span style="color: #339933;">=</span> oDocEl.<span style="color: #660066;">selectSingleNode</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'whatever:child'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Note, this is the same code as Case 2 (which is a good thing)</span></pre></div></div>

<p>Note that we did <em>not</em> have to use the same prefix that was defined by the result XML.  We can map it to whatever we want (literally).
</li>
</ol>
<h2>Why is this important?</h2>
<p>Because most libraries don&#8217;t handle Case 2 and Case 3, which are important parts of XML.  Here&#8217;s some code straight from YUI 2.5.1 (DataSource component):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Line 1394</span>
<span style="color: #003366; font-weight: bold;">var</span> xmlNode <span style="color: #339933;">=</span> result.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>xmlNode <span style="color: #339933;">&amp;&amp;</span> xmlNode.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> xmlNode.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">firstChild</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    data <span style="color: #339933;">=</span> xmlNode.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
       data <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice how they just do getElementsByTagName.  For shame :(  jQuery doesn&#8217;t handle Case 2 or Case 3 either.  (Proof is an exercise to the reader :P)  So, if you have XML data sources with namespaces, it would do you well to use the solution presented in this article, or you&#8217;re going to have headaches later.</p>
<h2>Springer&#8217;s Final Word</h2>
<p>Don&#8217;t use getElementsByTagName.  If you do, PLEASE include a note saying that your code isn&#8217;t going to support namespaced XML.  Branch your selection code to check if Sarissa has been included on the page, and use Sarissa for namespaced XML if it&#8217;s there.  It&#8217;s not fun to be pidgin-holed into the simplest case of XML.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2008/05/10/selecting-xml-with-javascript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Rethinking JavaScript Grids and DataTables</title>
		<link>http://www.zachleat.com/web/2008/04/13/rethinking-javascript-grids-and-datatables/</link>
		<comments>http://www.zachleat.com/web/2008/04/13/rethinking-javascript-grids-and-datatables/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 20:29:26 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[Interface Design]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Conservative Design]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[ExtJS]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/?p=122</guid>
		<description><![CDATA[In the world of front end engineering, one must consider the end-user of the interface first, and above all other things. The priorities should not start with development ease, nor external library preference. The priorities should start with the needs of the consumer of your end product. Evolution of your engineering skill is also a [...]]]></description>
			<content:encoded><![CDATA[<p>In the world of front end engineering, one must consider the end-user of the interface first, and above all other things.  The priorities should not start with development ease, nor external library preference.  The priorities should start with the needs of the consumer of your end product.</p>
<p>Evolution of your engineering skill is also a vital trait in this world, which means that as a developer increases his knowledge of good practices and proper methods, sometimes he must shirk his previous assertions about the world as he previously knew it.  And today I&#8217;m shirking a staple of the front end as all web users know it: The Grid (DataTable) Component.</p>
<p>Of course, I&#8217;ve written a few articles in the past about the <a href="http://developer.yahoo.com/yui/datatable/">YUI DataTable</a>, during my long love affair with Yahoo&#8217;s User Interface library.  Another popular one is jQuery&#8217;s <a href="http://tablesorter.com/docs/">TableSorter</a>.  Then there&#8217;s the <a href="http://dojotoolkit.org/book/dojo-book-0-9/docx-documentation-under-development/grid">Dojo Grid</a>, a component <a href="http://www.sitepen.com/blog/2007/09/16/the-dojo-grid/">inherited from TurboAjax</a>.  ExtJS has a variety of nice examples as well for their <a href="http://extjs.com/deploy/dev/examples/#sample-1">Ext 2.0 Grid</a>.</p>
<p>And after using these Grids and DataTables, I certainly respect the programming that went into developing these components.  But let&#8217;s take a step back for a second.  Why do the users need the bells and whistles in these components?  Are they worth the extra load time and complexity they add to the interface?</p>
<p>All we&#8217;re doing here is putting a nice coat of paint on a &lt;table&gt; tag.  Sure, it might have some nice ancillary features like Ajax Data Loading, but those don&#8217;t really matter &#8211; they are things that can be easily performed with some good Ajax and DOM insert utility functions.  In fact, most of the core features included in these components could be described as feature creep, and not beneficial to the end user at all.  Feature creep contributes to code bloat, which means the user is downloading bytes to their web browser that they don&#8217;t need, which can hamper performance.  Libraries usually have online examples of their components, and the include sizes are seen below.  (Gzip compression not considered)</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>JavaScript Size</th>
<th>Minimized</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<tr>
<td>YUI DataTable</td>
<td>216.6 KB</td>
<td>Minimized</td>
<td><a href="http://developer.yahoo.com/yui/examples/datatable/dt_basic_clean.html">Example</a></td>
</tr>
<tr>
<td>Dojo Grid</td>
<td>338.4 KB</td>
<td>Unminimized</td>
<td><a href="http://dojotoolkit.org/book/dojo-book-0-9/docx-documentation-under-development/grid/simple-grid">Example</a></td>
</tr>
<tr>
<td>Ext Grid</td>
<td>545.5 KB</td>
<td>Minimized</td>
<td><a href="http://extjs.com/deploy/dev/examples/grid/array-grid.html">Example</a></td>
</tr>
<tr>
<td>jQuery TableSorter</td>
<td>66 KB</td>
<td>Minimized</td>
<td><a href="http://www.tablesorter.com/">Example</a></td>
</tr>
</tbody>
</table>
<p>The JavaScript include sizes listed above are directly proportional to the feature set that the components provide, and should give you an idea of the overhead involved with using them.  Do we need 545.5 KB of features coming down the pipe to give our users an extra bell, or an extra whistle?  Let&#8217;s analyze the features to rationalize their usage, and remove items from the feature set.</p>
<ul>
<li>Sorting</li>
<li>Modifying column order and display</li>
<li>Resizing columns</li>
<li>Editing of row data directly on the grid itself</li>
<li>Scrolling</li>
<li>Pagination</li>
</ul>
<p><strong>Sorting</strong></p>
<p>Data should be used in the context of its usefulness.  You have a list of messages in your e-mail inbox.  What&#8217;s the most useful context for this list?  In order of date received.  The default sort order provided by the application, to facilitate proper use of the application.  Is comparing the rows in the grid by any other method as useful?  Does the user need to see the list of messages ordered alphabetically by subject?  In these cases where the user is in need of a specific message, <em>searching and filtering</em> is more useful than sorting.  The default sort is useful, but allowing the user to resort on the client, in most cases, is not as useful as other methods of finding a row.</p>
<p><strong>Modifying column order and display</strong></p>
<p>The same argument can be made for any of the other methods of customization provided to the end user.  Does the user need to reorder or hide columns?  The context provided by the application should be sufficient to use the applications data in the way it was intended.  Don&#8217;t overcomplicate your user interface with needless features or a deluge of useless data.  Provide succinct, appropriate data, and the user needn&#8217;t reorder or change the interface.</p>
<p><strong>Resizing columns</strong></p>
<p>The default HTML &lt;table&gt; tag expands to fit the data inside of its cells.  Even when you set the width of the table explicitly, the cells adjust themselves accordingly to fit the data.  This should be the behavior of your table.  You needn&#8217;t monkey around with widths, the browser is smart enough to do it for you.  You can even customize a cell to wrap its text to multiple lines with CSS, if need be.</p>
<p><strong>Editing of row data directly on the grid itself</strong></p>
<p>If you&#8217;re providing an Excel spreadsheet interface for the end user to customize your data, you haven&#8217;t designed your interface correctly.  Rethink how the user needs to interact with the data you&#8217;ve provided, and give them a better, simpler way to edit the data.</p>
<p><strong>Scrolling</strong></p>
<p>Everyone knows that internal scrollbars on a page are evil.  I don&#8217;t even like scrollbars on textareas, to be honest.  Previously, I had worked and reworked the YUI DataTable to handle horizontal scrolling.  Looking back on this, it was a mistake.  There are better ways to handle lots of data in a table, without the heavy mouse interaction and scanning that scrolling require.  Which brings me to my next point.</p>
<p><strong>Pagination</strong></p>
<p>This is the one exception to the feature set cutting board.  This is the one feature that&#8217;s is a requirement, when the data set has too many records to fit on a single page.</p>
<p>Keep these in mind, and look at the feature set provided by a few sites using tabular data centric interfaces that know a thing or two about interface design:</p>

<a href='http://www.zachleat.com/web/2008/04/13/rethinking-javascript-grids-and-datatables/gmail/' title='Google Mail'><img width="150" height="150" src="http://www.zachleat.com/web/wp-content/uploads/2008/04/gmail-150x150.png" class="attachment-thumbnail" alt="Google Mail" title="Google Mail" /></a>
<a href='http://www.zachleat.com/web/2008/04/13/rethinking-javascript-grids-and-datatables/wordpress-admin/' title='Wordpress 2.5 Admin Interface'><img width="150" height="150" src="http://www.zachleat.com/web/wp-content/uploads/2008/04/wordpress-admin-150x150.png" class="attachment-thumbnail" alt="Wordpress 2.5 Admin Interface" title="Wordpress 2.5 Admin Interface" /></a>
<a href='http://www.zachleat.com/web/2008/04/13/rethinking-javascript-grids-and-datatables/google-reader/' title='Google Reader List View'><img width="150" height="150" src="http://www.zachleat.com/web/wp-content/uploads/2008/04/google-reader-150x150.png" class="attachment-thumbnail" alt="Google Reader List View" title="Google Reader List View" /></a>

<p><strong>So, what should we include?</strong></p>
<p>A simple CSS class to style your table is sufficient, with links to paginate the table (properly) and/or a hover for row selection if needed.  You&#8217;re looking at 10-20 lines of jQuery code, maximum, and a few CSS declarations.  In lieu of sorting, of course, you&#8217;ll need to program in a mechanism for searching and filtering as well.  But really, the difficulty with programming this component is knowing what to leave out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2008/04/13/rethinking-javascript-grids-and-datatables/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ALARMd 2 Beta, with Google Calendar Integration</title>
		<link>http://www.zachleat.com/web/2008/04/06/alarmd-2-beta-with-google-calendar-integration/</link>
		<comments>http://www.zachleat.com/web/2008/04/06/alarmd-2-beta-with-google-calendar-integration/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 07:31:59 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[Interface Design]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Alarmd]]></category>
		<category><![CDATA[Google Calendar]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Youtube]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/?p=111</guid>
		<description><![CDATA[Take a look: ALARMd 2 Beta Update: added Metric and Unit Circle time formats. I know, some of you are reading this and thinking to yourself &#8212; genital herpes is more appealing than yet another online alarm clock. But to that I say, congratulations, that&#8217;s one of the new features in ALARMd 2! Why did [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.zachleat.com/Projects/alarmd-beta/">Take a look: ALARMd 2 Beta</a></p>
<p><em><strong>Update</strong>: added Metric and Unit Circle time formats.</em></p>
<p>I know, some of you are reading this and thinking to yourself &#8212; genital herpes is more appealing than yet another online alarm clock.  But to that I say, congratulations, that&#8217;s one of the new features in ALARMd 2!</p>
<p>Why did I make another online alarm clock?  Mostly due to missing features and limitations in the old version, but also because it&#8217;s a good exercise in JavaScript programming and User Interface design.  Everyone likes to hone their skills, and this is my publicly viewable work desk.  But the real reason I went back to rewrite the old version is that I&#8217;m hooked on <a href="http://jquery.com">jQuery</a>.  I can&#8217;t get enough of that sweet, sweet, source code, and couldn&#8217;t stand to see my old, crusty, handwritten DOM manipulations polluting web browsers across the world.  Users of the Yahoo User Interface Library (what I used for the original version of ALARMd) would do well to consider jQuery a nice plugin to be used alongside YUI.  It will clean up your code MAX_INT-fold.</p>
<p>Here are a few new features and addressed limitations in ALARMd 2.</p>
<ul>
<li>Easy Alarm Mode: No more fumbling around when you just want one simple easy-to-add alarm.</li>
<li>Google Calendar Alarm Mode: Customize your alarm schedule to your heart&#8217;s content, it will load your alarms straight from a publicly available Google Calendar.  There are some great features with this:
<ul>
<li>Load only the first calendar event of every day</li>
<li>Day Limiter (Example: Only load calendar events within the next 3 days)</li>
<li>Minute Adjuster (Example: Alarm me 90 minutes before work without adding a separate event)</li>
<li>Google does a nice job of normalizing dates as well, so you don&#8217;t have to worry about calendar time syncing.  If it says 8AM on your calendar, it&#8217;s going to alarm you at 8AM on your computer&#8217;s local time.</li>
</ul>
</li>
<li>New Clock Formats:
<ul>
<li>Human Readable Clock Format: Think &#8220;Half Past Two&#8221;, or &#8220;Quarter Til Twelve&#8221;.  This idea is from <a href="http://www.insightoutsight.co.uk/viewproject.php?cid=2&#038;pid=3&#038;iid=2">Laurence Willmott&#8217;s Project &#8220;It&#8217;s about Time&#8221;</a>.  I took some liberties with his labeling scheme, I hope he doesn&#8217;t mind too much.</li>
<li><a href="http://zapatopi.net/metrictime/">Metric Time Format</a>: Shows the measurement Centi-days in Local Metric Time.  Basically, it&#8217;s a percentage of much of the day has passed.  If it&#8217;s 80.000, 80 percent of the day has passed, which coincides with 7:12 PM.</li>
<li>Unit Circle Time Format: Displays the time in radians that would be shown if a clock were <a href="http://www.cafepress.com/poofietomato.49111330">pasted on top of a unit circle</a>.  If it&#8217;s 12 o&#8217;clock, it will read &pi;/2.  After programming this one, it&#8217;s starting to seem normal in my brain.  Oh, it&#8217;s 3&pi;/2?  Time to eat dinner.  How the hell did it get 2&pi;/3??</li>
</ul>
</li>
<li><a href="http://www.zachleat.com/web/2008/03/23/yet-another-pretty-date-javascript/">Human Readable Alarm Dates</a>: Think &#8220;29 Minutes&#8221;, &#8220;1 Hour&#8221;, &#8220;2 Days Ago&#8221;.</li>
<li>Much cleaner interface, using a jQuery accordion to display the options.</li>
<li>The old ALARMd required an internet connection for all sources, and provided no safeguard if your internet went down whilst you were sleeping.  ALARMd 2 preloads all Youtube videos in the background when the page loads using the new <a href="http://apiblog.youtube.com/2008/03/something-to-write-home-about.html">Youtube JavaScript API</a>.  A nice benefit of this is that the browser window no longer requires focus to play the YouTube video.</li>
<li>Less clunky interface for adding new alarm sources.</li>
<li>Repeat option for YouTube videos and MP3&#8242;s.  Turn infinite loop on or off.  Careful with this one.  Don&#8217;t leave ALARMd going if you&#8217;re not going to be home when it goes off.</li>
<li>CSS Skins, Use the really simple ones I&#8217;ve included for Red, Green, or Blue, or include your own URL to your own hosted CSS file.  Have a good skin?  I&#8217;ll include it in the select list and give you some props here, just link to it in the comments below.  Use some CSS class hooks to spice up your skins (they are mutually exclusive).  These are CSS classes that are added to the body tag to allow you to style the alarm differently depending on the alarm clock&#8217;s current state.  <em>Future enhancements</em> might include more than just alarm-based hooks: Year, day of the year, and hour of the day might be useful, that way you could style the clock to show a lighter background during the day and a darker background at night.
<ul>
<li>.alarmWithin30Minutes</li>
<li>.alarmWithin15Minutes</li>
<li>.alarmWithin5Minutes</li>
<li>.alarmWithin1Minute</li>
<li>.alarmActive (Alarm is being played)</li>
</ul>
</li>
<li>Still has all the old favorites:
<ul>
<li>Test Button to make sure the video or source is working and to check your volume.</li>
<li>Count Down mode to show the time between now and the first alarm.</li>
<li>Store your own list of alarm sources, using YouTube, MP3, Last.FM, or any URL (Pandora is included).</li>
<li>Military time (24 hour clock) and seconds toggle.</li>
<li>Naked mode (get rid of the extras)</li>
</ul>
</li>
</ul>
<p>Finished reading?  I&#8217;m surprised you didn&#8217;t click the link at the top: <a href="http://www.zachleat.com/Projects/alarmd-beta/">ALARMd 2 Beta</a></p>
<p>Remember, this is BETA.  That means it&#8217;s new.  I&#8217;ve done my best to test and code out all the kinks, but there may be a few that slipped through.  Try it out and please report any bugs.  And once again, this code is released under the BSD license.</p>
<p>I&#8217;ll probably move this to the main ALARMd.com domain shortly.</p>
<p>Alarmd has been personally tested with Firefox 2, Firefox 3, Internet Explorer 7, Safari 3.1, and Opera 9.27, all on Windows.  Google GDATA reports an unsupported browser error on Safari and Opera, which you can hide using custom CSS.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2008/04/06/alarmd-2-beta-with-google-calendar-integration/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Tips for Programming JavaScript Functions</title>
		<link>http://www.zachleat.com/web/2008/03/28/tips-for-programming-javascript-functions/</link>
		<comments>http://www.zachleat.com/web/2008/03/28/tips-for-programming-javascript-functions/#comments</comments>
		<pubDate>Fri, 28 Mar 2008 14:32:27 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2008/03/28/tips-for-programming-javascript-functions/</guid>
		<description><![CDATA[This article is about my personal coding style, given little tips and tricks that I use to make my code cleaner and more readable. Required and Optional Arguments There are generally two styles used when programming a new function in JavaScript. The first, most obvious, and least extensible method is putting each argument as its [...]]]></description>
			<content:encoded><![CDATA[<p><em>This article is about my personal coding style, given little tips and tricks that I use to make my code cleaner and more readable.</em></p>
<h2>Required and Optional Arguments</h2>
<p>There are generally two styles used when programming a new function in JavaScript.  The first, most obvious, and least extensible method is putting each argument as its own argument in the function definition, as so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// For our purposes, assume arg1 is required, and arg2 and arg3 are optional.</span>
<span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span>arg1<span style="color: #339933;">,</span> arg2<span style="color: #339933;">,</span> arg3<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   arg2 <span style="color: #339933;">=</span> arg2 <span style="color: #339933;">||</span> <span style="color: #3366CC;">'defaultValue'</span><span style="color: #339933;">;</span>
   arg3 <span style="color: #339933;">=</span> arg3 <span style="color: #339933;">||</span> <span style="color: #3366CC;">'defaultValue'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>One obvious downside to this method is that it requires a line of code for each optional argument to define a default value.  What if you want to pass in an arg3, but not an arg2?  Your call could end up like: <code>myFunction('myArg1', null, 'myArg3');</code>.  This might work in the beginning, but what about when you have more than 3 arguments defined?  That&#8217;s going to get messier than a Dick Cheney hunting party.</p>
<p>The next method people generally move to when they have a lot of optional arguments in their function definition is to put all the arguments into a single object argument, like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// taken from jQuery</span>
 $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
   type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
   url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;some.php&quot;</span><span style="color: #339933;">,</span>
   data<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;name=John&amp;location=Boston&quot;</span><span style="color: #339933;">,</span>
   success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The benefit to this method is that you can populate all the defaults easily by using jQuery.extend, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// simulated code, not from jQuery</span>
$.<span style="color: #660066;">ajax</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> defaultArgs <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span>
        data<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>
        dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">'text'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// overwrites defaultArgs with args values, stores result into args.</span>
    args <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> defaultArgs<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>args<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The args object would end up with the following value:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
   type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
   url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;some.php&quot;</span><span style="color: #339933;">,</span>
   data<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;name=John&amp;location=Boston&quot;</span><span style="color: #339933;">,</span>
   success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
   dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">'text'</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This method can be easily seen as extensible.  You could put a metric shit ton of optional arguments in your function (the option is there, I won&#8217;t judge you), and you could define them in any order inside of the object without having to put in null spacers like the previous method.</p>
<p>But what about required arguments?  The Ajax method above requires a URL, and the script can&#8217;t guess a default for that variable.  But it isn&#8217;t immediately obvious to the end-user that URL is always required.  And what&#8217;s more, you&#8217;ll have to put specific code inside your function to check that the required arguments were sent in the object.  (Think <code>if(!args['url']) { return; }</code> as shown two code blocks up.)</p>
<p>Which leads me to my favorite way to define functions: a combination of the two methods.  Declare your required arguments as explicit arguments, and pile the rest into an optional arguments object.  Here I&#8217;ll show you how I would define the Ajax function using this method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// jQuery doesn't use this argument structure, so don't copy and paste this.</span>
 $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'some.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
   type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
   data<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;name=John&amp;location=Boston&quot;</span><span style="color: #339933;">,</span>
   success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This way, it is immediately obvious which is a required argument and which is optional, and your required arguments will never be accidentally assigned a default value in your code.  If the end-user doesn&#8217;t send in all of the required arguments, the resulting error message will be obvious and easily fixed, without a ton of code inside of your function to check that the end-user obeyed the function defintion.</p>
<h2>Setting Default Values for Optional Arguments</h2>
<p>When developing code, sometimes it is obvious that the method described above for optional arguments may be overkill for a tiny little utility method.  Sometimes you just want a boolean flag, or a single optional argument.  When that occassion arrives, it&#8217;s time to put on your robe and coding hat, because we&#8217;re going to town.</p>
<p>In the beginning of JavaScript maturity,  my &lt;body&gt; was going through a lot of changes.  I was confused about a lot of things, and you&#8217;d see a lot of lines of code like this (embarrassingly enough):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span>runDoSomething<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>runDoSomething <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          runDoSomething <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// never make your default true, as I'll show you below</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>runDoSomething<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ugh, checking against null?  Janet Reno looked better in the morning.  But now that I&#8217;m a full grown man that can open his own pickle jars, I&#8217;ve got some guidelines:</p>
<ol>
<li>When the argument is optional and might be left out, define it in your code in such a way that if it does have a value, it will not evaluate to boolean false (0, false, undefined, null, &#8221;, NaN).  In other words, don&#8217;t make the value the user passes in for this argument be false.  False is the value it should have when it&#8217;s empty.  See the example below:

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// runDoSomething is still optional</span>
<span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span>runDoSomething<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>runDoSomething<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We didn&#8217;t have to assign a default value to arg1, because if it&#8217;s <code>null</code>, then <code>doSomething()</code> won&#8217;t execute.  Don&#8217;t ever use arg1 in a way that would have you passing in false, because false and null are the same in a boolean comparison.
</li>
<li>When using non-boolean optional arguments, another way to avoid assigning a default value to an optional argument is to use the OR operator to give you a default value where you&#8217;re using it.  This is really only useful when you use the argument in one single place inside of your function.  If you&#8217;re using it more than once, it&#8217;s best to define the default value at the top of the function.

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span>arg1<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// arg1 is used only once</span>
     jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myId'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>arg1 <span style="color: #339933;">||</span> <span style="color: #3366CC;">'myDefaultValue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span>arg1<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     arg1 <span style="color: #339933;">=</span> arg1 <span style="color: #339933;">||</span> <span style="color: #3366CC;">'myDefaultValue;
     // arg1 is used multiple times
     jQuery('</span>#myId<span style="color: #3366CC;">').html(arg1);
     jQuery('</span>#myOtherId<span style="color: #3366CC;">').html(arg1);
}</span></pre></div></div>

</li>
</ol>
<p>Hopefully you&#8217;ve learned something from this post.  It is the byproduct of many mistakes and overcomplicated functions that I&#8217;ve refactored to end up with this result.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2008/03/28/tips-for-programming-javascript-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Architecture Choices: Callbacks and Events in JavaScript</title>
		<link>http://www.zachleat.com/web/2008/03/28/architecture-choices-callbacks-and-events-in-javascript/</link>
		<comments>http://www.zachleat.com/web/2008/03/28/architecture-choices-callbacks-and-events-in-javascript/#comments</comments>
		<pubDate>Fri, 28 Mar 2008 06:03:11 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Callbacks]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2008/03/28/architecture-choices-dependent-function-execution-in-javascript/</guid>
		<description><![CDATA[Warning: Blog Post written for Beginner and Intermediate JavaScript Developers Like any well intentioned programmer, you&#8217;re writing reusable code in JavaScript. Maybe it&#8217;s a simple widget, maybe it&#8217;s a higher level plug-in for your favorite JavaScript library. But now you want to provide a mechanism for your friendly neighborhood developer to extend your code by [...]]]></description>
			<content:encoded><![CDATA[<p><em>Warning: Blog Post written for Beginner and Intermediate JavaScript Developers</em></p>
<p>Like any well intentioned programmer, you&#8217;re writing reusable code in JavaScript.  Maybe it&#8217;s a simple widget, maybe it&#8217;s a higher level plug-in for your favorite JavaScript library.  But now you want to provide a mechanism for your friendly neighborhood developer to extend your code by hooking into it with a little bit of code of their own.  How do you accomplish this?</p>
<p>Generally, it can be done one of two ways:</p>
<ul>
<li>A callback function</li>
<li>A custom event</li>
</ul>
<p>A callback function is a function passed into your code as an argument that will be executed at the time the library code specifies.  For instance, callback functions are usually supplied for for Ajax XmlHttpRequest&#8217;s to execute, one callback if the Ajax Request is successful, and another on failure.  See the following code as an example:</p>
<p>End-Developer&#8217;s Code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
   type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
   url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;some.php&quot;</span><span style="color: #339933;">,</span>
   data<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;name=John&amp;location=Boston&quot;</span><span style="color: #339933;">,</span>
   success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;Data Saved: &quot;</span> <span style="color: #339933;">+</span> msg <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Library Code (from jQuery):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// taken out of context, just know that s stands for the options object passed into $.ajax() above.</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> s.<span style="color: #660066;">success</span> <span style="color: #009900;">&#41;</span>
	s.<span style="color: #660066;">success</span><span style="color: #009900;">&#40;</span> data<span style="color: #339933;">,</span> <span style="color: #000066;">status</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the above code example taken straight from the <a href="http://docs.jquery.com/Ajax/jQuery.ajax#options">jQuery documentation</a>, the success key of the object being passed into the ajax() function is a callback function.  It will be executed after the Ajax request has successfully completed.</p>
<p>The other mechanism you can use for controlling dependent function execution is a custom event.  Custom events provide more flexability because they use the publish/subscribe mechanism.  That means, instead of the library author deciding how many callbacks he or she is going to allow you to pass into their method as arguments, he or she will just trigger a custom event, which will fire all functions the end-developer has said they want to subscribe to that event.  See the following example from the jQuery documentation.</p>
<p>End-Developer&#8217;s Code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;myCustomEvent&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;Data Saved: &quot;</span> <span style="color: #339933;">+</span> msg <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;myCustomEvent&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;More Data Saved: &quot;</span> <span style="color: #339933;">+</span> msg <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// We can subscribe as many functions as we want to myCustomEvent.</span></pre></div></div>

<p>Library Code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// again, no context here</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">trigger</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;myCustomEvent&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span> <span style="color: #3366CC;">&quot;My Message&quot;</span> <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see above, custom events are much more extensible and customizable to your needs.  Want to attach 10 functions to myCustomEvent?  Sure, go right ahead.  Want to try to attach 10 callbacks to the ajax success method above, that&#8217;s going to be a bit more work.  The benefit to using callbacks lies in their disposability and isolation.  Perhaps you don&#8217;t want to publish to the world when your Ajax Request completes &#8212; maybe you just want to handle the completion and be done with it.</p>
<p>Let&#8217;s Review.</p>
<p><strong>Callback Functions</strong>: + Private + Disposable &#8211; Limited by Design of Parent Code (Number of callbacks)<br />
<strong>Custom Events</strong>: +Extensible (Any number of functions attached) &#8211; Public &#8211; Not Disposable (Functions stay attached after execution, and continue to fire when triggered)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2008/03/28/architecture-choices-callbacks-and-events-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yet Another Pretty Date JavaScript</title>
		<link>http://www.zachleat.com/web/2008/03/23/yet-another-pretty-date-javascript/</link>
		<comments>http://www.zachleat.com/web/2008/03/23/yet-another-pretty-date-javascript/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 02:53:34 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Dates]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2008/03/23/yet-another-pretty-date-javascript/</guid>
		<description><![CDATA[I can&#8217;t let this Pretty Date thing go. I decided to use a modification of John Resig&#8217;s Pretty Date JavaScript implementation written by Dean Landolt and shared in the comments on John&#8217;s page. The script was an obvious choice for the next iteration of Alarmd, which is nearing completion as I type. The more I [...]]]></description>
			<content:encoded><![CDATA[<p>I can&#8217;t let this Pretty Date thing go.  I decided to use a modification of <a href="http://ejohn.org/blog/javascript-pretty-date/">John Resig&#8217;s Pretty Date JavaScript implementation</a> written by Dean Landolt and shared in the comments on John&#8217;s page.  The script was an obvious choice for the next iteration of <a href="http://www.zachleat.com/web/2007/06/18/wake-up-to-youtube-on-my-internet-alarm-clock/">Alarmd</a>, which is nearing completion as I type.</p>
<p>The more I used <a href="http://deanlandolt.com/archives/163">Dean Landolt&#8217;s script</a>, the more problems I began to see with his implementation.  It was a good start, but definitely had bugs.  His assumptions translating from integer second differences to human readable labels stretched too far at times (there is an error in logic to say anything between 24 hours and 48 hours from now can be labeled &#8220;Tomorrow&#8221;), and he was a bit loose with his difference categories (assumed average month length was 28 days, and always used Math.floor instead of rounding &#8212; 47 hours from now would be labeled &#8220;1 Day&#8221;).  It was great code otherwise, and I definitely liked the way he used the while loop to run through the comparisons.</p>
<p>So, I&#8217;ve cleaned up his great start, and am releasing it to the world in the spirit of cooperation and open sauce.  You just read that typo out loud didn&#8217;t you?</p>
<p><strong>Download</strong>: <a href="http://www.zachleat.com/Lib/jquery/humane.js">Yet Another Pretty Date Implementation</a> (2 KB)</p>
<p><strong>Update</strong>: Dates for this script must have a specific <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO8601 format</a>: <em>YYYY-MM-DDTHH:MM:SSZ</em> (in UTC) where T and Z are literals.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2008/03/23/yet-another-pretty-date-javascript/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Namespacing outside of the YAHOO Namespace</title>
		<link>http://www.zachleat.com/web/2007/08/28/namespacing-outside-of-the-yahoo-namespace/</link>
		<comments>http://www.zachleat.com/web/2007/08/28/namespacing-outside-of-the-yahoo-namespace/#comments</comments>
		<pubDate>Wed, 29 Aug 2007 01:34:24 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Namespacing]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2007/08/28/namespacing-outside-of-the-yahoo-namespace/</guid>
		<description><![CDATA[YAHOO.namespace(). A lovely little utility function subject that I&#8217;ve written about before. If you&#8217;ve never heard of YAHOO.namespace or aren&#8217;t even familiar with namespacing, I&#8217;d read that article first. I&#8217;ll be honest, using the YAHOO namespace to store my own code makes my bunghole tighten just a little bit. What if I had written code [...]]]></description>
			<content:encoded><![CDATA[<p>YAHOO.namespace().  A lovely little utility function subject that <a href="http://www.zachleat.com/web/2007/08/09/yui-code-review-yahoonamespace/">I&#8217;ve written about before</a>.  If you&#8217;ve never heard of YAHOO.namespace or aren&#8217;t even familiar with namespacing, I&#8217;d read that article first.</p>
<p>I&#8217;ll be honest, using the YAHOO namespace to store my own code makes my bunghole tighten just a little bit.  What if I had written code stored under YAHOO.tool, which was unused prior to YUI 2.3.0?  What would I do now?  I&#8217;d have to rewrite my code, or never include any of the wonderful <code>YAHOO.tool.TestCase</code>, put together by <a href="http://www.nczonline.net/">Nicholas Zakas</a>.  As is traditional with most of my weblog posts, I try not to just complain about a problem without giving you a solution (but let&#8217;s be honest, only if it doesn&#8217;t take too much work).</p>
<p>Let&#8217;s rewrite the YAHOO.namespace function to work outside of the YAHOO Namespace, so we can do things like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'zachsWorld.partyTime'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
zachsWorld.<span style="color: #660066;">partyTime</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'Excellent.'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
zachsWorld.<span style="color: #660066;">partyTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// obviously would alert: Excellent.</span></pre></div></div>

<p>Here&#8217;s some code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> a<span style="color: #339933;">=</span>arguments<span style="color: #339933;">,</span> o<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> j<span style="color: #339933;">,</span> d<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>a.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">=</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        d<span style="color: #339933;">=</span>a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        o<span style="color: #339933;">=</span>window<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j<span style="color: #339933;">&lt;</span>d.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">=</span>j<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            o<span style="color: #009900;">&#91;</span>d<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>o<span style="color: #009900;">&#91;</span>d<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            o<span style="color: #339933;">=</span>o<span style="color: #009900;">&#91;</span>d<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> o<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Obviously this is just a modification of the YAHOO.namespace function.  I&#8217;d recommend putting this under your own namespace (I&#8217;m trying to put most of the code I write on this website under the Y2 namespace, but everyone should have their own parent namespace), like so (any namespaces you create using this function won&#8217;t use the parent namespace assigned here by default):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> Y2 <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> Y2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
Y2.<span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* copy function contents from above. */</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But you can do whatever you want.  This is one of the functions that I think separates YUI from the other frameworks out there, giving an easy utility to organize your code.  This is something I think jQuery seriously lacks, noting from the code inside the many jQuery plugins contributed by end users.  Many jQuery contributors do not organize their code, putting multiple unnecessary top level functions into jQuery.fn as methods, or into the jQuery object itself as functions.  People whine about polluting the global namespace&#8230; I suppose it might be considered nitpickey to whine about the jQuery namespace.  Okay you whiney bastard, let&#8217;s help out and put this into jQuery!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// include jQuery first.</span>
jQuery.<span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> a<span style="color: #339933;">=</span>arguments<span style="color: #339933;">,</span> o<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> j<span style="color: #339933;">,</span> d<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>a.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">=</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        d<span style="color: #339933;">=</span>a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        o<span style="color: #339933;">=</span>window<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j<span style="color: #339933;">&lt;</span>d.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">=</span>j<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            o<span style="color: #009900;">&#91;</span>d<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>o<span style="color: #009900;">&#91;</span>d<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            o<span style="color: #339933;">=</span>o<span style="color: #009900;">&#91;</span>d<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> o<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Please note, this method does NOT work using jQuery methods with an element context, such as jQuery(&#8216;div&#8217;).myNamespace.myMethod().  Your element context won&#8217;t be carried through to myMethod().</p>
<p>However, you can use it for jQuery functions, such as this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// definition</span>
jQuery.<span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'jQuery.debug'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jQuery.<span style="color: #660066;">debug</span>.<span style="color: #660066;">test1</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'test1 function'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
jQuery.<span style="color: #660066;">debug</span>.<span style="color: #660066;">test2</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'test2 function'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// usage</span>
jQuery.<span style="color: #660066;">debug</span>.<span style="color: #660066;">test1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jQuery.<span style="color: #660066;">debug</span>.<span style="color: #660066;">test2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>For more information, you can read <a href="http://yuiblog.com/blog/2006/06/01/global-domination/">Global Domination, an article written by Douglas Crockford</a>, or <a href="http://docs.jquery.com/Plugins/Authoring">Best Practices for Writing jQuery plugins</a>.</p>
<p><b>Updated</b>: fixed some weird source parsing errors introduced by the code formatting plugin.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2007/08/28/namespacing-outside-of-the-yahoo-namespace/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using DOM Query Libraries in YUI</title>
		<link>http://www.zachleat.com/web/2007/07/30/using-dom-query-libraries-in-yui/</link>
		<comments>http://www.zachleat.com/web/2007/07/30/using-dom-query-libraries-in-yui/#comments</comments>
		<pubDate>Mon, 30 Jul 2007 07:37:03 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[CSS Selectors]]></category>
		<category><![CDATA[DED|Chain]]></category>
		<category><![CDATA[ExtJS]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Packer]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2007/07/30/using-dom-query-libraries-in-yui/</guid>
		<description><![CDATA[Recently, I posted the top 8 things I thought the YUI Library needed to be a top tier JavaScript library again. One of those things included a CSS Selector DOM Querying class. Use one of these babies for awhile, and you&#8217;ll never be able to code without it again. They&#8217;re amazingly useful and will shorten [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I posted the top 8 things I thought the YUI Library needed to be a top tier JavaScript library again.  One of those things included a CSS Selector DOM Querying class.  Use one of these babies for awhile, and you&#8217;ll never be able to code without it again.  They&#8217;re amazingly useful and will shorten your code quite beautifully.</p>
<p>So, let&#8217;s plug one of them into YUI!  Let&#8217;s get some of that power under the hood.  And let&#8217;s make it work identically to how we write our code for YUI currently, instead of changing the coding style (a la Dustin Diaz&#8217;s great extension to YUI called <a href="http://dedchain.dustindiaz.com/">DED|Chain</a>).</p>
<p>How are we going to do this?  Let&#8217;s dive into the shallow end of the pool.</p>
<p>The include order of our javascript files is going to be important.  So, first we&#8217;ll do the YUI library core file (either yahoo-dom-event.js or utilities.js depending on your need for animation).  Next, we need to include a CSS Selector DOM Querying class.  For this proof of concept, I&#8217;ve provided examples for my two favorites, the jQuery engine and Jack Slocum&#8217;s DomQuery.  These files are released under the MIT license, so make sure that complies with your project.  It&#8217;s pretty liberal, so you shouldn&#8217;t have any problems.  If you find these querying libraries useful, please donate to these projects!</p>
<p>For code simplicity, I&#8217;ve added a one liner to Jack&#8217;s DomQuery to make it work as a standalone file.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">typeof</span> Ext <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span> <span style="color: #009900;">&#41;</span> <span style="color: #003366; font-weight: bold;">var</span> Ext <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you want to read more about DomQuery, you can go to <a href="http://www.jackslocum.com/blog/2007/01/11/domquery-css-selector-basic-xpath-implementation-with-benchmarks/">Jack&#8217;s site all about syntax and benchmarks</a>.</p>
<p>Download: <a href="/Projects/Y2/DomQuery.js">Jack Slocum&#8217;s DomQuery Standalone File (24 KB)</a> 1.0 Alpha 3 &#8211; Rev 4<br />
Download: <a href="/Projects/Y2/DomQuery-packer.js">Jack Slocum&#8217;s DomQuery Standalone File Packed (7 KB)</a> 1.0 Alpha 3 &#8211; Rev 4</p>
<p>If you want to use jQuery&#8217;s selector engine, I&#8217;ve stripped out the functions unnecessary to the selector engine in the 1.1.3.1 release and packaged it up as jDomQuery.  There are differences between these two package&#8217;s syntax, so make sure you keep that in mind, especially when looking at the pseudos, like :gt() and :lt() for example.</p>
<p>Download: <a href="/Projects/Y2/jdomquery-1.1.3.1.js">jDomQuery Standalone File (23 KB)</a> 1.1.3.1<br />
Download: <a href="/Projects/Y2/jdomquery-1.1.3.1-packer.js">jDomQuery Standalone File Packed (10 KB)</a> 1.1.3.1</p>
<p>If you want to read about jQuery syntax, <a href="http://docs.jquery.com/Selectors">go over to their documentation</a>.  My current preference is the jQuery syntax, given that it <a href="http://www.zachleat.com/web/2007/07/10/javascript-frameworks-and-jsf/">works with poorly implemented JSF ID attributes</a>.  But that&#8217;s a different argument.</p>
<p>If you want to use the full jQuery library, that should work too, but if you want to do that, you might just want to switch to using jQuery at that point (dare I say it).</p>
<p>Next, we&#8217;ll extend the YAHOO.util.Dom class into a new Y2 namespace.  This will map all of the functions from YAHOO.util.Dom into a new CSS Selector extension.</p>
<h1>Download: <a href="/Projects/Y2/Dom.js">Y2.util.Dom (2 KB)</a></h1>
<p>(pack at your own leisure, you can do it online at <a href="http://dean.edwards.name/packer/">Dean Edward&#8217;s Packer</a>)</p>
<h1>Putting It All Together</h1>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
	&lt;body&gt;
		&lt;div&gt;
			&lt;div class=&quot;first&quot;&gt;
				&lt;span&gt;&lt;/span&gt;
			&lt;/div&gt;
			&lt;div class=&quot;second&quot;&gt;
				&lt;b&gt;&lt;/b&gt;
			&lt;/div&gt;
		&lt;/div&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;/Lib/yui/build/yahoo-dom-event/yahoo-dom-event.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;jdomquery-1.1.3.1-packer.js&quot;&gt;&lt;/script&gt;
		&lt;!-- &lt;script type=&quot;text/javascript&quot; src=&quot;DomQuery-packer.js&quot;&gt; --&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;Dom.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot;&gt;
		Y2.util.Dom.addClass( 'div div:last', 'myClass' )
		Y2.util.Dom.hasClass( 'div div:last', 'myClass' ); // returns true
		var b = Y2.util.Dom.get( 'div div:last b' );
		Y2.util.Dom.addClass( b, 'myOtherClass' );
		&lt;/script&gt;
	&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>One important thing to note is that when using the CSS Selector syntax, when only one node is selected, functions will return a scalar instead of an array.  So if you&#8217;re expecting to select multiple nodes in your selector query and only one node results, make sure your code handling the result is prepared for such an instance.</p>
<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2007/07/30/using-dom-query-libraries-in-yui/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>JavaScript Frameworks and JSF</title>
		<link>http://www.zachleat.com/web/2007/07/10/javascript-frameworks-and-jsf/</link>
		<comments>http://www.zachleat.com/web/2007/07/10/javascript-frameworks-and-jsf/#comments</comments>
		<pubDate>Wed, 11 Jul 2007 02:24:05 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[ExtJS]]></category>
		<category><![CDATA[Facelets]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[Mootools]]></category>
		<category><![CDATA[Prototype]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2007/07/10/javascript-frameworks-and-jsf/</guid>
		<description><![CDATA[You&#8217;re programming a new web application using JSF, maybe with Facelets, maybe without. Which client-side JavaScript framework is going to work with it&#8217;s unstandardized method of assigning ID attributes to it&#8217;s elements? Here is an example of a JSF file: &#60;%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %&#62; &#60;%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %&#62; &#60;html&#62; &#60;body&#62; &#60;f:view&#62; &#60;h:form id="myForm"&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;re programming a new web application using JSF, maybe with Facelets, maybe without.  Which client-side JavaScript framework is going to work with it&#8217;s unstandardized method of assigning ID attributes to it&#8217;s elements?  Here is an example of a JSF file:<br />
<code><br />
&lt;%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %&gt;<br />
&lt;%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %&gt;<br />
&lt;html&gt;<br />
 &lt;body&gt;<br />
   &lt;f:view&gt;<br />
     &lt;h:form id="myForm"&gt;<br />
      &lt;h:inputText id="myText"/&gt;<br />
     &lt;/h:form&gt;<br />
   &lt;/f:view&gt;<br />
 &lt;/body&gt;<br />
&lt;/html&gt;<br />
</code></p>
<p>or maybe the same thing using Facelets:<br />
<code><br />
&lt;html xmlns="http://www.w3.org/1999/xhtml"<br />
      xmlns:h="http://java.sun.com/jsf/html"<br />
      xmlns:f="http://java.sun.com/jsf/core"&gt;<br />
 &lt;body&gt;<br />
   &lt;f:view&gt;<br />
     &lt;form jsfc="h:form" id="myForm"&gt;<br />
      &lt;input type="text" jsfc="h:inputText" id="myText"/&gt;<br />
     &lt;/form&gt;<br />
   &lt;/f:view&gt;<br />
 &lt;/body&gt;<br />
&lt;/html&gt;<br />
</code></p>
<p>These will both output the following result:<br />
<code><br />
&lt;html xmlns="http://www.w3.org/1999/xhtml"<br />
      xmlns:ui="http://java.sun.com/jsf/facelets"&gt;<br />
 &lt;body&gt;<br />
     &lt;form id="myForm"&gt;<br />
      &lt;input type="text" id="myForm:myText"/&gt;<br />
     &lt;/form&gt;<br />
 &lt;/body&gt;<br />
&lt;/html&gt;<br />
</code></p>
<p>Notice how the resulting text field has an ID attribute with the parent id prepended on.  Now the real question is, how do we select this result node using the various JavaScript frameworks that are available to us?</p>
<h1>Works, but we don&#8217;t care: Raw JavaScript or any GetById Function</h1>
<p>Obviously using <code>document.getElementById( 'myForm:myText' );</code> will function correctly.  As will any framework that has a similar &#8220;GetById&#8221; function (YAHOO.util.Dom.get, or $ in Prototype, etc)  What is more interesting to us is the behavior using the various CSS selector packages included with each JavaScript library.</p>
<h1>Does not work: <a href="http://extjs.com/deploy/ext/docs/output/Ext.DomQuery.html">DomQuery</a> in Ext 1.1 Beta 2</h1>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm:myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Ext.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm<span style="color: #000099; font-weight: bold;">\:</span>myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>These two tests will throw the error <code>Ext.DomQuery.pseudos[name] is not a function</code>, which obviously means that myText is not a pseudo class like :first or :last.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm<span style="color: #000099; font-weight: bold;">\\</span>:myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Throws the error: <code>Error parsing selector, parsing failed at "\:myText"</code></p>
<h1>Works: <a href="http://docs.jquery.com/DOM/Traversing/Selectors">jQuery</a> 1.1.3</h1>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm<span style="color: #000099; font-weight: bold;">\\</span>:myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As of version 1.1.3 (the latest of this writing), they have added support for the colon in ID selection when escaped with a double backslash.  Versions older than 1.1.3 will not function properly.  This is not yet in the documentation but can be viewed in the <a href="http://jquery.com/blog/2007/07/01/jquery-113-800-faster-still-20kb/">Escape selectors section of a blog post</a>.</p>
<h1>Does not Work: <a href="http://www.prototypejs.org/api/utility/dollar-dollar">Prototype</a> 1.5.1.1</h1>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm:myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm<span style="color: #000099; font-weight: bold;">\:</span>myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Returns the form instead of the input element and treats myText as a pseudo class.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm<span style="color: #000099; font-weight: bold;">\\</span>:myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Interprets as XPATH selectors (\\ means anywhere in the document), using myText as a pseudo selector now, so it returns ALL nodes in the document.</p>
<h1>Does not Work: <a href="http://dojotoolkit.org/node/336">Dojo query</a> 0.9.0 Beta</h1>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm:myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm<span style="color: #000099; font-weight: bold;">\:</span>myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm<span style="color: #000099; font-weight: bold;">\\</span>:myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It&#8217;s becoming mind numbingly obvious that if the framework doesn&#8217;t specifically say that you can escape selectors, it&#8217;s not going to let you do so.  In the first two tests, Dojo performs the same as Prototype.  In the double backslash test however, Dojo returns no result nodes.</p>
<h1>Does not Work: <a href="http://docs.mootools.net/Element/Element-Selectors.js">Mootools</a> 1.11</h1>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm:myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm<span style="color: #000099; font-weight: bold;">\:</span>myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'#myForm<span style="color: #000099; font-weight: bold;">\\</span>:myText'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All throw the error <code>The expression is not a legal expression." code: "51</code>.</p>
<p>There you have it folks.  jQuery is the JavaScript library of choice for the discerning JSF applications developer.  Kudos to you if you&#8217;re already using it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/2007/07/10/javascript-frameworks-and-jsf/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
