<?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>techno.blog(&#34;Dion&#34;) &#187; dojo</title>
	<atom:link href="http://almaer.com/blog/tag/dojo/feed" rel="self" type="application/rss+xml" />
	<link>http://almaer.com/blog</link>
	<description>blogging about life, the universe, and everything tech</description>
	<lastBuildDate>Tue, 28 Aug 2012 14:41:55 +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>Cleanup of manual animation via Dojo Animation</title>
		<link>http://almaer.com/blog/cleanup-of-manual-animation-via-dojo-animation</link>
		<comments>http://almaer.com/blog/cleanup-of-manual-animation-via-dojo-animation#comments</comments>
		<pubDate>Mon, 27 Jul 2009 20:52:54 +0000</pubDate>
		<dc:creator>dion</dc:creator>
				<category><![CDATA[Bespin]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://almaer.com/blog/?p=2485</guid>
		<description><![CDATA[
When posting on the Bespin Pie back in the day, I showed the Dojo animation code that we used to fake out our canvas pie animation:

var anim = dojo.fadeIn&#40;&#123;
    node: &#123; // fake it out for Dojo to think it is changing the style :)
        [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://bengalbraith.files.wordpress.com/2009/05/puck_off.png?w=188&#038;h=188" align="center"></p>
<p>When posting on <a href="http://almaer.com/blog/bespin-gives-you-some-pie-and-will-do-more-web-work-for-you">the Bespin Pie</a> back in the day, I showed the Dojo animation code that we used to fake out our canvas pie animation:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> anim <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    node<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// fake it out for Dojo to think it is changing the style :)</span>
        style<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span>
    easing<span style="color: #339933;">:</span> dojo.<span style="color: #660066;">fx</span>.<span style="color: #660066;">easing</span>.<span style="color: #660066;">backOut</span><span style="color: #339933;">,</span>
&nbsp;
    onAnimate<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> progress <span style="color: #339933;">=</span> values.<span style="color: #660066;">opacity</span><span style="color: #339933;">;</span>
        renderPie<span style="color: #009900;">&#40;</span>progress<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Pete Higgin&#8217;s hinted that we should cleanup this hack (asking to do the opacity and then getting the value back out) and finally (due to a weird bug in a WebKit nightly that gave me reason to poke) I cleaned it up:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">new</span> dojo._Animation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span>
    easing<span style="color: #339933;">:</span> dojo.<span style="color: #660066;">fx</span>.<span style="color: #660066;">easing</span>.<span style="color: #660066;">backOut</span><span style="color: #339933;">,</span>
    curve<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0.0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    onAnimate<span style="color: #339933;">:</span> renderPie
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I am sure there is an easier way too!</p>
<p>FYI, the &#8220;pie&#8221; is finally a pie menu in tip, which will make its way to a Bespin version push soon with functionality we have been dying to get in production since the initial prototype.</p>
]]></content:encoded>
			<wfw:commentRss>http://almaer.com/blog/cleanup-of-manual-animation-via-dojo-animation/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Dealing with JavaScript scope issues; The tale of Alex kindly indulging me</title>
		<link>http://almaer.com/blog/dealing-with-javascript-scope-issues-the-tale-of-alex-kindly-indulging-me</link>
		<comments>http://almaer.com/blog/dealing-with-javascript-scope-issues-the-tale-of-alex-kindly-indulging-me#comments</comments>
		<pubDate>Mon, 30 Mar 2009 15:18:26 +0000</pubDate>
		<dc:creator>dion</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://almaer.com/blog/?p=2335</guid>
		<description><![CDATA[Dealing with JavaScript scope issues; The tale of Alex kindly indulging me
JavaScript is about run-time. Run-time is great and all, but especially when dealing with the browser, and how your Web page has to bootstrap the entire world on every load, Ajax developers have to think about issues that other people don&#8217;t. Problems that others [...]]]></description>
			<content:encoded><![CDATA[<p>Dealing with JavaScript scope issues; The tale of Alex kindly indulging me</p>
<p>JavaScript is about run-time. Run-time is great and all, but especially when dealing with the browser, and how your Web page has to bootstrap the entire world on every load, Ajax developers have to think about issues that other people don&#8217;t. Problems that others can compile away, or know that &#8220;that happens once when I start up the puppy on the server&#8221; are here for us to stay.</p>
<p>This often seems to mean that we have to deal with writing our applications in ways that aren&#8217;t as clean as we may like. We run across problems where for the Nth time someone was bitten as somewhere code did a &#8220;for in&#8221; that wasn&#8217;t guarded with <code>hasOwnProperty</code> and then someone throws up there arms. Never Again. Dojo does a lot of things out of this experience. It is out of real-world pain that choices were made in the toolkit. One of these is how they are very careful not to pollute the global namespace. This is great in that you don&#8217;t run into collisions, especially in a world where code is being sucked in from who knows where (e.g. some Ad code is sucking in things). As the author of some JavaScript code, you don&#8217;t actually know what else may get into your global area when running, so you need to guard against it.</p>
<p>The problem is that this means that you can lose out. Prototype feels so right to me in many ways as it is less of a &#8220;JavaScript library&#8221; than a &#8220;way in which JavaScript should have evolved&#8221;. We have seen some of its goodness get into ES 3.1 (e.g. <code>bind()</code>) but at that rate of progress we will get four more methods in 20 years ;)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;some content   &quot;</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// feels right</span>
dojo.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;some content   &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// doesn't feel right</span></pre></div></div>

<p>I have to take a second to tease here. Did you notice that there are two trim methods in Dojo? <code>dojo.trim</code> and <code>dojo.string.trim</code>. This is a good example of the crazy things that we have to think about. It appears that <code>dojo.trim</code> is lean code, so it gets into base, but <code>dojo.string.trim</code> is more code, but it runs faster. Wow :)</p>
<p>Once you get a lot of code going, you suddenly realise that the word &#8220;dojo&#8221; appears 50 times per screen of code. It makes me want to create a Bespin plugin that change the shade of that one word. It feels like Java. Unlike Java, you can&#8217;t import away some of the pain. You can try to <code>var foo = some.really.big.package;</code> but that gets annoying quickly.</p>
<p>So, to the point of the blog post. When I saw Alex Russell at a nice wine bar in San Francisco last week, I told him how I would die for the ability to have the best of both worlds of Dojo and Prototype:</p>
<blockquote><p>
I want to be able to write code the way that feels right, without the verboseness, but ALSO not run into the scoping issues. I would even take a performance hit for this. What if I could have:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">runSomeCodeWithMagic<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// in here I am in the lovely land where &quot;some content   &quot;.trim() works</span>
    <span style="color: #006600; font-style: italic;">// and I can just forEach instead of dojo.forEach</span>
    <span style="color: #006600; font-style: italic;">// but the outside world isn't affected</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</blockquote>
<p>Well, Alex listened to me blather on, probably thinking I was a total idiot, and then went on to quickly indulge me by giving me a little of my wish by giving me <code>dojo.runWith</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo._runWithObjs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
dojo.<span style="color: #660066;">runWith</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>objs<span style="color: #339933;">,</span> func<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>dojo.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		console.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;runWith must be passed a function to invoke!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003366; font-weight: bold;">var</span> rwo <span style="color: #339933;">=</span> dojo._runWithObjs<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> iLength <span style="color: #339933;">=</span> rwo.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// console.debug(func.toString());</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>dojo.<span style="color: #660066;">isArray</span><span style="color: #009900;">&#40;</span>objs<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		objs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span> objs <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003366; font-weight: bold;">var</span> catchall <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span>dojo.<span style="color: #660066;">global</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> fstr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span> <span style="color: #3366CC;">&quot;(&quot;</span><span style="color: #339933;">,</span> func.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;)()&quot;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	objs.<span style="color: #660066;">unshift</span><span style="color: #009900;">&#40;</span>catchall<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> locals <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	objs.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>locals<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	objs <span style="color: #339933;">=</span> objs.<span style="color: #660066;">reverse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	dojo.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span>objs<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> idx <span style="color: #339933;">=</span> rwo.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
		rwo.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		fstr.<span style="color: #660066;">unshift</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;with(dojo._runWithObjs[&quot;</span><span style="color: #339933;">+</span>idx<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;]){&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		fstr.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Function</span><span style="color: #009900;">&#40;</span>fstr.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// allow us to GC objs passed as contexts, but don't rewind</span>
	<span style="color: #006600; font-style: italic;">// further than we started (allowing nested calls)</span>
	rwo.<span style="color: #660066;">length</span> <span style="color: #339933;">=</span> iLength<span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// TODO:</span>
	<span style="color: #006600; font-style: italic;">//		iterate on locals and look for new properties that</span>
	<span style="color: #006600; font-style: italic;">//		might have been assigned. Maybe give the with-caller a</span>
	<span style="color: #006600; font-style: italic;">//		way to handle them or specify a policy like &quot;make</span>
	<span style="color: #006600; font-style: italic;">//		global&quot;?</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To use it I can do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
    &lt;head&gt;
        &lt;script src=&quot;http://o.aolcdn.com/dojo/1.3.0/dojo/dojo.xd.js&quot;&gt;&lt;/script&gt;
        &lt;script src=&quot;runwith.js&quot;&gt;&lt;/script&gt;
        &lt;script&gt;
        dojo.addOnLoad(function() {
            dojo.runWith([ dojo ], function() {
                var sum = 0;
                forEach([1, 2, 3, 4], function(i) {
                    sum += i;
                });
                byId(&quot;result&quot;).value = &quot;The sum is: &quot; + sum;
            });
        });
        &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1&gt;Run with Wolves&lt;/h1&gt;
&nbsp;
        &lt;input type=&quot;text&quot; id=&quot;result&quot;&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Very nice! Only a couple of dojo&#8217;s in sight!</p>
<p>Here are some of the thoughts from Alex himself:</p>
<blockquote><p>
Note/warning about the runtime cost: If your browser parses things fully in it&#8217;s JS engine up front, this function may hurt. A lot. It de-compiles a function using the toString() method, meaning that it does an uneval + eval + string concat + with() call. Each of these operations alone might be painful in a slow engine. Together they could be fatal. On the other hand, if you&#8217;re using these functions in, e.g., Chrome/V8, this could turn out to be relatively cheap, particularly as this is run-once kinda thing. The runtime cost involves namespace misses on locals, and that can be significant. I dunno. You&#8217;ll have to test to find out.</p>
<p>Note that you won&#8217;t easily be able to define globals from here by dropping a &#8220;var&#8221;. This might be a feature or a bug, depending on how you think about it.</p>
<p>Anyway, hope it&#8217;s useful. I&#8217;d imagine that you&#8217;d structure your files like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// something.js</span>
dojo.<span style="color: #660066;">provide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;thinger.something&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;thinger.blah&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
dojo.<span style="color: #660066;">runWith</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span> dojo<span style="color: #339933;">,</span> thinger <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</blockquote>
<p>What about getting the magic on the core objects? Again, only within the magically land of that scope do we want String to have the trim method&#8230;. and code outside of it shouldn&#8217;t see it. Can we swap onto the objects and their prototypes? Alex has some thoughts here too:</p>
<blockquote><p>
I think I can proxy intrinsics at some additional cost. I&#8217;d like to make it a protocol, though, so that you might be able to have a list or function that handles your extensions. E.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> contextObj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;:intrinsics&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">&quot;String&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;Number&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            <span style="color: #006600; font-style: italic;">// ...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
dojo.<span style="color: #660066;">runWith</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span> contextObj<span style="color: #339933;">,</span> ... <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> ... <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This would give us a way to un-install them later, but I don&#8217;t have a solution for aync code as we do with with(){ &#8230; } which actually binds definitions at declaration time.</p>
<p>I might try to proxy intrinsic prototypes somehow, but I need to spend more time thinking about how to get that to work well. I&#8217;d like these things not to place big constraints on how you think about or use this system.
</p></blockquote>
<p>Wicked. This also ties into Pete Higgins and some of the very interesting work he is doing in similar but different veins with his <a href="http://code.google.com/p/dojotype/">dojotype</a> and <a href="http://code.google.com/p/plugd/">plugd</a> which munge Dojo into interesting forms.</p>
<p>Since Dojo has pretty much everything that any other library has, you can start thinking about how you can bend it to look and feel like others, take on their APIs when it makes sense, and bend it to your whim.</p>
<p>With research like Alex&#8217;s we could see an interesting view when you can create worlds which don&#8217;t affect each other, let you have a view that makes sense for your code, but doesn&#8217;t affect others.</p>
<p>Even if you poo-poo some of it for the performance aspects, this is why it is incredibly exciting to see the latest JavaScript Vm work. With these guys running, they can optimize a lot of this a way, and things that used to be bottlenecks in the code will cease to be.</p>
<p>Thanks to Alex and Pete for indulging me, and taking the time to listen and produce really interesting solutions!</p>
<p><b>Updated: Using the Dojo Loader</b></p>
<p>James Burke has a very cool <a href="http://tagneto.blogspot.com/2009/04/custom-local-variables-using-dojo.html">follow up</a> on how to give a solution to this kind of problem using the Dojo Loader itself:</p>
<p><b>Setup your locals</b></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">provide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;coolio.locals&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
dojo.<span style="color: #660066;">setLocalVars</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;coolio&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
 trim<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;dojo.hitch(dojo, 'trim')&quot;</span><span style="color: #339933;">,</span>
 $<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;dojo.hitch(dojo, 'query')&quot;</span><span style="color: #339933;">,</span>
 id<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;dojo.hitch(dojo, 'byId')&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now setup your actions that will use the locals:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">provide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;coolio.actions&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
coolio.<span style="color: #660066;">actions</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
 init<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#trimButton&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span><span style="color: #009900;">&#40;</span>coolio.<span style="color: #660066;">actions</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   id<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;trimOutput&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> trim<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;trimOutput&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally, use HTML to auto load:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;dojo/dojo.js&quot; djConfig=&quot;require: ['coolio.locals']&quot;&gt;&lt;script&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://almaer.com/blog/dealing-with-javascript-scope-issues-the-tale-of-alex-kindly-indulging-me/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Bespin now learning some art in the Dojo</title>
		<link>http://almaer.com/blog/bespin-now-learning-some-art-in-the-dojo</link>
		<comments>http://almaer.com/blog/bespin-now-learning-some-art-in-the-dojo#comments</comments>
		<pubDate>Mon, 02 Mar 2009 17:40:37 +0000</pubDate>
		<dc:creator>dion</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Bespin]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://almaer.com/blog/?p=2293</guid>
		<description><![CDATA[
Roberto Saccon has given the Bespin project a nice gift, by porting the Bespin codebase to Dojo. This was a lot of work and is very much appreciated. The obvious question is going to be &#8220;Why did you decide to change from Prototype to Dojo?&#8221;
Firstly, Ben and I really like Prototype. I am a Ruby [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://almaer.com/blog/uploads/two.png" alt="two" title="two" width="498" height="462" class="alignnone size-full wp-image-2294" /></p>
<p><a href="http://www.rsaccon.com/">Roberto Saccon</a> has given the Bespin project a nice gift, by porting the Bespin codebase to Dojo. This was a lot of work and is very much appreciated. The obvious question is going to be &#8220;Why did you decide to change from Prototype to Dojo?&#8221;</p>
<p>Firstly, Ben and I really like Prototype. I am a Ruby guy, so it feels good to me. I <a href="http://almaer.com/blog/why-i-often-prefer-prototype-too">talked about it a bit here</a>, and I will use Prototype in many projects in the future.</p>
<p>The state of Ajax frameworks is quite interesting. On the one hand, many of the top dogs have actively learned from each other which has lead to many of them offering similar functionality. For example, Dojo and Prototype can do a good job with DOM selectors, which jQuery is known for.</p>
<p>Although you can do a good job in any of the top libraries, they still differ in scope, and are optimized for certain use cases, project sizes, and functionality wants.</p>
<p>jQuery offers a phenomenal API for doing stuff with the DOM. It feels right. It is also trivial to extend this world, which has lead to the huge number of plugins to do just that. Because of the clean, simple API, we have seen a huge surge in jQuery usage and interest. I would say that it is optimal for designers and beginner JavaScript folk. This is <em>not</em> to say that it isn&#8217;t also great for experts. There is large support from folks like Simon Willison. John Resig did something amazing. If you think functional, this will be your cup of tea even above and beyond.</p>
<p>Prototype goes a little further in that it offers more sugar on top of the language itself. Some find the strapping on of methods on core Objects as obscene. Personally, I love it. It just feels so much nicer for me to say <code>array.last()</code> or <code>array.include(thing)</code>, compared to <code>dojo.indexOf(array, item) &gt; -1</code> for example. In fact, looking at a bunch of code has me feeling a touch nuts with all of the &#8220;dojo.&#8221; items in it. My brain is starting to ignore them.</p>
<p>That being said, Dojo&#8217;s purity gives you a lot. It may be more verbose to <code>dojo.byId</code> than <code>$</code>, but we have no namespace pollution.</p>
<p>The community quickly wanted us to package up Bespin in a way that we could share things. We wanted the same, and have natural components (e.g. the Editor, Thunderhead itself), and some didn&#8217;t like the leakage of the Editor component requiring Prototype which in turn meant pollution into the global namespaces. With Dojo we can hide away nicely, and can even run a build to become <code>foo</code> instead of <code>dojo</code> if we wanted.</p>
<p>Since the community really wanted it, and someone stepped up and actually did it, we accepted the change. On our codebase it had some interesting side effects. For example, before hand we had Prototype, Script.aculo.us, and a slew of third party libraries to give us a bit of this, that, and the other. With Dojo, they had everything we were needing and more, so we could <code>hg rm external/</code> and be done. We are also doing Comet work and the like, and it fits in nicely.</p>
<p>The first change here was to get Bespin working in Dojo, but now we have more work to make sure that:</p>
<ul>
<li>The codebase feels Dojo-y</li>
<li>Use more of the Dojo features (e.g. dojo.keys for key handling, CSS store, Comet, &#8230;)</li>
<li>Clean up and package our stuff nicely (e.g. bespin vs. editor vs. th)</li>
</ul>
<p>If you are a Dojo fan, or fancy getting into it anyway, please <a href="http://groups.google.com/group/bespin">join in!</a> There are a few Prototype things left around, so some of <a href="http://hg.mozilla.org/labs/bespin/file/601a944a1639/frontend/js/bespin/util/util.js">the spirit is there</a>.</p>
<p><b>A bit of an aside&#8230;</b></p>
<p>A big pain with Ajax and components, is the whole &#8220;I really like that jQuery UI component, but I am using Prototype already&#8230;. grrr&#8221;. Simon Kaegi of IBM has been putting together some <a href="http://wiki.eclipse.org/E4/JavaScript">thoughts and code</a> around a JavaScript module system that would enable you to say &#8220;I want service X which happens to depend on jQuery, and service Y which depends on Prototype.&#8221; I am very interested to see where this goes. We sorely need it! The annoying problem on the client is that having multiple libraries is not cheap. On the server though? Not as big a deal potentially.</p>
]]></content:encoded>
			<wfw:commentRss>http://almaer.com/blog/bespin-now-learning-some-art-in-the-dojo/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
