<?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>Higgins for President &#187; dojo</title>
	<atom:link href="http://higginsforpresident.net/category/dojo/feed/" rel="self" type="application/rss+xml" />
	<link>http://higginsforpresident.net</link>
	<description>Rants, Raves, and Code: the Interwebs and beyond</description>
	<lastBuildDate>Sun, 17 Jan 2010 15:20:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Widgets within Widgets</title>
		<link>http://higginsforpresident.net/2010/01/widgets-within-widgets/</link>
		<comments>http://higginsforpresident.net/2010/01/widgets-within-widgets/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 12:57:32 +0000</pubDate>
		<dc:creator>dante hicks</dc:creator>
				<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://higginsforpresident.net/?p=49</guid>
		<description><![CDATA[At the day-job we do a lot of Widget work. We have Container widgets that hold Panel widgets, which hold Box widgets, which hold other widgets. The widgets themselves create other widgets and place them in their own ownership. Our full-page/no-refresh/Ajax-app with the long lived page views creates and destroys all these widgets based on [...]]]></description>
			<content:encoded><![CDATA[<p>At the day-job we do a <em>lot</em> of Widget work. We have Container widgets that hold Panel widgets, which hold Box widgets, which hold other widgets. The widgets themselves create other widgets and place them in their own ownership. Our full-page/no-refresh/Ajax-app with the long lived page views creates and destroys all these widgets based on the various events published around the page, but we ran into a problem along the way: a lot of the widgets weren't being destroyed. Ever. </p>
<p>The whole system is quite sound, though lacked in this one regard. To be fair, Dijit cleans up after itself. Everything that is created when a Dijit widget instance is <em>new</em>'d up is removed when that instance is destroyed. Everything that is created declaratively in a template is cleaned up automatically for you when a widget is destroyed. The problem only exists when one widget creates another widget programatically, either for permanent placement or temporary use. Let me illustrate with a basic Widget example:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// file is my/Thinger.js</span>
dojo.<span style="color: #006600;">provide</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my.Thinger&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dijit._Widget&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dijit._Templated&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">declare</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my.Thinger&quot;</span>, <span style="color: #66cc66;">&#91;</span>dijit._Widget, dijit._Templated<span style="color: #66cc66;">&#93;</span>, <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">// the listItem in the template is &quot;tracked&quot; and cleaned</span>
    widgetsInTemplate:<span style="color: #003366; font-weight: bold;">true</span>,
    templateString:<span style="color: #3366CC;">&quot;
&lt;div&gt;
&lt;ul&gt;&lt;&quot;</span> + <span style="color: #3366CC;">&quot;li dojoType='my.ListItem' dojoAttachPoint=&quot;</span>li<span style="color: #3366CC;">&quot;&gt;${foo}&lt;/&quot;</span> + <span style="color: #3366CC;">&quot;li&gt;&lt;/ul&gt;
&lt;/div&gt;
&nbsp;
&quot;</span>,
&nbsp;
    postCreate: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// some self-cleaning event connections. These are disconnected upon destruction.</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">domNode</span>, <span style="color: #3366CC;">&quot;onclick&quot;</span>, <span style="color: #3366CC;">&quot;_clicker&quot;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #009900; font-style: italic;">// this item is leaked when Thinger is destroyed.</span>
        <span style="color: #003366; font-weight: bold;">new</span> my.<span style="color: #006600;">ListItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">placeAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">li</span>, <span style="color: #3366CC;">&quot;after&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Here, we're creating new <code>my.ListItem</code> instances and adding them to our own DOM, but not keeping track of them in any way. This is an exceptionally common pattern of code compartmentalization. The <code>my.Thinger</code> scopes all DOM references and event connections to itself. It contains other widgets which handle some part of the overall functionality. By making a whole <code>ListItem</code> Class, we compartmentalize <em>that</em> bit of logic into little pieces. </p>
<p>We can see the leak pretty easily:</p>
<pre class="javascript">&nbsp;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>dijit.<span style="color: #006600;">registry</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// 0</span>
<span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #003366; font-weight: bold;">new</span> my.<span style="color: #006600;">Thinger</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">placeAt</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>dijit.<span style="color: #006600;">registry</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// 3, A Thinger, and Two ListItem instances</span>
x.<span style="color: #006600;">destroy</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>dijit.<span style="color: #006600;">registry</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// 1. A ListItem</span>
&nbsp;</pre>
<p>So I'll show my solution to this little dilemma in hopes you can adopt a similar method to suite your own needs. My solution requires imposing a style guideline on <em>how</em> new widgets are bound and added to other widgets, but I feel it to be well worth the extra efforts. </p>
<p>The first thing we do, because all Dijit widgets clean up properly after themselves already, is make our own Base Widget class, extending dijit._Widget. Here we'll add a few functions to add new APIs to the lifecycle so we can track our own widgets.</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">provide</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my.Widget&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dijit._Widget&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">delcare</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my.Widget&quot;</span>, <span style="color: #66cc66;">&#91;</span> dijit._Widget, dijit._Templated <span style="color: #66cc66;">&#93;</span>, <span style="color: #66cc66;">&#123;</span>
    <span style="color: #009900; font-style: italic;">// summary: Our custom dijit._Widget base class. All our widgets should inherit from this. </span>
&nbsp;
    adopt: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>cls, props, srcNode<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// summary: Take ownership of a new widget</span>
        <span style="color: #009900; font-style: italic;">// returns: The added widget</span>
    <span style="color: #66cc66;">&#125;</span>,
&nbsp;
    orphan: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>widget, destroy<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// summary: relinquish ownership of an owned widget. optionally destroy.</span>
        <span style="color: #009900; font-style: italic;">// returns: nothing.</span>
    <span style="color: #66cc66;">&#125;</span>,
&nbsp;
    destroy: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>preserveDom<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// summary: an overridden destroy method. call the parent method, but we'll add stuff here too.</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">inherited</span><span style="color: #66cc66;">&#40;</span>arguments<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>,
&nbsp;
    _kill: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>widget<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// summary: destroy properly this widget instance</span>
        <span style="color: #009900; font-style: italic;">// returns: nothing.</span>
    <span style="color: #66cc66;">&#125;</span>,
&nbsp;
    _addItem: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #009900; font-style: italic;">/* Widget... */</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// summary: Add any number of widgets to this instance</span>
        <span style="color: #009900; font-style: italic;">// returns: nothing.</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>So we've stubbed out the API with two new public functions, a single overridden destroy method, and two private helper functions. <code>_addItem</code> will do most of the work, so we'll show it first:</p>
<pre class="javascript">&nbsp;
    _addItem: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #009900; font-style: italic;">/* Widget... */</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._addedItems = <span style="color: #000066; font-weight: bold;">this</span>._addedItems || <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
        <span style="color: #000066; font-weight: bold;">this</span>._addedItems.<span style="color: #006600;">push</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._addedItems, arguments<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>,
&nbsp;</pre>
<p>The code will create or reuse an instance member (array) called <code>_addedItems</code>. This contains the list of widgets we've created programatically. It accepts any number of arguments, so you can add multiple items in the same line of code.</p>
<pre class="javascript">&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>._addItem<span style="color: #66cc66;">&#40;</span>
        <span style="color: #003366; font-weight: bold;">new</span> dijit.<span style="color: #006600;">form</span>.<span style="color: #006600;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">placeAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">fooNode</span><span style="color: #66cc66;">&#41;</span>,
        <span style="color: #003366; font-weight: bold;">new</span> dijit.<span style="color: #006600;">form</span>.<span style="color: #006600;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">placeAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">barNode</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>When the widget calling addItem is destroyed, the two anonymously created Button instances will go with it. Well, not quite yet, but we only have to add a line to our destroy method to handle that action. We'll also add the code for the <code>_kill</code> function, which we'll reuse in <code>orphan</code> in a moment. <code>_kill</code> is a simple function which will destroy a single widget instance properly. In <code>destroy</code> we call <code>_kill</code> by passing it to the <code>forEach</code> call:</p>
<pre class="javascript">&nbsp;
    destroy: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        dojo.<span style="color: #006600;">forEach</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._addedItems, <span style="color: #000066; font-weight: bold;">this</span>._kill<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">inherited</span><span style="color: #66cc66;">&#40;</span>arguments<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>,
    _kill: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>widget<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>widget.<span style="color: #006600;">destroyRecursive</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
            widget.<span style="color: #006600;">destroyRecursive</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>widget.<span style="color: #006600;">destroy</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
            widget.<span style="color: #006600;">destroy</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>So we now have a way to add tracked widgets to ourself and to destroy them cleanly when we go away. Admittedly, I don't like the API for <code>_addItem</code> ... Not only is it _private, I still have a lot of boilerplate stuff repeated, now being wrapped with <code>_addItem()</code>. This is where the <code>adopt</code> API. It takes the repetition down one notch, and wraps the creation and adding into a single function call:</p>
<pre class="javascript">&nbsp;
    adopt: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>cls, props, srcNode<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #003366; font-weight: bold;">new</span> cls<span style="color: #66cc66;">&#40;</span>pros, srcNode<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">this</span>._addItem<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">return</span> x;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>All Dijit _Widget derivatives accept two constructor arguments: an object hash of properties, and an optional source-node reference (from which to create the instance). We simply relay those two arguments into a single 'new' call, add the item to ourselves, and returning the widget instance otherwise unmodified. To use it is simple:</p>
<pre class="javascript">&nbsp;
    postCreate: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">adopt</span><span style="color: #66cc66;">&#40;</span>my.<span style="color: #006600;">ListItem</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">placeAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">li</span>, <span style="color: #3366CC;">&quot;before&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The ListItem in this example doesn't need any constructor args, so they are omitted. The return from <code>adopt</code> is the widget instance, so you can chain placeAt calls on it, or save the return value to reuse in other scoped functions. Here's a small example of everything working "together":</p>
<pre class="javascript">&nbsp;
    postCreate: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dialog</span> = <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">adopt</span><span style="color: #66cc66;">&#40;</span>my.<span style="color: #006600;">Dialog</span>, <span style="color: #66cc66;">&#123;</span> onClick: dojo.<span style="color: #006600;">hitch</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, <span style="color: #3366CC;">&quot;_click&quot;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
        setTimeout<span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">hitch</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dialog</span>, <span style="color: #3366CC;">&quot;open&quot;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #CC0000;">2000</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">destroyButton</span>, <span style="color: #3366CC;">&quot;onclick&quot;</span>, <span style="color: #3366CC;">&quot;_close&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>,
    _click: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        e &amp;&amp; e.<span style="color: #006600;">preventDefault</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>,
    _close: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dialog</span> &amp;&amp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dialog</span>.<span style="color: #000066;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The last function is wildy useful, but not as mind-blowing as having tracked programmatic instances. In the above example we tracked an instance of <code>my.Dialog</code> locally as <code>this.dialog</code>. We're checking the presence of <code>this.dialog</code> before calling <code>close()</code> on it. Sometimes we only need a widget for a short while ... where we want to destroy it upon closing, or destroy a single reused variable holding a widget. That is what <code>orphan</code> is for:</p>
<pre class="javascript">&nbsp;
    orphan: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>widget, destroy<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._addedItems = <span style="color: #000066; font-weight: bold;">this</span>._addedItems || <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
        <span style="color: #003366; font-weight: bold;">var</span> i = dojo.<span style="color: #006600;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._addedItems, widget<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>i &gt;= <span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">this</span>._addedItems.<span style="color: #006600;">splice</span><span style="color: #66cc66;">&#40;</span>i, <span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#41;</span>;
        destroy &amp;&amp; <span style="color: #000066; font-weight: bold;">this</span>._kill<span style="color: #66cc66;">&#40;</span>widget<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The code loaded the local list of added children, searches for a passed reference, and removes it from the tracking list. Optionally, you can destroy the widget while being orphaned by passing any truthy value in the second argument. Now, the code pattern which looks like:</p>
<pre class="javascript">&nbsp;
    _click: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dialog</span> &amp;&amp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dialog</span>.<span style="color: #006600;">destroy</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dialog</span> = <span style="color: #003366; font-weight: bold;">new</span> my.<span style="color: #006600;">Dialog</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>can be reduced to:</p>
<pre class="javascript">&nbsp;
    _click: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dialog</span> &amp;&amp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">orphan</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dialog</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dialog</span> = <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">adopt</span><span style="color: #66cc66;">&#40;</span>my.<span style="color: #006600;">Dialog</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The latter example is <em>slightly</em> more verbose, but it has the benefit of not leaking any widgets.  Memory costs more than keystrokes methinks.</p>
<p>The full, documented code is as follows. Feel free to use it in your own app: the migration is painless:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">declare</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my.Widget&quot;</span>, <span style="color: #66cc66;">&#91;</span> dijit._Widget, dijit._Templated <span style="color: #66cc66;">&#93;</span>,
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #009900; font-style: italic;">// summary:</span>
    <span style="color: #009900; font-style: italic;">//    The Foundation widget for our things. Includes _Widget and _Templated with some custom addin methods.</span>
&nbsp;
    adopt: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #009900; font-style: italic;">/* Function */</span>cls, <span style="color: #009900; font-style: italic;">/* Object? */</span>props, <span style="color: #009900; font-style: italic;">/* DomNode */</span>node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// summary: Instantiate some new item from a passed Class, with props with an optional srcNode (node)</span>
        <span style="color: #009900; font-style: italic;">//  reference. Also tracks this widget as if it were a child to be destroyed when this parent widget</span>
        <span style="color: #009900; font-style: italic;">//  is removed.</span>
        <span style="color: #009900; font-style: italic;">//</span>
        <span style="color: #009900; font-style: italic;">// cls: Function</span>
        <span style="color: #009900; font-style: italic;">//      The class to instantiate. Cannot be a string. Use dojo.getObject to get a full class object if you</span>
        <span style="color: #009900; font-style: italic;">//      must.</span>
        <span style="color: #009900; font-style: italic;">//</span>
        <span style="color: #009900; font-style: italic;">// props: Object?</span>
        <span style="color: #009900; font-style: italic;">//      An optional object mixed into the constructor of said cls.</span>
        <span style="color: #009900; font-style: italic;">//</span>
        <span style="color: #009900; font-style: italic;">// node: DomNode?</span>
        <span style="color: #009900; font-style: italic;">//      An optional srcNodeRef to use with dijit._Widget. This thinger will be instantiated using</span>
        <span style="color: #009900; font-style: italic;">//      this passed node as the target if passed. Otherwise a new node is created and you must placeAt() your</span>
        <span style="color: #009900; font-style: italic;">//      instance somewhere in the dom for it to be useful.</span>
        <span style="color: #009900; font-style: italic;">//</span>
        <span style="color: #009900; font-style: italic;">// example:</span>
        <span style="color: #009900; font-style: italic;">//  |    this.adopt(my.ui.Button, { onClick: function(){} }).placeAt(this.domNode);</span>
        <span style="color: #009900; font-style: italic;">//</span>
        <span style="color: #009900; font-style: italic;">// example:</span>
        <span style="color: #009900; font-style: italic;">//  |   var x = this.adopt(my.ui.Button).placeAt(this.domNode);</span>
        <span style="color: #009900; font-style: italic;">//  |   x.connect(this.domNode, &quot;onclick&quot;, &quot;fooBar&quot;);</span>
        <span style="color: #009900; font-style: italic;">//</span>
        <span style="color: #009900; font-style: italic;">//  example:</span>
        <span style="color: #009900; font-style: italic;">//  If you *must* new up a thinger and only want to adopt it once, use _addItem instead:</span>
        <span style="color: #009900; font-style: italic;">//  |   var t;</span>
        <span style="color: #009900; font-style: italic;">//  |   if(4 &gt; 5){ t = new my.ui.Button(); }else{ t = new my.ui.OtherButton() }</span>
        <span style="color: #009900; font-style: italic;">//  |   this._addItem(t);</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #003366; font-weight: bold;">new</span> cls<span style="color: #66cc66;">&#40;</span>props, node<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">this</span>._addItem<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">return</span> x; <span style="color: #009900; font-style: italic;">// my.Widget</span>
    <span style="color: #66cc66;">&#125;</span>,
&nbsp;
    _addItem: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #009900; font-style: italic;">/* dijit._Widget... */</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// summary: Add any number of programatically created children to this instance for later cleanup.</span>
        <span style="color: #009900; font-style: italic;">// private, use `adopt` directly.</span>
        <span style="color: #000066; font-weight: bold;">this</span>._addedItems = <span style="color: #000066; font-weight: bold;">this</span>._addedItems || <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
        <span style="color: #000066; font-weight: bold;">this</span>._addedItems.<span style="color: #006600;">push</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._addedItems, arguments<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>,
&nbsp;
    orphan: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #009900; font-style: italic;">/* dijit._Widget */</span>widget, <span style="color: #009900; font-style: italic;">/* Boolean? */</span>destroy<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// summary: remove a single item from this instance when we destroy it. It is the parent widget's job</span>
        <span style="color: #009900; font-style: italic;">// to properly destroy an orphaned child.</span>
        <span style="color: #009900; font-style: italic;">//</span>
        <span style="color: #009900; font-style: italic;">// widget:</span>
        <span style="color: #009900; font-style: italic;">//      A widget reference to remove from this parent.</span>
        <span style="color: #009900; font-style: italic;">//</span>
        <span style="color: #009900; font-style: italic;">// destroy:</span>
        <span style="color: #009900; font-style: italic;">//      An optional boolean used to force immediate destruction of the child. Pass any truthy value here</span>
        <span style="color: #009900; font-style: italic;">//      and the child will be orphaned and killed.</span>
        <span style="color: #009900; font-style: italic;">//</span>
        <span style="color: #009900; font-style: italic;">// example:</span>
        <span style="color: #009900; font-style: italic;">//  Clear out all the children in an array, but do not destroy them.</span>
        <span style="color: #009900; font-style: italic;">//  |   dojo.forEach(this._thumbs, this.orphan, this);</span>
        <span style="color: #009900; font-style: italic;">//</span>
        <span style="color: #009900; font-style: italic;">// example:</span>
        <span style="color: #009900; font-style: italic;">//  Create and destroy a button cleanly:</span>
        <span style="color: #009900; font-style: italic;">//  |   var x = this.adopt(my.ui.Button, {});</span>
        <span style="color: #009900; font-style: italic;">//  |   this.orphan(x, true);</span>
        <span style="color: #009900; font-style: italic;">//</span>
        <span style="color: #000066; font-weight: bold;">this</span>._addedItems = <span style="color: #000066; font-weight: bold;">this</span>._addedItems || <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
        <span style="color: #003366; font-weight: bold;">var</span> i = dojo.<span style="color: #006600;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._addedItems, widget<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>i &gt;= <span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">this</span>._addedItems.<span style="color: #006600;">splice</span><span style="color: #66cc66;">&#40;</span>i, <span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#41;</span>;
        destroy &amp;&amp; <span style="color: #000066; font-weight: bold;">this</span>._kill<span style="color: #66cc66;">&#40;</span>widget<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>,
&nbsp;
    _kill: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>w<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// summary: Private helper function to properly destroy a widget instance.</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>w &amp;&amp; w.<span style="color: #006600;">destroyRecursive</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
            w.<span style="color: #006600;">destroyRecursive</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>w &amp;&amp; w.<span style="color: #006600;">destroy</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
            w.<span style="color: #006600;">destroy</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>,
&nbsp;
    destroy: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// summary: override the default destroy function to account for programatically added children.</span>
        dojo.<span style="color: #006600;">forEach</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._addedItems, <span style="color: #000066; font-weight: bold;">this</span>._kill<span style="color: #66cc66;">&#41;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">inherited</span><span style="color: #66cc66;">&#40;</span>arguments<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Does anyone have any alternate solutions they've found to combat this problem? </p>
]]></content:encoded>
			<wfw:commentRss>http://higginsforpresident.net/2010/01/widgets-within-widgets/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>140 Characters of awesome</title>
		<link>http://higginsforpresident.net/2009/12/140-characters-of-awesome/</link>
		<comments>http://higginsforpresident.net/2009/12/140-characters-of-awesome/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 11:26:05 +0000</pubDate>
		<dc:creator>dante</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://higginsforpresident.net/?p=38</guid>
		<description><![CDATA[A few days ago I tweeted a line of code and decided it was wonderful enough to warrant further explanation. While the code itself may have fit into 140 characters, the examples and use cases go on and on. Because of a few architectural decisions made by Dojo long ago, these 140 characters are powerful [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I <a href="http://twitter.com/phiggins/status/6235308045">tweeted</a> a line of code and decided it was wonderful enough to warrant further explanation. While the code itself may have fit into 140 characters, the examples and use cases go on and on. Because of a few architectural decisions made by Dojo long ago, these 140 characters are powerful and flexible as well as mindlessly simple to use.</p>
<p>One thing a <a href="http://api.dojotoolkit.org/jsdoc/1.3.2/dojo.NodeList">dojo.NodeList</a> does not have by default in Base (dojo.js) is a way to fetch and inject content into a node (or nodes). This isn't necessarily an oversight, there are <a href="http://dojocampus.org/content/2008/03/14/functional-ajax-with-dojo/">several ways</a> this can be accomplished. In fact it is so simple an operation with so many possible edge cases it may even not be <em>worth</em> implementing. Here I present to you my own take on this super simple concept, built by using standard Base (dojo.js) functionality. </p>
<p>The function will be called <code>grab()</code>. It will grab a url, and inject it into some nodes. Here is the first iteration of this idea:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">extend</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">NodeList</span>, <span style="color: #66cc66;">&#123;</span>
	grab: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>url<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		dojo.<span style="color: #006600;">xhr</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span>, <span style="color: #66cc66;">&#123;</span> url:url <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
			.<span style="color: #006600;">addCallback</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>response<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">addContent</span><span style="color: #66cc66;">&#40;</span>response, <span style="color: #3366CC;">&quot;only&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>We're simply mixing a new function in <code>dojo.NodeList.prototype</code> via <code>dojo.extend</code>, giving all instances of a NodeList this method. The plugin calls "return this;" to allow further chaining. Ajax operations by default are asynchronous</p>
<p>We work with NodeLists via <code>dojo.query</code>, so to use this code now we simply query the dom and load some content via Ajax:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">addOnLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#header&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">grab</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;header.html&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>That is all well and good and works brilliantly until you need to a) load non-html content b) issue an xhrPost instead of get or c) configure the xhr call in any way. No worries, with just a few more bytes we can add all of that functionality piggybacking on the behavior of <code>dojo.Deferred</code> and objects.</p>
<p>First, we'll add support for mixing in extra parameters into the object passed to the XHR call. Currently, we're only passing <code>{ url: url }</code>, giving the Ajax call an endpoint to fetch. <code>dojo.xhr</code> accepts a <strong>lot</strong> of options in this magic object, so allowing a developer/user to mix in their own custom information here is as simple as calling <code>dojo.mixin</code> with an optional parameter. mixin() is safe like this, in that if extraArgs is undefined or null nothing happens. The url:url aspect is still retained, and any args passed as extraArgs are overwritten into the call. </p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">extend</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">NodeList</span>, <span style="color: #66cc66;">&#123;</span>
	grab: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>url, extraArgs<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		dojo.<span style="color: #006600;">xhr</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span>, dojo.<span style="color: #006600;">mixin</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> url:url <span style="color: #66cc66;">&#125;</span>, extraArgs<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			.<span style="color: #006600;">addCallback</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>response<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">addContent</span><span style="color: #66cc66;">&#40;</span>response, <span style="color: #3366CC;">&quot;only&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>This now allows us to mix extra items into the Ajax call like timeout, sync, error handlers and so on. Back to the nature of Ajax calls being asynchronous: if we issue a grab() call on some node, the next items in the chain will not apply to the new content until an undetermined point in the future. We can overcome this limitation by passing <code>{ sync:true }</code> to the Ajax call, making the operation synchronous and thus making the following chains not execute until <em>after</em> the transfer is complete.</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">addOnLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#content&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">grab</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;/article/12345&quot;</span>, <span style="color: #66cc66;">&#123;</span> sync: <span style="color: #003366; font-weight: bold;">true</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
		.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;p.title&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">onclick</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">innerHTML</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>If we had omitted the <code>{ sync:true }</code> here, the find() call would be querying the dom of the node with id="content", though the content of <code>/article/12345</code> url would not yet be in the container. We don't <em>have</em> to use synchronous Ajax here, it is just a possibility. For instance, we could have connected the click handler to the #content NodeList and delegated from the bubble, or used plugd's <a href="http://code.google.com/p/plugd/source/browse/trunk/connectlive.js">connectLive</a> plugin to simple connect to "p.title" and delegated that way. </p>
<p>We can use the <code>extraArgs</code> in so many ways it's ridiculous. What if you wanted to know if the loading error'd out for some reason:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#content&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">grab</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;/foo.html&quot;</span>, <span style="color: #66cc66;">&#123;</span>
	error: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		console.<span style="color: #006600;">warn</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;error fetching foo.html&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Or, more importantly, what if you wanted to load content other than plain HTML. Perhaps you have a JSON object to load in. This is entirely possible because of the way <code>dojo.Deferred</code> works. When we call addCallback() on the Deferred chain in the plugin we're really registering the <em>last</em> chain. Anything we mix in to the Ajax call directly is executed before the final callback which calls this.addContent(). The secret is: the return value from a previous callback is passed to the next callback in the chain. So if we register a <code>load:</code> callback in the extraArgs we are permitted a pre-processing step for whatever content comes in. We simply need to supply an alternate <code>handleAs</code> to tell Ajax to process as JSON, and the <code>load</code> callback to process the JSON and return HTML so <code>addContent</code> works.</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;ul &gt; li&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">grab</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;person.json&quot;</span>, <span style="color: #66cc66;">&#123;</span>
	handleAs:<span style="color: #3366CC;">&quot;json&quot;</span>,
	load: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// again, broken string for a broken wordpress highlight.</span>
		<span style="color: #000066; font-weight: bold;">return</span> dojo.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;&lt;&quot;</span> + <span style="color: #3366CC;">&quot;p&gt;{username} - {age} / {sex}&lt;/&quot;</span> + <span style="color: #3366CC;">&quot;p&gt;&quot;</span>, data<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>The [new in Dojo 1.4] function <code>dojo.replace</code> does simple template substitution. In the above example, data.username is substituted into {username}, and the full HTML is returned to the second callback for this Ajax call, then injected into the appropriate node(s). In version prior to Dojo 1.4 you can use dojo.string.replace to accomplish mostly the same goals (though the templating is done differently, ${username} would need to be substituted). Above will inject a paragraph into each of the matched list items with some data. If our data came back as an array, we could simply make up the list items as well.</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;ul#mainlist&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">grab</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;tweets.json&quot;</span>, <span style="color: #66cc66;">&#123;</span>
	handleAs:<span style="color: #3366CC;">&quot;json&quot;</span>,
	load:<span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> response = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
		dojo.<span style="color: #006600;">forEach</span><span style="color: #66cc66;">&#40;</span>data, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #009900; font-style: italic;">// string intentionally broken up because wordpress highlight breaks it.</span>
			response.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;&lt;l&quot;</span> + <span style="color: #3366CC;">&quot;i&gt;&lt;&quot;</span> + <span style="color: #3366CC;">&quot;p&gt;{username} - {age}&lt;/&quot;</span> + <span style="color: #3366CC;">&quot;p&gt;&lt;/l&quot;</span> + <span style="color: #3366CC;">&quot;i&gt;&quot;</span>, data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">return</span> response.<span style="color: #006600;">join</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Here we're building up a string of list-items to be injected into the unordered list with id="mainlist". When the full response is joined and returned, that value is processed by <code>addContent</code> and injected appropriately.</p>
<p>The last little piece of the puzzle is to assume you don't want to issue and xhrGet call. There are several HTTP verbs to use, each with their own purpose: PUT, DELETE, or POST come to mind. With a simple adjustment to the <code>grab</code> function we can allow the developer to optionally overwrite this as well:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">extend</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">NodeList</span>, <span style="color: #66cc66;">&#123;</span>
	grab: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>url, extraArgs, method<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		dojo.<span style="color: #006600;">xhr</span><span style="color: #66cc66;">&#40;</span>method || <span style="color: #3366CC;">&quot;GET&quot;</span>, dojo.<span style="color: #006600;">mixin</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> url:url <span style="color: #66cc66;">&#125;</span>, extraArgs<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			.<span style="color: #006600;">addCallback</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>response<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">addContent</span><span style="color: #66cc66;">&#40;</span>response, <span style="color: #3366CC;">&quot;only&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Now, if we pass a method parameter the default value of "GET" is ignored. Otherwise the defaults take over. Again utilizing the extraArgs "mixin", we can make an Ajax POST request and send along custom data:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#login&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">grab</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;/user/login&quot;</span>, <span style="color: #66cc66;">&#123;</span>
	content:<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066;">name</span>: dojo.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;username&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span>,
		pass: dojo.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;password&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>, <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>One implementation detail I don't like about <code>grab</code> is the forced emptying of the target node. In the <code>addCallback</code> function where we call <code>addContent</code> with a parameter of "only" we are forcefully emptying out the nodes before injecting the new content. Unfortunately, this is the <em>only</em> behavior we've permitted here. It would be trivial to omit to "only" (and less bytes, too) and require the developer to call <code>empty()</code> manually before injecting new content if that behavior was desired. By default then we'd be adding the content "last", which is the default <code>addContent</code> position. Let's rework the <code>grab</code> function to handle this:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">extend</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">NodeList</span>, <span style="color: #66cc66;">&#123;</span>
	grab: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>url, extraArgs, method<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		dojo.<span style="color: #006600;">xhr</span><span style="color: #66cc66;">&#40;</span>method || <span style="color: #3366CC;">&quot;GET&quot;</span>, dojo.<span style="color: #006600;">mixin</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> url:url <span style="color: #66cc66;">&#125;</span>, extraArgs<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addCallback</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, <span style="color: #3366CC;">&quot;addContent&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>It got even smaller! the "(scope, method)" pattern used in <code>addCallback</code> above is found throughout Dojo, and is super handy. That function "pair" (this.addContent) will be passed whatever value is returned from the Ajax call still, in the first position. The difference is we are not permitted to pass a position (without lamba's in JavaScript). Now, to inject some content into a node <strong>and</strong> empty it we must do that manually:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">empty</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">grab</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;bar.html&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Without the <code>empty()</code>, "bar.html" content would be <em>appended</em> to the node rather than replace the content. I have yet to decide which behavior I like better. Opinions? I'll need to decide that before adding this functionality to Base Dojo proper. It currently lives in <a href="http://code.google.com/p/plugd/">plugd</a>, along with a slew of other new functionality for Dojo 1.4.</p>
<p>One last thing to do to <code>grab</code> function: there is no point in sending off the request if no nodes were found in the query. A simple check on the .length of the NodeList will prevent any unnecessary XHR calls from taking place:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">extend</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">NodeList</span>, <span style="color: #66cc66;">&#123;</span>
	grab: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>url, extraArgs, method<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">length</span> &amp;&amp; dojo.<span style="color: #006600;">xhr</span><span style="color: #66cc66;">&#40;</span>method || <span style="color: #3366CC;">&quot;GET&quot;</span>, dojo.<span style="color: #006600;">mixin</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> url:url <span style="color: #66cc66;">&#125;</span>, extraArgs<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addCallback</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, <span style="color: #3366CC;">&quot;addContent&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>And there you have it, 140 characters of awesome. (it may be a bit more with the longhand variables, pre-shrinkage. Also the inclusion of the extend call and so on add a few bytes, but in plugd's base.js these were already there for the other plugins provided, so is moot)</p>
]]></content:encoded>
			<wfw:commentRss>http://higginsforpresident.net/2009/12/140-characters-of-awesome/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Degrading Script Tags, an aside.</title>
		<link>http://higginsforpresident.net/2009/05/degrading-script-tags/</link>
		<comments>http://higginsforpresident.net/2009/05/degrading-script-tags/#comments</comments>
		<pubDate>Fri, 29 May 2009 00:41:51 +0000</pubDate>
		<dc:creator>dante hicks</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://higginsforpresident.net/?p=37</guid>
		<description><![CDATA[A few months ago John Resig published a 'degrading script tag' post, which outlined a very simple pattern for better utilizing a script element. I liked it, it has always bothered me a simple construct like:
&#160;
&#60;script src=&#34;foo.js&#34;&#62;
	foo(); // woohoo!
&#60;/script&#62;
&#160;
doesn't "just work". It makes perfect sense: load foo.js and execute some code when the script is [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago John Resig published a '<a href="http://ejohn.org/blog/degrading-script-tags/">degrading script tag</a>' post, which outlined a very simple pattern for better utilizing a script element. I liked it, it has <em>always</em> bothered me a simple construct like:</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;foo.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	foo(); // woohoo!
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span>
&nbsp;</pre>
<p>doesn't "just work". It makes perfect sense: load foo.js and execute some code when the script is ready. If the script fails or doesn't exist, don't execute the code. It is the perfect pattern for progressive javascript. </p>
<p>This makes perfect sense for Dojo as well: load dojo.js and execute the content therein if everything is OK. It would be great to support a syntax like:</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;dojo/dojo.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	dojo.require(&quot;dijit.layout.TabContainer&quot;);
	dojo.addOnLoad(function(){
		// make some tabs, and stuff.
	});
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span>
&nbsp;</pre>
<p>Not satisfied with John's <code>scripts[scripts.length - 1]</code> solution, and [at the time] lacking a better idea of my own, I dismissed the idea as novel. A few days ago it dawned on me: Dojo can do this already. I quickly threw together a hack. All we need is the script element. </p>
<p>Base Dojo is broken into <a href="http://svn.dojotoolkit.org/src/dojo/trunk/_base/">several files</a>  (though works transparently between the "development" version and the "built" base, aka: dojo.js), and uses a sophisticated build system to make a custom deployment as simple as:</p>
<pre>
./build.sh profile=myStuff
</pre>
<p>My quick solution involves suggesting some incredibly simple changes into some deep bits of the Dojo loader. Some background: Dojo 1.x supports an optional "djConfig" parameter placed on the script element loading the library. eg:</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;dojo.js&quot;</span> djConfig=<span style="color: #ff0000;">&quot;parseOnLoad:true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span>
&nbsp;</pre>
<p>As a result of the lookup involved in having maintained this behavior for years, Dojo "knows" the element loading dojo.js (or dojo.xd.js), but only in a browser environment. We can add a single line to the <a href="http://svn.dojotoolkit.org/src/dojo/trunk/_base/_loader/hostenv_browser.js">hostenv_browser.js</a>, saving a reference to our own script node:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// after we are in the regexp match looking for ourself:</span>
d._scriptElem = scripts<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
&nbsp;</pre>
<p>Then, we create a new base module to process the content if it exists:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">provide</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo._base._loader.self&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// tell package/build system about this module/plugin</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// execute this content [if] found in the script tag for dojo.js</span>
	e &amp;&amp; e.<span style="color: #006600;">innerHTML</span>.<span style="color: #006600;">length</span> &amp;&amp; dojo<span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">&quot;eval&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>e.<span style="color: #006600;">innerHTML</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000066; font-weight: bold;">delete</span> dojo._scriptElem; <span style="color: #009900; font-style: italic;">// cleanup?</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>dojo._scriptElem<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Hooking the two parts together is another one-liner: add a conditional require to the "base layer" file. As the last line to ensure it is the last piece of code executed by dojo.js:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">requireIf</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">isBrowser</span>, <span style="color: #3366CC;">&quot;dojo._base._loader.self&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>If we are building for non-browser situations the code is ignored. We can go a step further and save a few bytes by excluding the whole module as an optional build parameter:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">//&gt;&gt;excludeStart(&quot;noself&quot;, kwArgs.noSelf);</span>
dojo.<span style="color: #006600;">requireIf</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">isBrowser</span>, <span style="color: #3366CC;">&quot;dojo._base._loader.self&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #009900; font-style: italic;">//&gt;&gt;excludeEnd(&quot;noself&quot;);</span>
&nbsp;</pre>
<p>This way, if we build dojo.js with <code>noSelf=true</code>, the code never even becomes a consideration for base, and the innerHTML of our degrading script tag is simply ignored.</p>
<p>So my solution/hack here fixes a couple of the issues raised in John's blog comments, such as XHR loading of the script, delayed or deferred loading of the script, and most importantly the assumption the script would be the last in the page. I've thrown together some simple tests double checking <a href="/js/self/lazy.html">delayed loading</a> and <a href="/js/self/simple.html">basic success</a>, and made up a <a href="/js/self/build.sh">small shell script</a> which will fetch Dojo, apply <a href="http://dante.dojotoolkit.org/static/patches/1.3.0-self.patch">the patch</a>, and run a build (giving you a dojo.js with these behaviors, though the 'development' dojo.js works identically). I don't agree with the assumption John made about the code being executed within a <code>$(document).ready</code> function, as there is a lot of Dojo functionality that doesn't require the DOM to operate (eg: dojo.require) so I omitted that bit. </p>
<p>I'm torn on if this is worthwhile for Dojo or not, as we try to avoid any ubermagic that goes against what trained developers are expecting ... but I agree with John: the syntax is quite sexy and ridiculously clear as to what is and should be happening. </p>
<p>My initial pass at this was slightly more sloppy, but misguided. I had a nasty bit of code in self.js like:</p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span>	how = <span style="color: #3366CC;">&quot;textContent&quot;</span> <span style="color: #000066; font-weight: bold;">in</span> e ? <span style="color: #3366CC;">&quot;textContent&quot;</span> :
		<span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #000066; font-weight: bold;">in</span> e ? <span style="color: #3366CC;">&quot;text&quot;</span> :
		<span style="color: #3366CC;">&quot;innerHTML&quot;</span> <span style="color: #000066; font-weight: bold;">in</span> e ? <span style="color: #3366CC;">&quot;innerHTML&quot;</span> :
		<span style="color: #003366; font-weight: bold;">null</span>;
&nbsp;
e &amp;&amp; e<span style="color: #66cc66;">&#91;</span>how<span style="color: #66cc66;">&#93;</span> &amp;&amp; dojo<span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">&quot;eval&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#91;</span>how<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>But that was just me being neurotic from my experience of injecting raw code into a newly created script element. Turn's out .innerHTML works across browsers for <em>reading</em> the current content. </p>
<p>Interestingly, Opera stores the src="" value in .text and the <em>actual</em> innerHTML in .textContent, whereas IE only has .text. I even made a log from my findings (before I noticed John was only using .innerHTML, at which point I stopped populating my mini <a href="http://quirksmode.org">ppk</a>-inspired-compatibility <a href="/js/script-table.html">table</a>)</p>
<p>... so if the use of eval() bothers you, I've worked up a small bit of code to create a script and safely inject some JavaScript within (where I copy/pasted my sloppy self.js lookup code from):</p>
<pre class="javascript">&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
	d.<span style="color: #006600;">noteval</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #009900; font-style: italic;">/* String */</span>code<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// summary: Execute some javascript.</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>!code<span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>;
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span>	e = d.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;script&quot;</span>, <span style="color: #66cc66;">&#123;</span> type:<span style="color: #3366CC;">&quot;text/javascript&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>,
			<span style="color: #009900; font-style: italic;">// jump through the cross-browser hoops:</span>
			how = <span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #000066; font-weight: bold;">in</span> e ? <span style="color: #3366CC;">&quot;text&quot;</span> :
				<span style="color: #3366CC;">&quot;textContent&quot;</span> <span style="color: #000066; font-weight: bold;">in</span> e ? <span style="color: #3366CC;">&quot;textContent&quot;</span> :
				<span style="color: #3366CC;">&quot;innerHTML&quot;</span> <span style="color: #000066; font-weight: bold;">in</span> e ? <span style="color: #3366CC;">&quot;innerHTML&quot;</span> :
				<span style="color: #3366CC;">&quot;appendChild&quot;</span>
		;
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>how == <span style="color: #3366CC;">&quot;appendChild&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			e<span style="color: #66cc66;">&#91;</span>how<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>d.<span style="color: #006600;">doc</span>.<span style="color: #006600;">createTextNode</span><span style="color: #66cc66;">&#40;</span>code<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #66cc66;">&#123;</span>
			e<span style="color: #66cc66;">&#91;</span>how<span style="color: #66cc66;">&#93;</span> = code;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">return</span> d.<span style="color: #006600;">doc</span>.<span style="color: #006600;">getElementsByTagName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;head&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>dojo<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>I've used this in a slightly more verbose version to support <a href="/js/static/scriptThing.js">a very sloppy</a> injection of JavaScript in XHR content via the dojoType attribute and utilizing the dojo.parser. It could probably be reduced. I should make a table.</p>
]]></content:encoded>
			<wfw:commentRss>http://higginsforpresident.net/2009/05/degrading-script-tags/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>More on Dojo &#8220;plugins&#8221;</title>
		<link>http://higginsforpresident.net/2009/05/more-on-dojo-plugins/</link>
		<comments>http://higginsforpresident.net/2009/05/more-on-dojo-plugins/#comments</comments>
		<pubDate>Thu, 21 May 2009 12:50:51 +0000</pubDate>
		<dc:creator>dante hicks</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://higginsforpresident.net/?p=36</guid>
		<description><![CDATA[Dojo doesn't call any of the hundreds of optional/add-on modules available on either CDN a "plugin", though they all ultimately behave as such. Dojo has had a custom package system for several years now: a way to modularize script files and create dependencies between them, loading them as you see fit and optionally compressing or [...]]]></description>
			<content:encoded><![CDATA[<p>Dojo doesn't call any of the hundreds of optional/add-on modules available on either CDN a "plugin", though they all ultimately behave as such. Dojo has had a custom package system for several years now: a way to modularize script files and create dependencies between them, loading them as you see fit and optionally compressing or concatenating them as part of the automated build system.</p>
<p>So for the sake of argument, we'll call the various Dojo modules available "plugins". On top of Base Dojo (dojo.js), there are a number of great plugins available "out of the box". This is how you load them:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// cookie setter-getter plugin</span>
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo.cookie&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// advanced string substitution stuff</span>
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo.string&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// the back-button management plugin</span>
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo.back&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// advanced Animation support. chain/combine, some prefab fx</span>
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo.fx&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// advanced number formatting functions</span>
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo.number&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// currency conversion, formatting, etc</span>
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo.currency&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// date formatting and utility functions</span>
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo.date&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// fast selector-based live api plugin</span>
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo.behavior&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Most are simply optional utility functions to help with the tasks you will face as a developer (especially if you are considering internationalization and accessibility issues). Load as needed, omit as desired. (There are a series of UI controls in Dijit, and hundreds more plugins in DojoX, again, all available on the CDNs, all just a require() away)</p>
<p>You could of course point &lt;script&gt; tags at each of the files, but should you change the location of anything you'll need to adjust each path to accomodate. By using the Dojo package system we're able to create a list of requirements to load, and only need to ensure the relative location to dojo.js is correct. dojo.js typically lives in a folder named 'dojo'. All dojo.* modules are floating around there as well. eg:</p>
<pre>
src/
	dojo/
		- dojo.js
		- behavior.js
		- number.js
</pre>
<p>As a design decision, Dojo uses clear namespaces to compartmentalize all plugins. This prevents any possible overlap and destruction of items fighting for the same name. If you've ever tried to include both Prototype and jQuery in a page you know what I'm talking about, as both "fight" for the right to use the global "$" function (jQuery for it's entire API and Prototype as a byId selector). jQuery bows out of that contest by providing a 'noConflict' function, allowing Prototype to keep $, and tells users to do something similar to:</p>
<pre class="javascript">&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// I can use $ again!</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>jQuery<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>to regain control of the $ in a localized scope. This is similar to something I do in plugd, and other places throughout the Dojo plugin landscape (though I do it because typing 'd' is three characters less than typing 'dojo', not because of any interoperability issues):</p>
<pre class="javascript">&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>d, $<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// I can use $, too!</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>dojo, dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>So the issue isn't that you <em>can't</em> "plugin" to Dojo, it is simply that as a best practice Dojo suggests you <em>don't</em> ... Doing so will likely cause headaches in the future. Though the API's in Base Dojo (dojo.js) are <em>always</em> backward compatible between releases, the addition of new APIs may conflict with whatever name you've chosen. I'm actually experiencing this myself a bit in plugd, where I chose to place all the plugins into the dojo namespace directly. For example, plugd's `create` function came first, and creates DOM markup from either a single element or complex markup. Dojo's `create` (as of 1.3) does not do this, `dojo.place` does. </p>
<p>In hindsight, I should have copied the dojo namespace into plugd and made my extensions all namespaced plugd. That way, using it in the dojo namespace would simply be:</p>
<pre class="javascript">&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>dojo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// that's an interesting circle, though JavaScript likely allows it.</span>
	<span style="color: #009900; font-style: italic;">// not tested this approach any, so don't quote me.</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>plugd<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>But the idea of stubbing functions onto dojo.query for the chaining and whatnot is entirely appealing, and entirely valid. dojo.query returns an instance of a dojo.NodeList, leaving the work of querying to a single module focused exclusively on querying, and treating the extensions externally. The typical way would be:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">extend</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">NodeList</span>, <span style="color: #66cc66;">&#123;</span>
	setColor: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>color<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// summary: set the color to &quot;color&quot;, and return this NodeList</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">style</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;color&quot;</span>, color<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Though, as per my <a href="http://www.slideshare.net/phiggins/dojopatterns">dojo.Patterns talk</a> and brief <a href="http://higginsforpresident.net/2009/03/a-dojo-plugin-pattern/">blog</a> on the topic, I would suggest making a single function to act upon a node (byId or node reference), and make that act upon the nodes in the list. This, simply put, is faster. There is no overhead of querying the DOM, instantiating a nodelist and so on, unless you know you need to. This pattern looks just like:</p>
<pre class="javascript">&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
	d.<span style="color: #006600;">setColor</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>n, color<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// summary: A function to set a color for some node</span>
		d.<span style="color: #006600;">style</span><span style="color: #66cc66;">&#40;</span>n, <span style="color: #3366CC;">&quot;color&quot;</span>, color<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// make this function an iterable when used with dojo.query</span>
	d.<span style="color: #006600;">NodeList</span>.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">setColor</span> = d.<span style="color: #006600;">NodeList</span>._adaptAsForEach<span style="color: #66cc66;">&#40;</span>d.<span style="color: #006600;">setColor</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>dojo<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Now we can use the setColor method either way: fast and on a single node, or marginally slower and across all nodes. Having the choice is nice:</p>
<pre class="javascript">&nbsp;
	<span style="color: #009900; font-style: italic;">// way one: force a byId lookup from a string:</span>
	dojo.<span style="color: #006600;">setColor</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;someId&quot;</span>, <span style="color: #3366CC;">&quot;blue&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// way two: from a saved node reference eg:</span>
	<span style="color: #003366; font-weight: bold;">var</span> node = dojo.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;someNode&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">parentNode</span>;
	dojo.<span style="color: #006600;">setColor</span><span style="color: #66cc66;">&#40;</span>node, <span style="color: #3366CC;">&quot;green&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// way three: in bulk, allowing for further chaining:</span>
	dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;.someNodes&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setColor</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>The most 'complicated' bit about this pattern is having to set a function directly on NodeList.prototype ... To shelter you from dealing with prototype's directly, I've devised a fun <a href="http://code.google.com/p/plugd">plugd</a> plugin, aptly named "dojo.plugin". It does two things:</p>
<ol>
<li>Creates <code>dojo.fn</code> as an alias to <code>dojo.NodeList.prototype</code>, so you can simply write a plugin like: <code>dojo.fn.myPlugin = function(props){ ... }</code>
	</li>
<li>Defines a <code>dojo.plugin</code> function to create both a single function and NodeList variant with a single syntax</li>
</ol>
<p>To use, simply require in the module (assuming you have plugd checked out. Alternately you can simply download the single file and use a &lt;script&gt; tag and place it anywhere):</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// load the resource/plugin</span>
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;plugd.plugin&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #009900; font-style: italic;">// define dojo.setColor and dojo.fn.setColor:</span>
dojo.<span style="color: #006600;">plugin</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;setColor&quot;</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>n, color<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	dojo.<span style="color: #006600;">style</span><span style="color: #66cc66;">&#40;</span>n, <span style="color: #3366CC;">&quot;color&quot;</span>, color<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>With those few lines, again, all the available patterns are made available to us. There are a whole collection of "dojo plugins" in the plugd project, all of which follow this "stub into dojo namespace willy nilly" pattern in the hopes some will be upgraded to Dojo Core or Base. </p>
<p>The source for the "<a href="http://code.google.com/p/plugd/source/browse/trunk/plugin.js">plugd.plugin</a>" module is simple. Sans comments, it weighs about 8 lines:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">provide</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;plugd.plugin&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
	d.<span style="color: #006600;">fn</span> = d.<span style="color: #006600;">NodeList</span>.<span style="color: #006600;">prototype</span>;
	d.<span style="color: #006600;">plugin</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>pluginNamespace, fn, way<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #009900; font-style: italic;">//&gt;&gt;excludeStart(&quot;safetyFirst&quot;, kwArgs.noWarnings == &quot;on&quot;);</span>
		<span style="color: #009900; font-style: italic;">// leave safety in optionally, build with noWarnings=on to disable check</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">&#91;</span>pluginNamespace<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			console.<span style="color: #006600;">warn</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;cowardly won't clobber '&quot;</span> + pluginNamespace + <span style="color: #3366CC;">&quot;' method&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000066; font-weight: bold;">return</span> fn; <span style="color: #009900; font-style: italic;">// Function</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #009900; font-style: italic;">//&gt;&gt;excludeEnd(&quot;safetyFirst&quot;);</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> f = d<span style="color: #66cc66;">&#91;</span>pluginNamespace<span style="color: #66cc66;">&#93;</span> = fn;
		d.<span style="color: #006600;">fn</span><span style="color: #66cc66;">&#91;</span>pluginNamespace<span style="color: #66cc66;">&#93;</span> = d.<span style="color: #006600;">NodeList</span><span style="color: #66cc66;">&#91;</span>way || <span style="color: #3366CC;">&quot;_adaptAsForEach&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>f<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #000066; font-weight: bold;">return</span> f; <span style="color: #009900; font-style: italic;">// Function</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>dojo<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>I added a "safetyFirst" #ifdef-like block for the build system. This way, I can disable the checking to see if my plugin will conflict with an existing function as a build option, further reducing the code. There is very little smoke and mirrors in Dojo - teaching fundamental JavaScript principals and suggesting some working practices along the way. Checkout some of the sample plugins in <a href="http://code.google.com/p/plugd/source/browse/#svn/trunk">plugd's trunk</a>, or <a href="http://code.google.com/p/plugd/source/checkout">check it out directly</a> into a plugd/ folder next to your dojo/ folder and <code>dojo.require</code> some stuff. Most of the code is commented to the hilt, and few (if any) of the functions exceed 8 lines of code. Excellent way to view and understand some fairly cool JavaScript magic.</p>
]]></content:encoded>
			<wfw:commentRss>http://higginsforpresident.net/2009/05/more-on-dojo-plugins/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Dojo Build string interning revelation</title>
		<link>http://higginsforpresident.net/2009/05/dojo-build-string-interning-revelation/</link>
		<comments>http://higginsforpresident.net/2009/05/dojo-build-string-interning-revelation/#comments</comments>
		<pubDate>Wed, 20 May 2009 17:48:19 +0000</pubDate>
		<dc:creator>dante hicks</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://higginsforpresident.net/?p=35</guid>
		<description><![CDATA[I made an interesting random discovery today which opened a door for a whole new breed of Templated Dojo widgets (aka: Dijits). The Dijit portion of the Dojo Toolkit really could be broken into two parts: A Collection of pre-fabricated UI components and a well thought out framework for creating your own UI components. 
At [...]]]></description>
			<content:encoded><![CDATA[<p>I made an interesting random discovery today which opened a door for a whole new breed of Templated Dojo widgets (aka: Dijits). The Dijit portion of the Dojo Toolkit really could be broken into two parts: A Collection of pre-fabricated <acronym title="User Interface">UI</acronym> components and a well thought out framework for creating your own UI components. </p>
<p>At the heart of Dijit are two modules used everywhere: dijit._Widget and dijit._Templated, but those pull in a number of _base modules used to power the framework. dijit.registry, dijit.byId, dijit.typeomatic, and all the various accessibility and utility functions used internally exposed. The overall size of the Dijit framework is about 10k gzipped, including dijit._Widget and dijit._Templated, so it hardly a drop in the bucket on the wire.</p>
<p>dijit._Templated does a cool thing and does a ton of caching, loading and managing of templates for widgets allowing a developer to define either a templateString or templatePath to use for the rendering:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">provide</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my.TemplatedWidget&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dijit._Widget&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dijit._Templated&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">declare</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my.TemplatedWidget&quot;</span>, <span style="color: #66cc66;">&#91;</span>dijit._Widget, dijit._Templated<span style="color: #66cc66;">&#93;</span>,<span style="color: #66cc66;">&#123;</span>
	templatePath: dojo.<span style="color: #006600;">moduleUrl</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my&quot;</span>, <span style="color: #3366CC;">&quot;templates/TemplatedWidget.html&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Basic example using templatePath, which the widget will fetch as necessary using XHR. The interesting part happens after running a Dojo build. As <a href="http://dojocampus.org/content/2008/03/24/interning-strings-everywhere-in-your-dojo-build/">Neil had previously investigated</a>, any matched templatePath variables will have their remote markup inlined into the javascript source as a templateString member. </p>
<p>I often run into a situation where I want to use the Dijit framework to create widgets that have children, but seldom need to make the children classes themselves. Delegating a number of identical items underneath the outer most widget instance is a great way to squeeze performance out of your app, especially when you don't need the children to compartmentalize any of their own behavior.</p>
<p>One solution is to handle this manually, and define some kind of childtemplate member, creating the children as you go:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">declare</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my.Thinger&quot;</span>, <span style="color: #66cc66;">&#91;</span>dijit._Widget, dijit._Templated<span style="color: #66cc66;">&#93;</span>, <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// the containerNode in Thinger.html is an unordered-list</span>
	templatePath: dojo.<span style="color: #006600;">moduleUrl</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my&quot;</span>, <span style="color: #3366CC;">&quot;templates/Thinger.html&quot;</span><span style="color: #66cc66;">&#41;</span>,
	childtemplate: <span style="color: #3366CC;">&quot;
&lt;li&gt;${foo}&lt;/li&gt;
&nbsp;
&quot;</span>,
&nbsp;
	addChild:<span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #009900; font-style: italic;">/* Object */</span>data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// render a child in this widget from the passed data.</span>
		dojo.<span style="color: #006600;">place</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">string</span>.<span style="color: #006600;">substitute</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">childtemplate</span>, data<span style="color: #66cc66;">&#41;</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">containerNode</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>But this immediately breaks the separation of concerns and leaves us with markup in our JavaScript, which is never a good idea. I wanted to specify an external template for the children items, but still get the advantage of the string-interning the build provides. On a whim, I decided to test how accurately the build was matching "templatePath" for the conversion, and to see if I would be able to pull something like this off:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">declare</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my.Thinger&quot;</span>, <span style="color: #66cc66;">&#91;</span>dijit._Widget, dijit._Templated<span style="color: #66cc66;">&#93;</span>, <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// the containerNode in Thinger.html is an unordered-list</span>
	templatePath: dojo.<span style="color: #006600;">moduleUrl</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my&quot;</span>, <span style="color: #3366CC;">&quot;templates/Thinger.html&quot;</span><span style="color: #66cc66;">&#41;</span>,
	childtemplatePath: dojo.<span style="color: #006600;">moduleUrl</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my&quot;</span>, <span style="color: #3366CC;">&quot;templates/Thinger-child.html&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>and have the resulting built version contain a "childtemplateString" equaling the contents of my now external Thinger-child.html template. </p>
<p>My whim proved accurate, though if this is a bug or a feature is yet to be determined. Taking advantage of this behavior, I know I can shim in some extra code creating a dijit._Templated variant which allows for the 'childtemplate' pattern I seek.</p>
<p>We're going to create a "subclass" of dijit._Templated, hooking into the buildRendering method and do some templatePath handling of our own. We need to support three things:</p>
<ol>
<li>A childtemplateString and childtemplatePath should work transparently together. String should take precedence over Path.
	</li>
<li>The developer should not have to know which is being used, so make the normalized string version available as "childtemplate"
	</li>
<li>The template member should be configurable, optional, and default to "off" (maintaining backwards compatibility)</li>
</ol>
<p>The task was incredibly easy. I chose using a "subTemplate" member as indication a child template exists, and to avoid ambiguity in naming selections. If no subTemplate is passed, nothing new happens. If a subTemplate string is passed, that string dictates where the template will be available. Eg: subTemplate:"foo" creates this.footemplate from either footemplateString or footemplatePath.</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">provide</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;beer._Templated&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dijit._Templated&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> cachedString = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
	dojo.<span style="color: #006600;">declare</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;beer._Templated&quot;</span>, dijit._Templated, <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #009900; font-style: italic;">// subTemplate: String</span>
		<span style="color: #009900; font-style: italic;">//		An identifier for this childtemplate relationship. eg: Set to 'item',</span>
		<span style="color: #009900; font-style: italic;">//		which allows for &quot;itemtemplateString&quot; or &quot;itemtemplatePath&quot; usage as</span>
		<span style="color: #009900; font-style: italic;">//		you would a normal `dijit._Templated`. After `buildRendering`, an</span>
		<span style="color: #009900; font-style: italic;">//		instance member is provided based on this `subTemplate` name as well.</span>
		<span style="color: #009900; font-style: italic;">//		eg: this.itemtemplate will always be available in `postCreate` for</span>
		<span style="color: #009900; font-style: italic;">//		use internally.</span>
		subTemplate:<span style="color: #003366; font-weight: bold;">null</span>,
&nbsp;
		buildRendering:<span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #009900; font-style: italic;">// hook into _Templated's buildRendering, cache templates on a</span>
			<span style="color: #009900; font-style: italic;">// per-class basis. </span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">subTemplate</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> identifier = <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">subTemplate</span> + <span style="color: #3366CC;">&quot;template&quot;</span>,
					s = <span style="color: #3366CC;">&quot;String&quot;</span>;
&nbsp;
				<span style="color: #009900; font-style: italic;">// there is a lot of repetition between cache and this[something],</span>
				<span style="color: #009900; font-style: italic;">// but it only happens once, so isn't so bad (vs more code?)</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>!cachedString<span style="color: #66cc66;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">declaredClass</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>!<span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#91;</span>identifier + s<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
						<span style="color: #009900; font-style: italic;">// assume we have an itemtemplatePath in the absence</span>
						<span style="color: #009900; font-style: italic;">// of an itemtemplateString</span>
						dojo.<span style="color: #006600;">xhrGet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
							url: <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#91;</span>identifier + <span style="color: #3366CC;">&quot;Path&quot;</span><span style="color: #66cc66;">&#93;</span>,
							sync: <span style="color: #003366; font-weight: bold;">true</span>, <span style="color: #009900; font-style: italic;">// we should be only doing this once</span>
							load: dojo.<span style="color: #006600;">hitch</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>template<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
								<span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#91;</span>identifier + s<span style="color: #66cc66;">&#93;</span> = dojo.<span style="color: #006600;">trim</span><span style="color: #66cc66;">&#40;</span>template<span style="color: #66cc66;">&#41;</span>;
							<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
						<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #66cc66;">&#125;</span>
					<span style="color: #009900; font-style: italic;">// cache the string for the template</span>
					cachedString<span style="color: #66cc66;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">declaredClass</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#91;</span>identifier + s<span style="color: #66cc66;">&#93;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#91;</span>identifier<span style="color: #66cc66;">&#93;</span> = cachedString<span style="color: #66cc66;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">declaredClass</span><span style="color: #66cc66;">&#93;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #009900; font-style: italic;">// always call native buildRendering</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">inherited</span><span style="color: #66cc66;">&#40;</span>arguments<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Now you can declare classes inheriting from beer._Templated, and have all the support of dijit._Templated in addition to a nice shorthand/custom templating solution. To use:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">provide</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my.Thing&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dijit._Widget&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;beer._Templated&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">declare</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my.Thing&quot;</span>, <span style="color: #66cc66;">&#91;</span>dijit._Widget, beer._Templated<span style="color: #66cc66;">&#93;</span>, <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// core:</span>
	templatePath: dojo.<span style="color: #006600;">moduleUrl</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my&quot;</span>,<span style="color: #3366CC;">&quot;templates/Thing.html&quot;</span><span style="color: #66cc66;">&#41;</span>,
	<span style="color: #009900; font-style: italic;">// extra:</span>
	childtemplatePath: dojo.<span style="color: #006600;">moduleUrl</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;my&quot;</span>, <span style="color: #3366CC;">&quot;templates/Sub-Thing.html&quot;</span><span style="color: #66cc66;">&#41;</span>,
	subTemplate:<span style="color: #3366CC;">&quot;child&quot;</span>,
&nbsp;
	<span style="color: #009900; font-style: italic;">// using it:</span>
	addChild: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> n = dojo.<span style="color: #006600;">place</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">string</span>.<span style="color: #006600;">substitute</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">childtemplate</span><span style="color: #66cc66;">&#41;</span>, data<span style="color: #66cc66;">&#41;</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">containerNode</span><span style="color: #66cc66;">&#41;</span>;
		dojo.<span style="color: #006600;">style</span><span style="color: #66cc66;">&#40;</span>n, <span style="color: #3366CC;">&quot;opacity&quot;</span>, <span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span>;
		dojo.<span style="color: #006600;">fadeIn</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> node: n <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>The only problem I have with this so far is the naming convention. "childtemplatePath" should be written as "childTemplatePath", as per the Dojo style guidelines. A quick (3 line) adjustment to the build utility, and that support can be <a href="http://bugs.dojotoolkit.org/ticket/9321">added as well</a>.</p>
<p>The 'beer' namespace comes from me discovering this tidbit while working on <a href="http://dante.dojotoolkit.org/tweeters/">twitterverse</a>, which [for whatever reason] is using 'beer' for the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://higginsforpresident.net/2009/05/dojo-build-string-interning-revelation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Dojo Plugin Pattern</title>
		<link>http://higginsforpresident.net/2009/03/a-dojo-plugin-pattern/</link>
		<comments>http://higginsforpresident.net/2009/03/a-dojo-plugin-pattern/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 23:23:20 +0000</pubDate>
		<dc:creator>dante hicks</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://higginsforpresident.net/?p=34</guid>
		<description><![CDATA[I created a new widget last night based on a couple small functions in base plugd: dojo.twit ... It is a very simple widget with a minimal 2k overhead and illustrates just how truly "plug-able" and flexible Dojo is.
For the horribly impatient: checkout the twitter feed on the sidebar (another degradable example of Dojo) or [...]]]></description>
			<content:encoded><![CDATA[<p>I created a new widget last night based on a couple small functions in base <a href="http://code.google.com/p/plugd/">plugd</a>: dojo.twit ... It is a very simple widget with a minimal 2k overhead and illustrates just how truly "plug-able" and flexible Dojo is.</p>
<p>For the horribly impatient: checkout the twitter feed on the sidebar (another degradable example of Dojo) or download the full built <a href="/js/twit-130.js">twit.js</a> (requires Dojo 1.3)</p>
<p>There are three existing patterns in Dojo that are loosely related:</p>
<ul>
<li>Acting upon a node - eg: dojo.style(node, {}), dojo.anim(node, {})</li>
<li>Acting upon a list of nodes - eg: dojo.query(".nodes").style({})</li>
<li>Converting a node into a "widget" - eg: new my.Widget({}, node)</li>
</ul>
<p>The first two go hand-in-hand. Anything you can do to one node you should be able to repeat across a list of nodes in the same way with only a marginal cost for the iteration. The functions to act upon a node follow a very simple API:</p>
<pre class="javascript">&nbsp;
some.<span style="color: #006600;">func</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #009900; font-style: italic;">/* String|DomNode */</span>node, <span style="color: #009900; font-style: italic;">/* Object? */</span>props<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// summary: do something to node.</span>
	<span style="color: #009900; font-style: italic;">// node: Can be a DOMNode reference, or string ID or a DOMNode to use</span>
	<span style="color: #009900; font-style: italic;">// props: Object-hash of properties and parameters for this function call</span>
	node = dojo.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #009900; font-style: italic;">// now do something to node.</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The functions that act upon a list of nodes are simply mixed into <code>dojo.NodeList</code>:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// new in Dojo 1.3.0:</span>
dojo.<span style="color: #006600;">NodeList</span>.<span style="color: #006600;">func</span> = dojo.<span style="color: #006600;">NodeList</span>._adaptAsForEach<span style="color: #66cc66;">&#40;</span>some.<span style="color: #006600;">func</span><span style="color: #66cc66;">&#41;</span>;	
&nbsp;
<span style="color: #009900; font-style: italic;">// old Dojo 1.0 - 1.2.x way:</span>
dojo.<span style="color: #006600;">extend</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">NodeList</span>, <span style="color: #66cc66;">&#123;</span>
	func: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>props<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// summary: run `some.func` for each of the nodes in this list</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">forEach</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			some.<span style="color: #006600;">func</span><span style="color: #66cc66;">&#40;</span>n, props<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>To use the <code>some.func</code> function either way:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// one node</span>
some.<span style="color: #006600;">func</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;someId&quot;</span>, <span style="color: #66cc66;">&#123;</span> arg1: <span style="color: #3366CC;">&quot;a&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #009900; font-style: italic;">// all with class=&quot;bar&quot;</span>
dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;.bar&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">func</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> arg1: <span style="color: #3366CC;">&quot;a&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Note the above use of a new Dojo 1.3 API <code>_adaptAsForEach</code> ... These are private API's put in place to allow little to no duplication between Dojo's own following of this pattern. The direct forEach method will always work as shown, as they are public APIs, though _adaptAsForEach may change. (I'm taking the gamble they will remain in place in some form for quite some time, and enjoy the savings they provide)</p>
<p>By following this pattern, Dojo is able to provide <em>incredibly</em> fast low-level APIs, then map them directly into the Selector engine (aka: dojo.query) for bulk operations.  </p>
<p>The last item ("Widgets") actually has two use cases: programatic and declarative. The programatic v. declarative argument has been going on for a long time, and many people still cry standards-foul when referring to Dojo -- this is entirely unnecessary. The declarative way is the optional method of the "plain old JavaScript" way of instantiating some class[like] object:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// programatic:</span>
<span style="color: #003366; font-weight: bold;">new</span> dijit.<span style="color: #006600;">TitlePane</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> title:<span style="color: #3366CC;">&quot;The Title&quot;</span> <span style="color: #66cc66;">&#125;</span>, <span style="color: #3366CC;">&quot;nodeOrId&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&lt;!-- declarative --&gt;
&lt;div dojoType=<span style="color: #3366CC;">&quot;dijit.TitlePane&quot;</span> id=<span style="color: #3366CC;">&quot;someId&quot;</span> title=<span style="color: #3366CC;">&quot;The Title&quot;</span>&gt;&lt;/div&gt;
&nbsp;</pre>
<p>The pattern here is simple. The dojoType attribute maps to some object/function (in the example: dijit.TitlePane). The <code>dojo.parser</code> is the only thing in Dojo that understands the dojoType attribute. The Parser's sole job is to:</p>
<ul>
<li>find nodes with a dojoType attribute. eg: dojoType="Thinger"</li>
<li>read the attributes on the found nodes, and convert them to an object.</li>
<li>call new Thinger(thatObject, foundNode) for each of the nodes</li>
</ul>
<p>The attributes which are read by the parser aren't entirely arbitrary. For performance reasons, the attributes must be defined in the class you plan to instantiate in order for the parser to find them:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">declare</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Thinger&quot;</span>, <span style="color: #003366; font-weight: bold;">null</span>, <span style="color: #66cc66;">&#123;</span>
	exampleAttrib:<span style="color: #3366CC;">&quot;default-value&quot;</span>
	constructor: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>args, node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// generic ctor function</span>
		dojo.<span style="color: #006600;">mixin</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, args<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">node</span> = dojo.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;</pre>
<p>The above "Thinger" example will only attempt to locate an "exampleAttrib" parameter on a passed node:</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> dojoType=<span style="color: #ff0000;">&quot;Thinger&quot;</span> exampleAttrib=<span style="color: #ff0000;">&quot;overridden&quot;</span> ignoredAttrib=<span style="color: #ff0000;">&quot;useless&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
&nbsp;</pre>
<p>So the widget pattern is nearly identical to the Dojo pattern (only reversed). Again together:</p>
<pre class="javascript">&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> args = <span style="color: #66cc66;">&#123;</span> prop:<span style="color: #3366CC;">&quot;b&quot;</span> <span style="color: #66cc66;">&#125;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// functions go node, args</span>
	my.<span style="color: #006600;">func</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;someNode&quot;</span>, args<span style="color: #66cc66;">&#41;</span>;
	dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#someNode&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">func</span><span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// widgets go args, node</span>
	<span style="color: #003366; font-weight: bold;">new</span> Thinger<span style="color: #66cc66;">&#40;</span>args, <span style="color: #3366CC;">&quot;someNode&quot;</span><span style="color: #66cc66;">&#41;</span>;	
&lt;p dojoType=<span style="color: #3366CC;">&quot;Thinger&quot;</span> prop=<span style="color: #3366CC;">&quot;b&quot;</span>&gt;
&nbsp;</pre>
<p>The reasoning behind the parameter order difference is simple: with widgets the node is optional and with functions the node is explicit. If you are programatically creating a Thinger, you can omit the node (at least in the case of Dijit) as one will be created for you. You must place the node in the DOM manually in this case.</p>
<pre class="javascript">&nbsp;
	<span style="color: #003366; font-weight: bold;">new</span> dijit.<span style="color: #006600;">TitlePane</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> title:<span style="color: #3366CC;">&quot;Title&quot;</span>, content:<span style="color: #3366CC;">&quot;
&nbsp;
The inner content
&nbsp;
&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">placeAt</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>The parser simply passes the node with the dojoType, and no new nodes are created. </p>
<p>All of this explanation is leading into my twit.js code, albeit an incredibly shortened version of the <a href="http://code.google.com/p/plugd/source/browse/trunk/twit.js">final file</a>. The above use cases exist in Dojo, and are more or less a constant across the toolkit. I wanted to make my little tweeter plugin work for all of them, without any duplication.</p>
<p>First things first: define the dojo.twit() and dojo.NodeList functions as skeletons. We're making this a full module, so we must include the dojo.provide() call. </p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">provide</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;plugd.twit&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojo.string&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// required for template substitution</span>
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;plugd.script&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// simple/tiny dojo.io.script replacement</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// the defaults we can override</span>
	<span style="color: #003366; font-weight: bold;">var</span> defaults = <span style="color: #66cc66;">&#123;</span>
		<span style="color: #3366CC;">&quot;user&quot;</span>:<span style="color: #3366CC;">&quot;phiggins&quot;</span>,
		<span style="color: #3366CC;">&quot;count&quot;</span>:<span style="color: #3366CC;">&quot;7&quot;</span>,
		<span style="color: #3366CC;">&quot;template&quot;</span>:<span style="color: #3366CC;">&quot;
&lt;p&quot;</span> + <span style="color: #3366CC;">&quot;&gt;${text}&lt;/&quot;</span> + <span style="color: #3366CC;">&quot;p&gt;&quot;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// the main function</span>
	d.<span style="color: #006600;">twit</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>node, args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		node = d.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #009900; font-style: italic;">// mix in the defaults with the args</span>
		<span style="color: #003366; font-weight: bold;">var</span> opts = d.<span style="color: #006600;">mixin</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>, defaults, args<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">/* the rest of the twit code */</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// 1.3+ only. use this.forEach method for 1.0 - 1.2</span>
	d.<span style="color: #006600;">NodeList</span>.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">twit</span> = d.<span style="color: #006600;">NodeList</span>._adaptAsForEach<span style="color: #66cc66;">&#40;</span>d.<span style="color: #006600;">twit</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>dojo<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>This, so far, allows us the two functional examples following the Dojo pattern. We need to fill in some code in the core dojo.twit() function to have this complete, but the keys are there:</p>
<ul>
<li>follows the same API pattern of accepting a string ID or domNode</li>
<li>accepts an object hash of properties to mix over the defaults</li>
<li>and works transparently with dojo.query</li>
</ul>
<p>Before we move into making this work as a typical widget would, I should point on special thing out. The "template" member in the defaults is using dojo.string.substitute for basic variable replacement. When we fetch the list of tweets for the opts.user, we will apply this template to each data item returned. No extra work on my part was needed:</p>
<pre class="javascript">&nbsp;
	d.<span style="color: #006600;">twit</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>node, args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		node = d.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #009900; font-style: italic;">// mix in the defaults with the args</span>
		<span style="color: #003366; font-weight: bold;">var</span> opts = d.<span style="color: #006600;">mixin</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>, defaults, args<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">/* the rest of the twit code: in pseudo-code */</span>
		fetch<span style="color: #66cc66;">&#40;</span>twitterUrl, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			dojo.<span style="color: #006600;">forEach</span><span style="color: #66cc66;">&#40;</span>data, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>tweet<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #009900; font-style: italic;">// append a new DOM to this node based on the passed template:</span>
				dojo.<span style="color: #006600;">place</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">string</span>.<span style="color: #006600;">substitute</span><span style="color: #66cc66;">&#40;</span>opts.<span style="color: #006600;">template</span>, tweet<span style="color: #66cc66;">&#41;</span>, node<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The substitute function simply mixes the tweet data into opts.template string, eg: ${text} becomes whatever tweet.text (the tweet content) is. By passing this result directly to dojo.place we are creating the new dom (this is also new in 1.3 -- not place(), but place() acting as a dom-creation API), and appending it to the node we targeted. </p>
<p>This is awesome until we go to implement the Class-based / dojo.parser version. The template:"" option needs to be an HTML snippet, and it would be incredibly ugly to support a pattern like:</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul</span></a> dojoType=<span style="color: #ff0000;">&quot;dojo.Twitter&quot;</span> template=<span style="color: #ff0000;">&quot;&amp;lt;li&amp;gt;${text}&amp;lt;/li&amp;gt;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span>
&nbsp;</pre>
<p>That makes <em>me</em> cringe, and I'm <em>ok</em> with the general use of a dojoType. I needed a better solution. I decided it would be simple enough to use the content of the widget node as the template. For example:</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul</span></a> dojoType=<span style="color: #ff0000;">&quot;dojo.Twitter&quot;</span> user=<span style="color: #ff0000;">&quot;phiggins&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>${text}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span>
&nbsp;</pre>
<p>To do this we'll need to read the innerHTML at the time of instantiation, then empty the node (to clear out the template):</p>
<pre class="javascript">&nbsp;
	d.<span style="color: #006600;">Twitter</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>args, node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		node = d.<span style="color: #006600;">byId</span><span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">&#41;</span>;
		d.<span style="color: #006600;">twit</span><span style="color: #66cc66;">&#40;</span>node, d.<span style="color: #006600;">mixin</span><span style="color: #66cc66;">&#40;</span>args, <span style="color: #66cc66;">&#123;</span> template: n.<span style="color: #006600;">innerHTML</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		d.<span style="color: #006600;">empty</span><span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #009900; font-style: italic;">// mix in the defaults into the .prototype, making parser recognize them:</span>
	d.<span style="color: #006600;">extend</span><span style="color: #66cc66;">&#40;</span>d.<span style="color: #006600;">Twitter</span>, defaults<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Now, officially, if dojo.twit does what is is supposed to (and it does), all the following API's are available with almost no repetition. Pick your style:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// functions:</span>
dojo.<span style="color: #006600;">twit</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;nodeId&quot;</span>, <span style="color: #66cc66;">&#123;</span> user:<span style="color: #3366CC;">&quot;phiggins&quot;</span>, template:<span style="color: #3366CC;">&quot;
&nbsp;
${text}
&nbsp;
&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;ul.foo&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">twit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> template:<span style="color: #3366CC;">&quot;
&lt;li&gt;${text}&lt;/li&gt;
&nbsp;
&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// plain class</span>
<span style="color: #003366; font-weight: bold;">new</span> dojo.<span style="color: #006600;">Twitter</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> user:<span style="color: #3366CC;">&quot;foo&quot;</span> <span style="color: #66cc66;">&#125;</span>, <span style="color: #3366CC;">&quot;nodeId&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #009900; font-style: italic;">// and the infamous dojoType</span>
&lt;ul dojoType=<span style="color: #3366CC;">&quot;dojo.Twitter&quot;</span> user=<span style="color: #3366CC;">&quot;phiggins&quot;</span>&gt;
&lt;li&gt;$<span style="color: #66cc66;">&#123;</span>text<span style="color: #66cc66;">&#125;</span>&lt;/li&gt;
&lt;/ul&gt;
&nbsp;</pre>
<p>You can see this in action in the sidebar. Entirely degradable. If no JS, no "Twitter" label is shown. Two lines in my global.js trigger the rest:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">addOnLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	dojo.<span style="color: #006600;">style</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;tweets&quot;</span>, <span style="color: #3366CC;">&quot;display&quot;</span>, <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// show the block, js is enabled</span>
	dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#twitter&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">twit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> template:<span style="color: #3366CC;">&quot;
&lt;li&gt;
&nbsp;
${text}
&lt;/li&gt;
&nbsp;
 &quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// make it a list</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>I love JavaScript, and Dojo 1.3 makes JavaScript <em>that</em> much more fun. Even if you don't agree with the library, and functionality deemed important and common enough for the base dojo.js -- there are years of professional-grade coding living in there, available to learn from and grow upon. </p>
]]></content:encoded>
			<wfw:commentRss>http://higginsforpresident.net/2009/03/a-dojo-plugin-pattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Changing APIs, or: why JavaScript rocks.</title>
		<link>http://higginsforpresident.net/2009/03/changing-apis-or-why-javascript-rocks/</link>
		<comments>http://higginsforpresident.net/2009/03/changing-apis-or-why-javascript-rocks/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 22:15:13 +0000</pubDate>
		<dc:creator>dante hicks</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://higginsforpresident.net/?p=33</guid>
		<description><![CDATA[Dojo has an exceptionally fast and performant "custom event" system that allows a developer to communicate ambiguously between components with little overhead, and [much like Dojo itself] in a "use as needed" manner: dojo.publish and dojo.subscribe, both provided in base Dojo (dojo.js)
The API's are basic: dojo.subscribe some function to a topic, and dojo.publish some data [...]]]></description>
			<content:encoded><![CDATA[<p>Dojo has an exceptionally fast and performant "custom event" system that allows a developer to communicate ambiguously between components with little overhead, and [much like Dojo itself] in a "use as needed" manner: dojo.publish and dojo.subscribe, both provided in base Dojo (dojo.js)</p>
<p>The API's are basic: dojo.subscribe some function to a topic, and dojo.publish some data on that topic. Bonus points for being able to unsubscribe at will:</p>
<pre class="javascript">&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> h = dojo.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;/dojo/rocks&quot;</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// &quot;A message&quot;;</span>
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	dojo.<span style="color: #006600;">publish</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;/dojo/rocks&quot;</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">&quot;A message!&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
	dojo.<span style="color: #006600;">unsubscribe</span><span style="color: #66cc66;">&#40;</span>h<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>The most immediate issue seen is having to pass an array to the publish() call. <a href="http://almaer.com">Dion</a> pointed this out to me shortly after I reviewed the <a href="http://almaer.com/blog/bespin-now-learning-some-art-in-the-dojo">newly-ported-to-Dojo</a> <a href="http://bespin.mozilla.com">Bespin</a> code base. The "array as the first parameter" bit is defined by the API docs and expected, though in many cases entirely unnecessary. The reason makes absolute sense: dojo.publish converts the passed array into ordered arguments:</p>
<pre class="javascript">&nbsp;
	dojo.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;/dojo/kicks&quot;</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a, b, c, d<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>a,b,c,d<span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// one, 2, three, 4</span>
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	dojo.<span style="color: #006600;">publish</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;/dojo/kicks&quot;</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">&quot;one&quot;</span>, <span style="color: #CC0000;">2</span>, <span style="color: #3366CC;">&quot;three&quot;</span>, <span style="color: #CC0000;">4</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Without getting into the details, the reason is the subscribed function is called with the array passed to .apply:</p>
<pre class="javascript">&nbsp;
	<span style="color: #009900; font-style: italic;">// define dojo.publish:</span>
	dojo.<span style="color: #006600;">publish</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>topic, args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> f = dojo._topics<span style="color: #66cc66;">&#91;</span>topic<span style="color: #66cc66;">&#93;</span>;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			f.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>f, args||<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>This triggers an empty function bound to the the topic name, and the magic of <code>dojo.connect</code> calls any attached functions, passing the same arguments to each. Because .apply() only accepts an array an empty array is passed in the event the passed args parameter is 'falsey'. </p>
<p>... which makes perfect sense until one of two things happen (which are arguably the most common):</p>
<pre class="javascript">&nbsp;
	<span style="color: #009900; font-style: italic;">// I want to pass one, single, parameter:</span>
	dojo.<span style="color: #006600;">publish</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;topic:foo&quot;</span>, <span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #009900; font-style: italic;">// I want to pass a list of parameters in order:</span>
	dojo.<span style="color: #006600;">publish</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;topic:foo&quot;</span>, <span style="color: #3366CC;">&quot;a&quot;</span>, <span style="color: #3366CC;">&quot;b&quot;</span>, <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>The second example makes less sense until you understand that dojo.connect executes all attached functions with the same parameters as the called function:</p>
<pre class="javascript">&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> obj = <span style="color: #66cc66;">&#123;</span>
		bar: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a,b,c<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #009900; font-style: italic;">// ... anything.</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// this won't work out of the box:</span>
	dojo.<span style="color: #006600;">connect</span><span style="color: #66cc66;">&#40;</span>obj, <span style="color: #3366CC;">&quot;bar&quot;</span>, dojo.<span style="color: #006600;">partial</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">publish</span>, <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	dojo.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;foo&quot;</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a,b,c,d<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>a,b,c,d<span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// one, two, three, undefined</span>
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	obj.<span style="color: #006600;">bar</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;one&quot;</span>, <span style="color: #3366CC;">&quot;two&quot;</span>, <span style="color: #3366CC;">&quot;three&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>When obj.bar() is called, the dojo.partial call executes "dojo.publish('foo', ...)", where '...' is equal to any arguments passed to the original obj.bar() call. In the above case, "one", "two", "three" is passed to the bound connected function. The connected function is a curried version of dojo.publish, pushing to the topic "foo". </p>
<p>I wanted to allow for either publish() syntax as mentioned, and not break backwards compatibility. I attempted to do this with:</p>
<pre class="javascript">&nbsp;
	<span style="color: #009900; font-style: italic;">// _this_ is why JavaScript is cool:</span>
	<span style="color: #003366; font-weight: bold;">var</span> old = dojo.<span style="color: #006600;">publish</span>;
	dojo.<span style="color: #006600;">publish</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>topic, args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> old<span style="color: #66cc66;">&#40;</span>topic, dojo.<span style="color: #006600;">isArray</span><span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span> ? args : <span style="color: #66cc66;">&#91;</span>args<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>But this has the unfortunate (non-back-compat) side effects of a) potentially passing a literal undefined to the subscribed functions 2) is an additional call to some [possibly] expensive functions, and c) generally breaks down for anyone expecting publish() to work as they've been explained previously, and 4) doesn't cover the case of an ambiguous length of arguments. </p>
<p>A quick solution to a) would be:</p>
<pre class="javascript">&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> old<span style="color: #66cc66;">&#40;</span>topic, dojo.<span style="color: #006600;">isArray</span><span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span> ? args : <span style="color: #66cc66;">&#91;</span> args || <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>... but you must still check the passed argument before dot-accessing any of it's members. </p>
<p>My solution was a new API, "pub", which simply wraps "dojo.publish" with some fun JavaScript foo:</p>
<pre class="javascript">&nbsp;
	dojo.<span style="color: #006600;">pub</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// summary: wrap `dojo.publish` to allow any number of arguments</span>
		<span style="color: #003366; font-weight: bold;">var</span> a = dojo._toArray<span style="color: #66cc66;">&#40;</span>arguments<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">return</span> dojo.<span style="color: #006600;">publish</span><span style="color: #66cc66;">&#40;</span>a.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, a<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Thought I won't take credit for it, I may have just just invented the most compelling use of an inline .shift() call, ever. Briefly: dojo._toArray, while private, will convert any enumerable object to a real array. Our function "pub" takes any number of arguments, converts them to an array (a), then returns the value from calling dojo.publish() ... what we pass to publish() is the fun:</p>
<p>a.shift() will return the first element of the array (a). This return value is passed to the first argument of the dojo.publish call. The shift() call also modifies the array (a) to be one element shorter, so our next reference to (a) will the reduced list. We're passing this reduced array to the second.</p>
<p>Following this pattern in your own functions is easy, even without Dojo. dojo._toArray does more, but in these examples all we've done is convert 'arguments' to an array. We can likely do this faster (and without Dojo):</p>
<pre class="javascript">&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> aps = Array.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">slice</span>;
	<span style="color: #009900; font-style: italic;">// simple version of `dojo._toArray`</span>
	<span style="color: #003366; font-weight: bold;">var</span> toArray<span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> aps.<span style="color: #006600;">call</span><span style="color: #66cc66;">&#40;</span>obj, <span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>`dojo.pub` is implemented in <a href="http://code.google.com/p/plugd/">plugd</a>, which will have a new version to coincide with Dojo 1.3.0 to be released shortly. Hopefully the dojo.pub API will take over the existing dojo.publish API in Dojo 2.0, where we get to fix these kinds of things.</p>
<p><strong>update Mar 7 2009:</strong> I mistakenly transposed the function as .slice(). It should have been .shift() in this blog. Thanks Wolfram for catching that! PlugD was using .shift() and the unit tests pass. </p>
]]></content:encoded>
			<wfw:commentRss>http://higginsforpresident.net/2009/03/changing-apis-or-why-javascript-rocks/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[more] JavaScript tips</title>
		<link>http://higginsforpresident.net/2009/01/more-javascript-tips/</link>
		<comments>http://higginsforpresident.net/2009/01/more-javascript-tips/#comments</comments>
		<pubDate>Sun, 11 Jan 2009 23:37:31 +0000</pubDate>
		<dc:creator>dante hicks</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://higginsforpresident.net/?p=30</guid>
		<description><![CDATA[A few weeks ago, Thomas Fuchs (author of script.aculo.us) posted a few "JavaScript tips" that sparked my interest ... 
The first, a post on Credit Card validation, and the importance of client-side validation because of potential costs and overhead associated with invalid CC requests to a provider.  It immediately reminded me of dojox.validate, Dojo's [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago, <a href="http://mir.aculo.us">Thomas Fuchs</a> (author of <a href="http://script.aculo.us">script.aculo.us</a>) posted a few "JavaScript tips" that sparked my interest ... </p>
<p>The first, a post on <a href="http://mir.aculo.us/2008/12/22/credit-card-validation">Credit Card validation</a>, and the importance of client-side validation because of potential costs and overhead associated with invalid CC requests to a provider.  It immediately reminded me of <em>dojox.validate</em>, Dojo's own in-house validation extension. I was certain there were existing functions to do all the things mentioned in the blog, and it turns out the <em>dojox.validate.creditCard</em> module I ported from Dojo 0.4 is still valid and kicking. It provides methods for:</p>
<ul>
<li>Determinging CC type by #</li>
<li>Determinging CC number by type</li>
<li>Simple validation of the CCV #</li>
<li>Determining validity by length and LUHN of CC #</li>
</ul>
<p>... and a few other methods. I recently updated it to expose the map of CC types to the regular expressions that match them so users could extend the validation to check their own custom Gift Card / Gift Certificate numbers. For instance, creating a custom type 'my' that starts with 7, made of only numbers, and is 15 characters long:</p>
<pre class="javascript">&nbsp;
dojo.<span style="color: #006600;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;dojox.validate.creditCard&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">mixin</span><span style="color: #66cc66;">&#40;</span>dojox.<span style="color: #006600;">validate</span>._cardInfo, <span style="color: #66cc66;">&#123;</span>
	<span style="color: #3366CC;">&quot;my&quot;</span>:<span style="color: #3366CC;">&quot;7[0-9]{14}&quot;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>The <em>dojox.validate</em> package also has out of the box email, url, and number validation, as well as locale-specific validation for edge cases like validating a Canadian Social Health number. This new extension point for CC's will be available in Dojo 1.3, as it was a private variable previously. I also cleaned up the inline <a href="http://svn.dojotoolkit.org/src/dojox/trunk/validate/">documentation</a> and <a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/util/doh/runner.html?testModule=dojox.validate.tests.module">unit tests</a> for the project, though the modules have been very stable for a long time now it seems. </p>
<p>I setup <a href="http://dante.dojotoolkit.org/static/valid_cc.html">an incredibly crude test</a> on my Dojo sandbox using the Google CDN and Dojo 1.2.3.  It simply prevents a form from submitting if an invalid credit card is detected, styling the input red. There is no endpoint, so no CC#-stealing is taking place ... I promise. </p>
<p>The other JavaScript Tips post was providing <a href="http://mir.aculo.us/2008/12/20/little-javascript-hints-episode-2-stay-dry">a Prototype-based DRY hint</a>.  It was a great tip. The example was a "page turner", using the same function for two actions, and currying <em>nextPage</em> and <em>prevPage</em> functions around that. The technique is sound. It made me think of <em>dojo.hitch</em>'s lesser-used cousin <em>dojo.partial</em>. To summarize:</p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> turnPage = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>direction<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
      <span style="color: #009900; font-style: italic;">// do something if direction is -1 or 1</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">var</span> nextPage = dojo.<span style="color: #006600;">partial</span><span style="color: #66cc66;">&#40;</span>turnPage, <span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> prevPage = dojo.<span style="color: #006600;">partial</span><span style="color: #66cc66;">&#40;</span>turnPage, <span style="color: #CC0000;">-1</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// goto next page.</span>
nextPage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// calls turnPage(1);</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// setup the nav:</span>
dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;a.nextPageLink&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">onclick</span><span style="color: #66cc66;">&#40;</span>nextPage<span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;a.prevPageLink&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">onclick</span><span style="color: #66cc66;">&#40;</span>prevPage<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>This is all well and good.  With <em>dojo.partial</em> I was able to simulate Thomas's example without issue -- but it got me thinking. I noticed in the blog example the 'curry' method was being called directly on the function. eg:</p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> turnPage<span style="color: #66cc66;">&#40;</span>dir<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #009900; font-style: italic;">/* doit */</span> <span style="color: #66cc66;">&#125;</span>,
    nextPage = turnPage.<span style="color: #006600;">curry</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#41;</span>,
    prevPage = turnPage.<span style="color: #006600;">curry</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">-1</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Which means it exists on the native prototype ... Though it is only an opinion, one typically should avoid extending native objects (in this case Function) with methods ... I would go so far as to say it is a bad practice overall. That fact, however, didn't squelch my curiosity as to how this was implemented, so I set out to to write a <em>curry</em> implementation in plain JavaScript that worked like <em>dojo.partial</em> except transparently on Function.prototype.</p>
<p>Initially, I tried using <em>dojo.partial</em> direcly, but couldn't quite get the arg-shifting and scopes right ... I turned to my friend and colleague <a href="http://lazutkin.com/blog/">Eugene Lazutkin</a> to help me write a function that behaved similarly to <em>dojo.partial</em> in that the 'hitched'/'curried' function would be called with the passed args first, as well as any arguments passed to the returned function. eg:</p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> foo = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a,b,c<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #009900; font-style: italic;">/* code */</span> <span style="color: #66cc66;">&#125;</span>;
<span style="color: #003366; font-weight: bold;">var</span> bar = dojo.<span style="color: #006600;">partial</span><span style="color: #66cc66;">&#40;</span>foo, <span style="color: #3366CC;">&quot;one&quot;</span><span style="color: #66cc66;">&#41;</span>;
bar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// foo(&quot;one&quot;)</span>
bar<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;baz&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// foo(&quot;one&quot;,&quot;baz&quot;);</span>
&nbsp;</pre>
<p>So we came up with a little function to do just this. Just a few lines of code, actually:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// a generic simple currying function. (thanks uhop!)</span>
<span style="color: #003366; font-weight: bold;">var</span> curryFunc = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> me = <span style="color: #000066; font-weight: bold;">this</span>, a = Array.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">concat</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>, arguments<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> me.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, a.<span style="color: #006600;">concat</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>a, arguments<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
&nbsp;</pre>
<p>So we have this function which refers to 'this', generates an Array (via .concat magic) from the passed 'arguments', and returns a function that will call (.apply) the original function (me) with those arguments mixed in with any others.  We can see this function working without extending any native objects by just referencing it on an instance:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// setup the test:</span>
<span style="color: #003366; font-weight: bold;">var</span> bar = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a, b, c<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bar'</span>, a, b, c<span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span>
bar.<span style="color: #006600;">partial</span> = curryFunc;
&nbsp;
<span style="color: #009900; font-style: italic;">// curry bar</span>
<span style="color: #003366; font-weight: bold;">var</span> foo = bar.<span style="color: #006600;">partial</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">1</span>, <span style="color: #CC0000;">2</span><span style="color: #66cc66;">&#41;</span>;
foo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// echos 'bar', 1, 2, undefined</span>
foo<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// echos 'bar', 1, 2, 3</span>
&nbsp;</pre>
<p>But adding the 'partial' function to every function we define seems like a lot of extra work. While I'm entirely happy calling <code>dojo.partial(someFunction, "curried","args")</code>, this experiment was to implement this function for all Functions. Easy enough:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// don't do this, please.</span>
<span style="color: #003366; font-weight: bold;">Function</span>.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">curry</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> me = <span style="color: #000066; font-weight: bold;">this</span>, a = Array.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">concat</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>, arguments<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> me.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, a.<span style="color: #006600;">concat</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>a, arguments<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
&nbsp;</pre>
<p>Which allows you (without using Dojo or Prototype for that matter) to write code like:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// create the function and the curried function:</span>
<span style="color: #003366; font-weight: bold;">var</span> foo = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a,b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>a,b<span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">var</span> bar = foo.<span style="color: #006600;">curry</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;first&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// test:</span>
bar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// &quot;first&quot;, undefined</span>
bar<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;baz&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// &quot;first&quot;, &quot;baz&quot;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// create a curried function from a curried function:</span>
<span style="color: #003366; font-weight: bold;">var</span> baz = bar.<span style="color: #006600;">curry</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;second&quot;</span><span style="color: #66cc66;">&#41;</span>;
baz<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// &quot;first&quot;, &quot;second&quot;</span>
&nbsp;</pre>
<p>Though, upon seeing my <a href="http://dante.dojotoolkit.org/static/curry.html">test page</a>, <a href="http://alex.dojotoolkit.org">Alex</a> immediately scolded me for touching <code>Function.prototype</code> at all, and suggested I set  ENUMMERABLE off so it doesn't show up in <code>for( in )</code> loops. I didn't get that far. I like using <em>dojo.partial</em>, and don't recommend doing this at all. </p>
<p>The article made me think about how Dojo recently re-factored <em>dojo.trim</em> to defer to a native String.trim() function if available ... It seems a great idea, but I was interested to see if anyone had provided String.prototype with this function themselves, like:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// don't do this, please. Give all strings a .trim() method.</span>
String.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">trim</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/^\s\s*/</span>, <span style="color: #3366CC;">''</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/\s\s*$/</span>, <span style="color: #3366CC;">''</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// safer, recommended. Make a function to trim a string.</span>
my.<span style="color: #006600;">trim</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>str<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> str.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/^\s\s*/</span>, <span style="color: #3366CC;">''</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/\s\s*$/</span>, <span style="color: #3366CC;">''</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Google says <a href="http://www.google.com/search?q=String.prototype.trim">quite a few</a> in fact, or at least a lot of people talk about it. So Dojo will defer to their implementation in edge cases (if it exists prior to dojo.js being on a page.) The API for trim() is simple and fairly universal so it shouldn't be too much an issue, but illustrates the complications extending native objects introduces. I'm not sure my Function.prototype.curry() method is 100% API compatible with <a href="http://www.prototypejs.org/api/function/curry">prototypejs's implementation</a>, so in creating it I am also creating potential interoperability issues. Fairly certain I am not. Short of looking at the code, .curry is to dojo.partial as .bind is to dojo.hitch, so the implementations should behave well together, but I'd like to avoid it none the less. </p>
]]></content:encoded>
			<wfw:commentRss>http://higginsforpresident.net/2009/01/more-javascript-tips/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Back In Business</title>
		<link>http://higginsforpresident.net/2008/12/back-in-business/</link>
		<comments>http://higginsforpresident.net/2008/12/back-in-business/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 14:53:26 +0000</pubDate>
		<dc:creator>dante hicks</dc:creator>
				<category><![CDATA[dojo]]></category>
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://higginsforpresident.net/?p=28</guid>
		<description><![CDATA[What an incredibly busy week! I'm finally getting settled in from a painful week of sunshine, technology and drinking in beautiful southern Florida, still suffering slightly from the rapid drop in ambient temperature I experienced exiting the plane in Knoxville [several hours late, no less].  But, I'm here, and back in business -- it's [...]]]></description>
			<content:encoded><![CDATA[<p>What an incredibly busy week! I'm finally getting settled in from a <em>painful</em> week of sunshine, technology and drinking in beautiful southern Florida, still suffering slightly from the rapid drop in ambient temperature I experienced exiting the plane in Knoxville [several hours late, no less].  But, I'm here, and back in business -- it's good to be home.</p>
<p>A big thanks to <a href="http://www.springsource.com/">SpringSource</a> for <em>forcing</em> me talk about <a href="http://dojotoolkit.org">Dojo</a> at the annual <a href="http://americas.springone.com/conference/hollywood/2008/12/index.html">SpringOne</a> America conference.  I've been there all week, and the experience was entirely positive: got to meet a whole <a href="http://twitpic.com/pvbv">host</a> of SpringPeople, talk to countless java developers, attend several quality talks, and ran into a number of Dojo contributors both known and anonymous (always nice to have a face to a name). Bonus points for the late-afternoon-beach-parties, endless supply of coffee, and incredible locale selection. I delivered [what I felt to be] a well-received talk about the philosophies and structure behind Dojo from base to util to <a href="http://dojocampus.org">dojoc/</a>, covered a lot of great material and just had an all-around great experience. Here's to hoping they like my paper for the upcoming <a href="http://europe.springone.com/europe-2009">SpringOne Europe</a> tour (which ironically is in the same hotel I stayed in during my last visit to Amsterdam, this time three years ago) . <em>Salud</em>.</p>
<p>My slides from the presentation are <a href="http://www.slideshare.net/phiggins/zero-to-dojo-presentation">available on SlideShare</a>.</p>
<p>So a week of hard work on the beaches of Florida also yielded several other awesome events: A night of good Cuban food, drinks and debauchery with another of my <em>oldest friends</em> (which isn't so nice anymore, as we're 'rounding to 30 now', and <em>old</em> is becoming an increasingly sensitive word) was only the beginning:</p>
<p><strong>Start to Finish</strong>: A motion to adopt <a href="http://ejohn.org">John Resig's</a> newest creation <a href="http://github.com/jeresig/sizzle/tree/master">Sizzle</a> as a top-level project to the <a href="http://dojofoundation.org">Dojo Foundation</a>  was recently proposed to the mailing list, and passed unanimously 21-<em>nil</em>.  The Dojo <em>Toolkit</em> was considering adopting the 'idea of a unified querySelectorAll engine', and suggested the idea to John to ensure "<a href="http://dojofoundation.org/about/hundredpoint/">all the things the Dojo <em>Foundation</em> is trying to achieve</a>" were in place -- enabling the Dojo <em>Toolkit</em> to use this new code (as well as anyone else wishing to adopt it). There seem to be a lot of <a href="http://ajaxian.com/archives/mootools-and-sizzle">mixed</a> <a href="http://www.clientcide.com/best-practices/dojos-dylan-schiemann-and-jquerys-john-resig-on-my-sizzle-post/">emotions</a> about the overall implications of this -- and I'd like to <em>briefly</em> throw in my take:</p>
<p>There is only one <em>est</em>. Be it tallest, smallest, smartest, or fastest. Until which time we can defer to a <em>fastest</em> native implementation for CSS selectors, we should collaborate on a unified codebase, out in the open, and available to all. This is all <em>just</em> JavaScript, we can offload library-specific redundancy by utilizing js's dynamic nature and create some portable underlying thing (which coincidentally <a href="http://bugs.dojotoolkit.org/ticket/7794">Dojo already did</a> to a degree) -- we are all fighting the same battle, <a href="http://en.wikipedia.org/wiki/Moore%27s_law">Moore is on our side</a>, and we should shift our performance-related optimizations at what it is we as toolkits choose to do <em>with</em> the nodes given to us by whatever selector the spec/browser provides ...</p>
<p>I respect their decision not to adopt the code-base, though am glad there are now two query engines available with which we might start this period of cooperation I long for. I love the fact they are even able to make such a choice. I welcome John to the "Project Lead Council" (I think is what it's called ... ) of the Dojo Foundation, and look forward to working more closely with yet another incredibly talented coder. </p>
<p><strong>Dojo 1.2.3 Released</strong> - We've already quietly released 1.2.1 and 1.2.2, both with minor (though critical stability fixes) changes, but had one large issue needing attention before commiting to pushing the release to the <a href="http://build.dojotoolkit.org/1.2.3/">various</a> <a href="http://code.google.com/apis/ajaxlibs/documentation/index.html#dojo">CDNs</a>.  I pushed a <a href="http://dojotoolkit.org/2008/12/08/dojo-1-2-3-ready-consumption">small announcement</a>, and the <a href="http://download.dojotoolkit.org/current-stable/">source is available</a> in the usual location. The 1.2 release was a strong one, and I might have secretly tricked Adam Peller into cutting a 1.2.2 a day early so that we <em>would</em> have a 1.2.3 (which has a nice ring to it, as far as version numbers go). </p>
<p><strong>Zend Framework 1.7.1</strong> was released, which I note because I failed to mention the <a href="http://devzone.zend.com/article/4045-Zend-Framework-1.7.0-is-now-available">release of 1.7</a> a few weeks back, which included an update to using Dojo 1.2 and several other fixes. The release of Dojo 1.2.3 should be a transparent update for Zend FrameWork 1.7 / Dojo 1.2 users.  Congrats to all the Zend developers! I've installed Git, and have been playing with <a href="http://weierophinney.net/matthew/">Matthew O'Phinney's</a> <a href="http://github.com/weierophinney/pastebin/tree/master">pastebin</a> app in the hopes of putting together a good example of best Dojo practices within Zend Framework ... </p>
<p><strong>dojo.beer()</strong> happened in Munich over the weekend. This is the second of the type put on by the <a href="http://blog.uxebu.com/">Uxebu</a> <a href="http://wolfram.kriesing.de/blog/">folk</a>, and it was reportedly a great success. From what was only a handful of German Dojo Developers grew exponentially it seems -- I even had the pleasure of <a href="http://blog.uxebu.com/2008/12/03/dojobeer-special-guests-and-more/">attending virtually</a> via iChat alongside <a href="http://dylan.io">Dylan</a>, though hopefully I'll be able to attend physically sometime soon ... Thanks to <a href="http://www.mayflower.de">Mayflower</a> for hosting the event, and Wolfram, Tobias, and Nikolai for organizing everything! I love seeing this kind of community-driven meet up happen, be it formal or informal. </p>
<p>That in mind -- I'd love to start something similar regionally: A SouthEast-based <acronym title="Dojo Developer Day">DDD</acronym>, just to get together for the face-to-face and have a dojo.beer(), talk shop and otherwise interact. Tennessee alone houses several Dojo developers I know personally, and I think it feasible to coordinate something somewhere reasonably close: Atlanta, GA? Charlotte, NC? -- In the immediate future we may be doing a small Nashville, TN dojo.beer() -- so if you are interested drop me a line and we'll see what we can put together and pick a good central locale. </p>
]]></content:encoded>
			<wfw:commentRss>http://higginsforpresident.net/2008/12/back-in-business/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dojo: The Missing APIs</title>
		<link>http://higginsforpresident.net/2008/11/dojo-the-missing-apis/</link>
		<comments>http://higginsforpresident.net/2008/11/dojo-the-missing-apis/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 11:59:00 +0000</pubDate>
		<dc:creator>dante hicks</dc:creator>
				<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://higginsforpresident.net/?p=27</guid>
		<description><![CDATA[In my last article I created a set of small, useful utility functions on top of the Base Dojo APIs for personal consumption. Patterns I'd been following and using in Dojo for some time that Dojo didn't have "official" functions for. Well, I've taken it "a little too far", and created a (if I do [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://higginsforpresident.net/2008/10/dojoshow-hide-toggle-and-more/">my last article</a> I created a set of small, useful utility functions on top of the Base Dojo APIs for personal consumption. Patterns I'd been following and using in Dojo for some time that Dojo didn't have "official" functions for. Well, I've taken it "a little too far", and created a (if I do say so myself) rather cool mini-open-source-project called <a href="http://code.google.com/p/plugd/">plugd</a>.  </p>
<p>I've put up a <a href="http://code.google.com/p/plugd/downloads/list">minified version</a> of the most recent "stable" version, and submitted all the code to subversion. Before gzip, the plugin is 2.5k (1.1k after), so I've broken my 1k threshold -- but I think the extra 100 bytes are worth it. So what's new? Where did the bytes comes from!?</p>
<p><a href="http://cometdaily.com/people/michael_carter/">Michael Carter</a> of <a href="http://orbited.org/">Orbited</a> fame stumbled into #dojo recently asking if Dojo had a simple "create some Dom from text" function like jQuery:</p>
<pre class="javascript">&nbsp;
$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;
&lt;div class='bar'&gt;&lt;/div&gt;
&nbsp;
&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">appendTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#baz&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>(<em>note: wordpress doesn't like div tags in code blocks? what's that about?</em>)</p>
<p>And unfortunately, we don't. Dojo's functional approach to things would say: "But dojo.query is a dom <em>selection</em> function, not a dom <em>creation</em> function", which is entirely reasonable.  In order to accomplish the above, you'd have to inject magic [bloat?] into dojo.query functionality to determine if the selector was in fact some DOM, and branch internally to return a NodeList from the created single element. This doesn't seem ideal: it changes the meaning of dojo.query. You can already use query this way by passing a node to dojo.query (and dojo.create from the plugd plugin):</p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> n = dojo.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">appendTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #009900; font-style: italic;">// or, because people seem to like single lines:</span>
dojo.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span>dojo.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">appendTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;</pre>
<p>Which is, to me, acceptable in most cases. It does not, however, solve the "complex markup" use case described initially. It would be really convenient if I were able to make calls like:</p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> n = dojo.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;
&lt;div class='bar'&gt;
&lt;p class='baz'&gt;Hi, &lt;span&gt;Dojo&lt;/span&gt;
&lt;/div&gt;
&nbsp;
&quot;</span><span style="color: #66cc66;">&#41;</span>;
dojo.<span style="color: #006600;">place</span><span style="color: #66cc66;">&#40;</span>n, dojo.<span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #3366CC;">&quot;first&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>So that's where to 100 bytes came from. I added some simple detection to the dojo.create function to either create "just a node from a tag", or a full structure of DOM nodes (with the caveat that it is only allowed a single top-level dom node, such as the div in the example). It could likely be more robust, and work around some cross-browser compatibility issues but I'm aiming for size here and can generally remember to not create invalid markup this way. (Note, there is already a much more robust Dom setter utility in the <em>dojo.html</em> plugin already in the Dojo Toolkit, this syntax is just an extremely lightweight version of that)</p>
<p>Truthfully though, that should not have taken 100 bytes -- there's more. I added other stuff, and rearranged some others: </p>
<ul>
<li>deprecated { bling:true }. <em>bling</em> is a silly word. the djConfig parameter is now simply <em>conflict</em>. eg: djConfig = { conflict:true } will create $, and $().ready() API's, mapping $ to dojo.query and providing the .fn stub for your own plugins.
</li>
<li>added dojo.toggle(node), matching the API for the $().toggle() method already in place</li>
<li>exposed the .val() function in NodeList - it seemed useful</li>
<li>added a private _stash method developers can use for advanced chaining, and a public .end() function to get out of it.
</li>
</ul>
<p>NodeList._stash and .end are nice little utilities, too: Sometimes after making a selection of domNodes you want to return a subset of those nodes, or children relative to those nodes, etc, and returning a new NodeList from the last call makes the most sense (so you can continue chaining, except different nodes this time). Even more often, you would like to break out of that list, and back into the original list.</p>
<p>For example, the .create() method returns a "stashed NodeList" because it makes the most sense to return the newly created node(s). wrap() uses the same logic, though provides an optional 'out' by not returning a stashed list by default. By using _stash in the plugin, and utilizing end() we can write Dojo code that looks like:</p>
<pre class="javascript">&nbsp;
$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;a[href^=http://]&quot;</span><span style="color: #66cc66;">&#41;</span>
      .<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;external&quot;</span><span style="color: #66cc66;">&#41;</span>
      .<span style="color: #006600;">wrap</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #009900; font-style: italic;">// now we're &quot;in&quot; the new list</span>
           .<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;aroundExternalLink&quot;</span><span style="color: #66cc66;">&#41;</span>
      .<span style="color: #006600;">end</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #009900; font-style: italic;">// we're back to the &lt;a&gt;'s, but they have a</span>
&lt;div&gt; around them now
      .<span style="color: #006600;">onclick</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> ... <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>I've also added DOH unit tests for all base.min.js functions, and added some other experimental plugins like a very simple block overlay based on <a href="http://www.sitepen.com/blog/2008/10/17/dojo-building-blocks-of-the-web/">an earlier article</a>, and a cross-browser position:fixed implementation that is incomplete, but has potential. None of the experimental code has very good in-line documentation though, so use with caution (if at all). base.js is <em>mostly</em> documentation, if you feel like reading.  If you are interested in contributing to this mini-project, let me know ... <a href="https://www.ohloh.net/projects/plugd">Ohloh is complaining</a> it only has one developer, and I'd love to see a whole collection of these "unofficial plugins" out in the wild.  </p>
]]></content:encoded>
			<wfw:commentRss>http://higginsforpresident.net/2008/11/dojo-the-missing-apis/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
