<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Getting StartED with Dojo</title>
	
	<link>http://startdojo.com</link>
	<description>Companion blog to the book</description>
	<lastBuildDate>Mon, 29 Aug 2011 15:42:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/StartDojo" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="startdojo" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">StartDojo</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Twitter-esque Animated Placeholders with Dojo</title>
		<link>http://startdojo.com/2011/04/20/twitter-esque-animated-placeholders-with-dojo/</link>
		<comments>http://startdojo.com/2011/04/20/twitter-esque-animated-placeholders-with-dojo/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 13:56:16 +0000</pubDate>
		<dc:creator>Kyle Hayes</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[animate]]></category>
		<category><![CDATA[cool]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[effects]]></category>
		<category><![CDATA[placeholder]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://startdojo.com/?p=240</guid>
		<description><![CDATA[Have you seen the cool]]></description>
			<content:encoded><![CDATA[<p>Have you seen the cool text input boxes on the Twitter homepage recently (make sure you are logged out). Twitter is using CSS transitions to achieve the effect but unfortunately those are still not fully adopted just yet. I created (with a bit of cleanup help from kgf on #dojo @ irc.freenode.net) a similar effect with Dojo and made a dojo.query() &#8220;plugin&#8221; out of it which does work in most browsers:<br />
<iframe style="width: 100%; height: 500px" src="http://jsfiddle.net/kylehayes/a4D8v/embedded/"></iframe></p>
<p>It&#8217;s simple enough to use this on your own code. Use the same HTML layout, and include the JavaScript plugin code on your page to use the plugin.</p>
]]></content:encoded>
			<wfw:commentRss>http://startdojo.com/2011/04/20/twitter-esque-animated-placeholders-with-dojo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Public Dojo Workshops Announced</title>
		<link>http://startdojo.com/2011/03/13/public-dojo-workshops-announced/</link>
		<comments>http://startdojo.com/2011/03/13/public-dojo-workshops-announced/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 15:06:16 +0000</pubDate>
		<dc:creator>Kyle Hayes</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[1.6]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[workshops]]></category>

		<guid isPermaLink="false">http://startdojo.com/?p=237</guid>
		<description><![CDATA[I&#8217;ve had many ask me]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve had many ask me about any workshops for developers who work with Dojo. When I direct them at <a href="http://www.sitepen.com/">SitePen</a>, they always respond with, but those are geared towards companies—aren&#8217;t there any public workshops? Aside from telling them to read the documentation and getting involved online, I&#8217;ve had to reply with a solemn &#8220;no&#8221;. That has all changed as <a href="http://www.sitepen.com/">SitePen</a> has announced that they are going to start an offering of <a href="http://www.sitepen.com/services/training.php">public workshops</a>.</p>
<p>They are going to offer two of them in San Jose for a first round, we&#8217;ll see what happens after that. They&#8217;re $2500 a pop but it sounds like they will be covering <a href="http://www.sitepen.com/services/training.php">a lot of great content</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://startdojo.com/2011/03/13/public-dojo-workshops-announced/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dojo Classes, Inherited and Constructors</title>
		<link>http://startdojo.com/2011/03/02/dojo-classes-inherited-and-constructors/</link>
		<comments>http://startdojo.com/2011/03/02/dojo-classes-inherited-and-constructors/#comments</comments>
		<pubDate>Wed, 02 Mar 2011 15:53:12 +0000</pubDate>
		<dc:creator>Kyle Hayes</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[declare]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[super]]></category>

		<guid isPermaLink="false">http://startdojo.com/?p=233</guid>
		<description><![CDATA[Dojo provides the ability for]]></description>
			<content:encoded><![CDATA[<p>Dojo provides the ability for developers to create Java-like classes with the powerful dojo.declare(). It does this by allowing you to easily create namespaced classes that support multiple inheritance (which are basically mixins). Provided in this functionality is the ability to call:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">inherited</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230; from within any of your methods in a class that extends another and have it call the super class instance of that method in the same way you would call <em>super()</em> in a Java class to call the super class&#8217; method.</p>
<p>This past week I was on a couple of new classes, one super class and another class that extends that super class. I called <a href="http://dojotoolkit.org/reference-guide/dojo/declare.html#inherited" target="_blank">this.inherited()</a> within my constructor of the class to call the super constructor. I was noticing something strange in Firebug though—my super class&#8217; constructor was getting called twice even though I was only calling it once. I reproduced this in JSFiddle as you can see below:<br />
<iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/kylehayes/KtkYZ/embedded/"></iframe></p>
<p>If you had FireBug (or JavaScript console in WebKit) open you would have seen that the super constructor was getting called twice. After pinging #dojo on irc at irc.freenode.net about the issue, @phiggins reminded me that when you instantiate classes that extend other classes, the constructor method automatically calls the super on its own. So if you call it again, it will be called twice. You can <a href="http://dojotoolkit.org/reference-guide/dojo/declare.html#default-constructor-chaining" target="_blank">read about the default constructor chaining</a> that dojo.declare gives you on the <a href="http://dojotoolkit.org/reference-guide/dojo/declare.html#default-constructor-chaining" target="_blank">Reference Guide</a>.</p>
<p>To summarize, only use <a href="http://dojotoolkit.org/reference-guide/dojo/declare.html#inherited" target="_blank">this.inherited()</a> within non-constructor methods in your classes. I recall running into this issue once before so I thought I blog about it for my own personal reference and for that of others.</p>
]]></content:encoded>
			<wfw:commentRss>http://startdojo.com/2011/03/02/dojo-classes-inherited-and-constructors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Difference between dojo.byId() and dijit.byId()</title>
		<link>http://startdojo.com/2011/01/17/difference-between-dojo-byid-and-dijit-byid/</link>
		<comments>http://startdojo.com/2011/01/17/difference-between-dojo-byid-and-dijit-byid/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 03:28:16 +0000</pubDate>
		<dc:creator>Kyle Hayes</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://startdojo.com/?p=230</guid>
		<description><![CDATA[Since we&#8217;ve completely committed to]]></description>
			<content:encoded><![CDATA[<p>Since we&#8217;ve completely committed to using the Dojo Toolkit at work, there have been more questions raised about various aspects of the toolkit. One of the more popular ones that I am asked and that I see asked often online is <em>what is the difference between dojo.byId() and dijit.byId()?</em>. </p>
<p>Dojo.byId() is simply a replacement for the native document.getElementById(). Dijit.byId() on the other hand is a utility method that you use to get a reference to an instance of a dijit widget on the page. This means that you won&#8217;t be getting a reference to a DOM node like you do with dojo.byId() but rather an instance of a dijit widget class along with access to all of its methods and properties.</p>
]]></content:encoded>
			<wfw:commentRss>http://startdojo.com/2011/01/17/difference-between-dojo-byid-and-dijit-byid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Return JSON with PHP</title>
		<link>http://startdojo.com/2010/06/13/return-json-with-php/</link>
		<comments>http://startdojo.com/2010/06/13/return-json-with-php/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 15:33:30 +0000</pubDate>
		<dc:creator>Kyle Hayes</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[xhr]]></category>

		<guid isPermaLink="false">http://startdojo.com/?p=202</guid>
		<description><![CDATA[When bringing Ajax technologies into]]></description>
			<content:encoded><![CDATA[<p>When bringing Ajax technologies into your websites, it&#8217;s almost certain you&#8217;ll comes across the need to return data from the backend in a format that JavaScript works well with, such as <acronym title="JavaScript Object Notation">JSON</acronym>. With that, PHP is often a popular choice amongst developers for backend programming due to its wide adoption and low cost (FREE!). Thankfully PHP is actively developed and it supports many of todays web standards including JSON.<br />
<span id="more-202"></span><br />
<script type="text/javascript"><!--
google_ad_client = "pub-1110683322124842";
/* Post | Return JSON with PHP #2 */
google_ad_slot = "0425570837";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
Since PHP 5.2, there are a couple of functions to help you consume PHP into the backend as well as return it to your frontend application for an <acronym title="XmlHttpRequest">XHR</acronym> call. We&#8217;ll just look at the return portion since that is what you&#8217;ll be using most commonly. For this, PHP provides you with json_encode() which will return a JSON formatted String of a value you pass to it. The value can be any type except a <a href="http://www.php.net/manual/en/language.types.resource.php">resource</a>. For instance, lets say you&#8217;ve just selected a few rows out of a database and you have an array of values. Below, we&#8217;ll define the array manually so you have a visual idea of what this looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;baz&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To have PHP output this as a JSON string, simply use json_encode($arr) as below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;baz&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><a href="/code/returnjson/">Check out the output of the above code</a>. As you can see it returns a basic JSON array with the values in it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;baz&quot;</span><span style="color: #009900;">&#93;</span></pre></div></div>

<p>If you wanted to return a JSON object, you could use PHP arrays as well. PHP arrays allow you to use Strings as the index. In many languages, this is known as an associative array and it looks like the code below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">&quot;baz&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">&quot;thinger&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;thing&quot;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To put this to practice, say you are returning a simple saved shopping cart to your user, your data might look as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;cartId&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">456186</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">&quot;lastModified&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Tue, 19 Jan 2010 03:14:07 GMT&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">&quot;items&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #cc66cc;">15642</span><span style="color: #339933;">,</span>
    <span style="color: #cc66cc;">45616</span><span style="color: #339933;">,</span>
    <span style="color: #cc66cc;">54984</span><span style="color: #339933;">,</span>
    <span style="color: #cc66cc;">45751</span>
  <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Here we have a cartId, the date it was lastModified, and an array of itemIds. Obviously if you were returning this to the frontend, you would probably want the items to be more descriptive so you don&#8217;t have to make separate calls for each id to get the product info. This is just a demo so it will serve our purposes just fine.</p>
<p><a href="/code/returnjson/cart.php">Check out the output of the above code snippet</a>.</p>
<p>Below is what it looks like as well:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;cartId&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">456186</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;lastModified&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Tue, 19 Jan 2010 03:14:07 GMT&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;items&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">15642</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">45616</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">54984</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">45751</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can see that PHP gave us a JSON object at the highest level (denoted by the beginning and ending curly braces). Each property of the object was the associative array properties that we defined in the PHP code. Finally, you&#8217;ll notice that since we provided an array of ids for items, PHP returned a JSON array with the ids. </p>
<p>Hopefully you can see the value in this. By providing a native JavaScript format of data to our frontend functions, you&#8217;ll have a much easier time working with this data when you use any of the Dojo XHR functions such as dojo.xhrGet().</p>
]]></content:encoded>
			<wfw:commentRss>http://startdojo.com/2010/06/13/return-json-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Decoupling Widget to Widget Interaction with Dojo</title>
		<link>http://startdojo.com/2010/05/21/decoupling-widget-to-widget-interaction-with-dojo/</link>
		<comments>http://startdojo.com/2010/05/21/decoupling-widget-to-widget-interaction-with-dojo/#comments</comments>
		<pubDate>Fri, 21 May 2010 22:03:36 +0000</pubDate>
		<dc:creator>Kyle Hayes</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[dijit]]></category>
		<category><![CDATA[publish]]></category>
		<category><![CDATA[subscribe]]></category>
		<category><![CDATA[topic]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://startdojo.com/?p=124</guid>
		<description><![CDATA[As a programmer, you should]]></description>
			<content:encoded><![CDATA[<p><img src="http://startdojo.com/wp-content/uploads/2010/05/iStock_000005716223XSmall-300x225.jpg" alt="" title="Breaking Chain" width="300" height="225" class="alignleft size-medium wp-image-181" />As a programmer, you should try to think of how you can write your code in a way that is maintainable and loosely coupled from other parts of your application. Loosely coupled basically means that you don&#8217;t tie down functionality and modules to be dependent on other parts of your application. In a way, you want to try and categorize the functionality and keep the functionality in one spot. Take our modern day telephone system for example. If you wanted to use your phone at home to call your friend on their phone on the other side of the world, the phones themselves don&#8217;t have to know each other. All they are concerned about is how to interact directly with the telephone system. The telephone system simply requires a telephone to know how to dial out and to be able to receive a phone call when the line is being rung.<br />
<span id="more-124"></span></p>
<p>Even though the two phones are separate entities in their own right, they are small pieces of functionality in the scope of the whole phone system that reaches the ends of the Earth. On a much smaller scale you want to do that with your application—break it up into smaller chunks of functionality.</p>
<p>This very concept can be applied to modules that you have on your webpages. Modules or more specifically Widgets, can and should be single independent pieces of functionality on a given page. However, lets say you want one module to interact with another, without breaking the cardinal rule of coupling them together? The answer is to use Dojo&#8217;s very powerful yet extremely easy-to-use topic system (<em>aka publish/subscribe</em>).</p>
<p>The dojo.publish() method allows you to build in a simple way for your widget to broadcast a message, which can contain objects of data, that anybody who is listening for that message will receive. The dojo.subscribe() message allows you to setup listeners for that message. It works very much like event listeners when you listen for DOM events such as clicks of a button. Only, instead of the DOM announcing an event, you are announcing your own custom event that something occurred.</p>
<p>How does this all work exactly? Well, let&#8217;s <a href="http://startdojo.com/code/topicdemo/" target="_blank">first take a look at a demo</a>. It&#8217;s pretty simple to play with, just type a message in the textbox and click &#8220;Send Message&#8221;. You&#8217;ll notice your message show up on the right with a timestamp. You can also add more message displays by clicking the button &#8220;Add Message Display&#8221;. You can add as many of these as you want and every one of them will receive the message that you type. Furthermore, any message display box that you have checked the box &#8220;Allow Images&#8221;, you will be able to click the &#8220;Send Image&#8221; button and an image will show up in the appropriate display boxes.</p>
<p>This is a lot easier than it appears. Let&#8217;s break down what the magic is. You can think of the interaction between publish and subscribe kind of like watching TV. If you want to watch a TV show, you tune into, say, Channel 7 and you will see the broadcast. dojo.subscribe() works kind of the same way. You give it a topic to listen to as a String, then provide it a function that you want to execute when it receives data on that channel:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;com.hayes.demo.topics.SendMessage&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onSendMessage</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Like I said, the first argument which is the topic you want to listen to is a string. It can be any string you want. The examples in the Dojo docs use a folder path notation (e.g. com/hayes/demo/topics/SendMessage), but I prefer the dot-notation since it is widely recognized. The second argument that I provide is the scope of which the callback function should be run in, and the third argument is the reference to the function (which could also have been provided as a string &#8220;onSendMessage&#8221;).</p>
<p>To broadcast a message on that same channel you simply use dojo.publish():</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;com.hayes.demo.topics.SendMessage&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
   message<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">msgNode</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here I use the same string that I used in dojo.subscribe, and my second argument is the data I want to send along. Note that here whatever data you send, has to be inside the first element of an array (of course its easiest to simply wrap your data in a set of square brackets). In the case above, I&#8217;m sending a simple plain object with a property labeled as &#8220;message&#8221; with a value of the text that was inputted in the textbox. Any other module on the page you want to receive this same data, you simply use dojo.subscribe() as above and every time a message is sent, all those functions will receive it. That&#8217;s why when you click &#8220;Add Message Display&#8221; they all receive the same message that the Control Panel sends. Every time those modules they get created, they are subscribed to the &#8220;com.hayes.demo.topics.SendMessage&#8221; topic.</p>
<p>This very same methodology is applied with the checkboxes in incrementing the count on the control panel, as well as sending the images. The difference here is that by default, these modules are not subscribed to the &#8220;com.hayes.demo.topics.SendImage&#8221; topic so if you try to send an image, it doesn&#8217;t get sent. However, once you tick the checkbox to allow images, the module will begin to receive the images (which in this case is simply a url for the MessageDisplay box to show in an img tag) that are sent.</p>
<p>Download the source of the demo: <a href="http://startdojo.com/code/topicdemo.tar.gz">tar.gz</a> | <a href="http://startdojo.com/code/topicdemo.zip">zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://startdojo.com/2010/05/21/decoupling-widget-to-widget-interaction-with-dojo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Things about JavaScript I wish I knew earlier</title>
		<link>http://startdojo.com/2010/04/20/things-about-javascript-i-wish-i-knew-earlier/</link>
		<comments>http://startdojo.com/2010/04/20/things-about-javascript-i-wish-i-knew-earlier/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 19:39:49 +0000</pubDate>
		<dc:creator>Kyle Hayes</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[objects]]></category>

		<guid isPermaLink="false">http://startdojo.com/?p=121</guid>
		<description><![CDATA[Christian Hellman wrote another excellent]]></description>
			<content:encoded><![CDATA[<p><img src="http://startdojo.com/wp-content/uploads/2010/05/javascript_blur.png" alt="" title="JavaScript Blur" width="300" height="200" class="alignright size-full wp-image-187" />Christian Hellman wrote another excellent JavaScript article over at <a href="http://www.smashingmagazine.com/">Smashing Magazine</a>. This time he addresses styles and techniques of coding that is widely supported, accepted, and expected by good JavaScript developers. If you’ve been programming JavaScript for awhile, you’ll notice much of the stuff is different now than it was less than a decade ago now that JavaScript has evolved and improved based on the  needs of the JavaScript community as well as the capabilities of modern-day browsers.<br />
<span id="more-121"></span><br />
Personally, the ones I’ve fallen victim to most recently are shortcut notations. Having a Java background, I always wanted to create new Objects and Arrays using the “new” keyword and constructing. During the writing of <a href="http://startdojo.com/buy">Getting Started with Dojo</a>, Peter Higgins (the technical reviewer) pounded in my head that today, objects are created like var foo = {} and arrays are created like var bar = []. Christian discusses this in the article (and a bit more in detail).</p>
<p>Using JSON as a data format is another one I can strongly relate to, especially with his mention of using arrays and delimited strings in the past. Today, there will not be a project that goes by where I need data brought in and I will always choose JSON if it’s available due to its native support in modern day JavaScript.</p>
<p>I personally am always looking for ways to make my JavaScript run faster. As developers, we so often get caught up in the moment of getting something to work and simply implement a solution in a way that just came natural and was easy, we don’t go back to see if we can make it more efficient—especially in terms of browser memory usage. There have been many times I’ve seen a simple for loop accessing data in a way that is calling another method to fetch the data, instead of caching the data once and accessing the local cache. Christian brought up another way to make your page more efficient in terms of events that I had not thought of. His example perfectly illustrates a situation in which you want to listen for the click event on a set of list items inside an unordered list. Instead of looping through each list item and adding an event handler, you can simply add an event handler to the parent of this list (the ul). From here you can determine which li was clicked. This assuming that you understand that events in the DOM roll up from the bottom when an event occurs. This means that if you click an li,if that click is not handled on the li, it will roll up to the ul and so forth.</p>
<p>Finally the last few points on anonymous functions with the module pattern, configuration, interacting with the back end, and browser specific code, I cannot stress enough to read and understand these the best you can. These four points that he clearly makes are utterly important in ensuring you are an efficient JavaScript developer. This will set you apart from being a good JavaScript dev, to an excellent JavaScript dev—proving you know how the language works and how to best control it and use it as it should be used.</p>
<p>I highly respect Christian’s articles and his advice and I recommend you head his comments where you see fit. You can read the full article on Smashing Magazine below:</p>
<p><a href="http://bit.ly/amJ8sj">http://bit.ly/amJ8sj</a></p>
]]></content:encoded>
			<wfw:commentRss>http://startdojo.com/2010/04/20/things-about-javascript-i-wish-i-knew-earlier/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cross-Domain Ajax with Dojo</title>
		<link>http://startdojo.com/2010/02/03/cross-domain-ajax-with-dojo/</link>
		<comments>http://startdojo.com/2010/02/03/cross-domain-ajax-with-dojo/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 04:38:31 +0000</pubDate>
		<dc:creator>Kyle Hayes</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[callback]]></category>
		<category><![CDATA[cross domain]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[dojo.io.script]]></category>
		<category><![CDATA[flickr]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[jsonp]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[webservice]]></category>
		<category><![CDATA[xd]]></category>
		<category><![CDATA[xhr]]></category>

		<guid isPermaLink="false">http://startdojo.com/?p=105</guid>
		<description><![CDATA[If you are fairly new]]></description>
			<content:encoded><![CDATA[<p>If you are fairly new to JavaScript development or Ajax programming, you may not know that JavaScript has a limitation in which it cannot make a remote call to a service that is not on the same domain the script was called from. For instance, if I hosted a simple web service at kylehayes.info, I would not be able to access it from this site through the normal methods such as <a href="http://api.dojotoolkit.org/jsdoc/HEAD/dojo.xhrGet" target="_blank">dojo.xhrGet()</a> or <a href="http://api.dojotoolkit.org/jsdoc/HEAD/dojo.xhrPost" target="_blank">dojo.xhrPost()</a>. However, there are a couple of solutions to this issue. One of those is to consume web services that use a format called <acronym title="JSON with Padding">JSON-P</acronym> (<acronym title="JavaScript Object Notation">JSON</acronym> with padding).<br />
<span id="more-105"></span><br />
Simply put, this method of retrieving data from another domain is done by using a script tag on your page that loads the external file that &#8220;executes&#8221; a named function on your calling page passing it data. Let me show you an example.</p>
<p>Flickr is a great example of a company that offers it&#8217;s web services in a JSON-P format. For instance, a few lines of their <em>interestingness</em> feed looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jsonFlickrApi<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #3366CC;">&quot;photos&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">&quot;page&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;pages&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;perpage&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;total&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">500</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;photo&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>
         <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;4325811507&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;owner&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;75345140@N00&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;secret&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;9d963f8778&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;server&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2702&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;farm&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Apple iMaxiPad (development name iAnvil)&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;ispublic&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;isfriend&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;isfamily&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">0</span>
         <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
         <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;4325354439&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;owner&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;34991336@N00&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;secret&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;4803bbeb7a&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;server&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2767&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;farm&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;San Francisco's Faces Teaser&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;ispublic&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;isfriend&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;isfamily&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">0</span>
         <span style="color: #009900;">&#125;</span>
...<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Notice at the beginning of the statement it begins with <code>jsonFlickrApi(...)</code> which looks just like a function call right? That&#8217;s because it is. In order for it to work though, we need to define that function in our page first so that when this code loads, it can call the function and pass in the data. Start off by creating a function on your page with the same name of the function that your service will call (known as the callback) and give it an argument that the data will be passed in as. Immediately after it, put a script tag that loads your external data.  I&#8217;ll use the <em>interestingness</em> feed from Flickr.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
var jsonFlickrApi = function(data) {
}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&amp;format=json&amp;api_key=9debd0de9deea0e59b4b8ae22a69078a&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>At this point, if you were to load your page, nothing would actually happen since the function doesn&#8217;t do anything once it&#8217;s called. If you&#8217;ve gotten this far without JavaScript warnings from your browser or Firebug, then you are in good shape. Let&#8217;s flesh out the jsonFlickrApi function with actual logic to work with the data that gets passed to our function. All we want to do is create a variable that will hold the contents of our data, in this case it will be the photo information that Flickr returns. Create a global variable that will hold the data, then assign it the data once the function is called:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> photoData <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> jsonFlickrApi <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    photoData <span style="color: #339933;">=</span> data.<span style="color: #660066;">photos</span>.<span style="color: #660066;">photo</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// the array of photo information objects that Flickr passes back</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Running the script now would load the array of photo information into our global variable called photoData. Once this is loaded we can actually play with the data by displaying the photos on our page. This entails adding an element on the page that will hold our image elements and looping through the photos that were returned and placing them in the page. This final product will look like the code below</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot;</span>
<span style="color: #00bbdd;">   &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>interestingness<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
	#imgs img {
	  display: block;
	}
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://o.aolcdn.com/dojo/1.4/dojo/dojo.xd.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
    var photoData = null;
  	var jsonFlickrApi = function(data) {
  	  photoData = data.photos.photo;
  	}
  	dojo.ready(function(){ 
  	  var imgsBody = dojo.byId('imgs');
  	  dojo.forEach(photoData, function(photo){
  	    dojo.place(dojo.create(&quot;img&quot;, {src: &quot;http://farm&quot; + photo.farm + &quot;.static.flickr.com/&quot; + photo.server + &quot;/&quot; + photo.id + &quot;_&quot; + photo.secret + &quot;.jpg&quot;}), imgsBody);
  	  });
  	});
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&amp;format=json&amp;api_key=9debd0de9deea0e59b4b8ae22a69078a&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;imgs&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p><a href="/code/flickrfun/interestingness.html">View a working example of the above code</a></p>
<p>The aforementioned explanation is how you could make a JSON-P call from any JavaScript toolkit (substituting the appropriate API calls where necessary). However, Dojo has built in functionality to make this a bit easier for the developer. The function to look at is dojo.io.script.get(). This allows us to provide a JSONP url, a callback name to use to pass a callback function name, and define a load function that will be used when the script has loaded successfully. An example of this is below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dojo.io.script&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    dojo.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      dojo.<span style="color: #660066;">io</span>.<span style="color: #660066;">script</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
       url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&amp;format=json&amp;api_key=9debd0de9deea0e59b4b8ae22a69078a&quot;</span><span style="color: #339933;">,</span>
       callbackParamName<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jsoncallback&quot;</span><span style="color: #339933;">,</span>
       load<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #003366; font-weight: bold;">var</span> imgsBody <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">byId</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;imgs&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #003366; font-weight: bold;">var</span> photos <span style="color: #339933;">=</span> data.<span style="color: #660066;">photos</span>.<span style="color: #660066;">photo</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// array of photo info</span>
         dojo.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span>photos<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>photo<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     	     dojo.<span style="color: #660066;">place</span><span style="color: #009900;">&#40;</span>dojo.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>src<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://farm&quot;</span> <span style="color: #339933;">+</span> photo.<span style="color: #660066;">farm</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.static.flickr.com/&quot;</span> <span style="color: #339933;">+</span> photo.<span style="color: #660066;">server</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;/&quot;</span> <span style="color: #339933;">+</span> photo.<span style="color: #660066;">id</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;_&quot;</span> <span style="color: #339933;">+</span> photo.<span style="color: #660066;">secret</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.jpg&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> imgsBody<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><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We start out by importing the <a href="http://api.dojotoolkit.org/jsdoc/HEAD/dojo.io.script" target="_blank">dojo.io.script</a> package for use on our page. Then we add a handler for when the page is done loading using <a href="http://api.dojotoolkit.org/jsdoc/HEAD/dojo.ready">dojo.ready</a> (which is simply a proxy function for dojo.addOnLoad). From there we make a dojo.io.script.get() call, passing in an object of properties and values that define our XHR call. You&#8217;ll notice that the Flickr URL is now defined in this list instead of a separate <code>&amp;lt;script&amp;gt;</code> tag. This is the beauty of this function as it allows us to keep our code cleaner and provide the functionality through JavaScript only. In the background, however, Dojo is handling the dynamic creation of the script tag similar to what we used in the first part of this demo.</p>
<p>The actual functionality for this execution didn&#8217;t change and when you <a href="/code/flickrfun/interestingness-dojo.html">run the demo for the code above</a>, you&#8217;ll notice nothing different except for the underlying logic.</p>
]]></content:encoded>
			<wfw:commentRss>http://startdojo.com/2010/02/03/cross-domain-ajax-with-dojo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Source Code for Getting StartED with Dojo</title>
		<link>http://startdojo.com/2010/01/06/source-code-for-getting-started-with-dojo/</link>
		<comments>http://startdojo.com/2010/01/06/source-code-for-getting-started-with-dojo/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 00:23:18 +0000</pubDate>
		<dc:creator>Kyle Hayes</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[updates]]></category>

		<guid isPermaLink="false">http://startdojo.com/?p=71</guid>
		<description><![CDATA[Thanks to a reader for]]></description>
			<content:encoded><![CDATA[<p>Thanks to a reader for reaching out to me and pointing out that the source code is not posted on the Friends of ED site or this site for the book. I sincerely apologize for this inconvenience and mishap. For now, until it is posted on the publisher&#8217;s site, I have provided a link below that will allow you to download all the source code zipped up.</p>
<p><a href='http://startdojo.com/wp-content/uploads/2010/01/gswd-code-examples.zip'>Source Code for Getting StartED with Dojo</a></p>
<p>Note, there is no source code in Chapter 3 and therefore you will not see a &#8220;ch03&#8243; folder in the zip file that you download.</p>
]]></content:encoded>
			<wfw:commentRss>http://startdojo.com/2010/01/06/source-code-for-getting-started-with-dojo/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Simple Ajax Form Tutorial</title>
		<link>http://startdojo.com/2010/01/02/simple-ajax-form-tutorial/</link>
		<comments>http://startdojo.com/2010/01/02/simple-ajax-form-tutorial/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 00:07:21 +0000</pubDate>
		<dc:creator>Kyle Hayes</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xhrPost]]></category>

		<guid isPermaLink="false">http://startdojo.com/?p=45</guid>
		<description><![CDATA[Submitting an Ajax form using]]></description>
			<content:encoded><![CDATA[<p>Submitting an Ajax form using Dojo is very straightforward and simple. The main goal here is to simply send the values of a form to a backend script (whatever your preference, e.g. PHP or Python), without ever leaving the page. The page should also show a response to the user confirming that their request was sent successfully.</p>
<p>For starters, we need a simple HTML form that we are going to send. Construct this as you normally would with an id, action, and method values. In my example, I&#8217;ve created a basic contact form:<br />
<span id="more-45"></span></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Contact Form<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;contact&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;contactFormAction.php&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;POST&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dl</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dt</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;txtFirstName&quot;</span>&gt;</span>First Name:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dt</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dd</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;txtFirstName&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;firstName&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dd</span>&gt;</span>
&nbsp;
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dt</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;txtLastName&quot;</span>&gt;</span>Last Name:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dt</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dd</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;txtLastName&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lastName&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dd</span>&gt;</span>
&nbsp;
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dt</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;txtEmail&quot;</span>&gt;</span>Email Address:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dt</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dd</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;txtEmail&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dd</span>&gt;</span>
&nbsp;
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dt</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;txtComments&quot;</span>&gt;</span>Comments / Suggestions<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dt</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dd</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;txtComments&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;comments&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dd</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dl</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit Form&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>As you can see, I have my contact form posting to a PHP script called <em>contactFormAction.php</em>. This is a very basic script that doesn&#8217;t actually email in our demo. It simply will read one of the values that is posted from the form, firstName, and display that along with a message encoded in a JSON string.</p>
<p>The contents of my contactFormAction.php file is:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #666666; font-style: italic;">// Logic to send an email would go here</span>
  <span style="color: #666666; font-style: italic;">// for now we are just going to send back a simple</span>
  <span style="color: #666666; font-style: italic;">// JSON response to show a successful submission</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'{&quot;success&quot;:&quot;true&quot;,&quot;message&quot;:&quot;Message sent successfully to '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;firstName&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.&quot;}'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><em>Note, that in order for something to be considered as a proper <acronyon title="JavaScript Object Notation">JSON</acronym> string, it needs to have all the properties and values in quotes.</em></p>
<p>At this point, if you were to setup these two pages and your webserver and submitted the form, everything would work as expected. The only difference is that when you posted the form, the browser would direct you to the PHP script to show it&#8217;s output. The whole idea with this exercise however is to submit this form without leaving the page and notifying the user that it was successful. This is very simple with Dojo&#8217;s <a href="http://api.dojotoolkit.org/jsdoc/HEAD/dojo.xhrPost" target="_blank">xhrPost()</a> function.</p>
<p>We need to first pull the Dojo toolkit library into our page by simply putting the following in the HEAD of our HTML page:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/dojo/1.4/dojo/dojo.xd.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>Once we have that included we can write the following JavaScript between a script block in our HEAD as well:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">addOnLoad</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  dojo.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>dojo.<span style="color: #660066;">byId</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'submit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'onclick'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    dojo.<span style="color: #660066;">stopEvent</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    dojo.<span style="color: #660066;">xhrPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      form<span style="color: #339933;">:</span> <span style="color: #3366CC;">'contact'</span><span style="color: #339933;">,</span>
      handleAs<span style="color: #339933;">:</span> <span style="color: #3366CC;">'json'</span><span style="color: #339933;">,</span>
      load<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> success <span style="color: #339933;">=</span> data.<span style="color: #660066;">success</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> msg <span style="color: #339933;">=</span> data.<span style="color: #660066;">message</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>success<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          dojo.<span style="color: #660066;">byId</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'responseMsg'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> msg<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      error<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>error<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: #006600; font-style: italic;">// xhrPost()</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// connect()</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// addOnLoad()</span></pre></div></div>

<p>This series of anonymous functions first creates a listener for when the page has completed loading (addOnLoad()), then sets up an event listener to listen for the <em>onclick</em> event from the submit button (dojo.connect()). Inside that event listener function we first stop the click event from proceeding with it&#8217;s default actions (i.e. submitting the form in a traditional manner), and we proceed with the xhrPost() function.</p>
<p>Dojo&#8217;s xhrPost() allows you to simply provide the <em>id</em> of a form element for it to determine where to post the form&#8217;s contents to. This is convenient because if you want the form to be able to be submitted even if the user didn&#8217;t have JavaScript support in their browser (such as some mobile devices), they still could. If they do have JavaScript support, then the xhrPost() function will send the Ajax request to the URL in the action attribute of the form. The handleAs property tells xhrPost what type of data will be returned from the script you are posting to. In my case, I created the PHP script to return a JSON string. Upon successful posting to that script, it will return a JSON string, and xhrPost will parse the string and turn it into a JavaScript object automatically and send it to our load function. The load function simply receives a single attribute which I called data. This is what our response will be sent back in. In my load function I first log to the Firebug console the value of data, then I determine if success was true and if it is I display the included message to the element with the <em>id</em> of &#8220;responseMsg&#8221;. I added this element inside a fieldset block below the form:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">fieldset</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">legend</span>&gt;</span>Response from server<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">legend</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;responseMsg&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">fieldset</span>&gt;</span></pre></div></div>

<p>That&#8217;s the basis of it, very easy and straightforward. Check out an online demo of this <a href="/code/ajaxform/" target="_blank">simple Ajax contact form</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://startdojo.com/2010/01/02/simple-ajax-form-tutorial/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

