<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>SitePen Blog</title>
	<atom:link href="http://www.sitepen.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepen.com/blog</link>
	<description>SitePen Services and notes about Dojo, DWR, Cometd, JavaScript, and the Web</description>
	<lastBuildDate>Wed, 11 Aug 2010 14:31:53 +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>Dive Into Dijit Forms</title>
		<link>http://www.sitepen.com/blog/2010/08/11/dive-into-dijit-forms/</link>
		<comments>http://www.sitepen.com/blog/2010/08/11/dive-into-dijit-forms/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 14:30:53 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[dijit]]></category>
		<category><![CDATA[dojox]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1727</guid>
		<description><![CDATA[
As was illustrated with our Dive Into Dijit post, the Dijit library provides an extremely powerful, flexible set of Dojo-based widgets with which you may easily enhance the look and functionality of your web application.  These widgets include drop down / popup menus, dialogs, page layouts, trees, progress bars, and form elements.  When looking at [...]


Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/07/12/dive-into-dijit/' rel='bookmark' title='Permanent Link: Dive Into Dijit'>Dive Into Dijit</a></li><li><a href='http://www.sitepen.com/blog/2009/11/05/dijit-prepackaged/' rel='bookmark' title='Permanent Link: Dijit: Prepackaged'>Dijit: Prepackaged</a></li><li><a href='http://www.sitepen.com/blog/2009/02/25/styling-dijit-form-elements/' rel='bookmark' title='Permanent Link: Styling Dijit Form Elements'>Styling Dijit Form Elements</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p><a href="https://user.sitepen.com/~dwalsh/dijit-forms.html"><img src="https://user.sitepen.com/~dwalsh/Form1.png" style="display:block;margin:0 0 20px 20px;float:right;" /></a></p>
<p>As was illustrated with our <a href="http://www.sitepen.com/blog/2010/07/12/dive-into-dijit/">Dive Into Dijit</a> post, the Dijit library provides an extremely powerful, flexible set of Dojo-based widgets with which you may easily enhance the look and functionality of your web application.  These widgets include drop down / popup menus, dialogs, page layouts, trees, progress bars, and form elements.  When looking at these elements, it&#8217;s easy to see that Dijit enhances their presentation but this post will focus on enhancing functionality;  specifically, enhancing a basic form with usability improvements and validation.</p>
<p><span id="more-1727"></span></p>
<h2>Quick Dijit Implementation Review</h2>
<p>The first step in introducing Dijit to any page is including Dojo:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/script.html"><span class="kw2">&lt;script</span></a> <span class="kw3">src</span>=<span class="st0">&quot;http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js.uncompressed.js&quot;</span> djConfig=<span class="st0">&quot;isDebug:true,parseOnLoad:true&quot;</span><span class="kw2">&gt;</span></span><span class="sc2"><span class="kw2">&lt;/script&gt;</span></span><br />
&nbsp;</div>
<p>The next step is requesting the Dijit theme stylesheet:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/style.html"><span class="kw2">&lt;style</span></a> <span class="kw3">type</span>=<span class="st0">&quot;text/css&quot;</span><span class="kw2">&gt;</span></span><br />
@import &quot;http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/resources/dojo.css&quot;;<br />
@import &quot;http://ajax.googleapis.com/ajax/libs/dojo/1.5/dijit/themes/claro/claro.css&quot;;<br />
<span class="sc2"><span class="kw2">&lt;/style&gt;</span></span><br />
&nbsp;</div>
<p>The last step is adding the theme name as a class for the <code>BODY</code> element:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/body.html"><span class="kw2">&lt;body</span></a> <span class="kw3">class</span>=<span class="st0">&quot;claro&quot;</span><span class="kw2">&gt;</span></span><br />
&nbsp;</div>
<h2>Enhancing Basic Form Elements</h2>
<p><em>Note:  this tutorial will use the declarative method of widget creation.  You may create any Dijit widget programatically using <code>dojo.behavior</code> as described in <a href="http://www.sitepen.com/blog/2010/07/12/dive-into-dijit/">Dive Into Dijit</a>.</em></p>
<p>The first step in enhancing our static form is enhancing the form elements themselves.  Dijit provides a corresponding widget (sometimes two or three) for the different types of form elements.  Using the declarative method of Dijit widget creation and the dojoType attribute, we&#8217;ll quickly widget-ize every piece of our static form:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/form.html"><span class="kw2">&lt;form</span></a> <span class="kw3">action</span>=<span class="st0">&quot;process.php&quot;</span> <span class="kw3">method</span>=<span class="st0">&quot;get&quot;</span><span class="kw2">&gt;</span></span><br />
<span class="sc2"><span class="coMULTI">&lt;!&#8211; text inputs: &nbsp;dijit.form.TextBox &#8211;&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/strong.html"><span class="kw2">&lt;strong&gt;</span></a></span>First Name: <span class="sc2"><span class="kw2">&lt;/strong&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&quot;text&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;firstName&quot;</span> placeholder=<span class="st0">&quot;Your Name&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;firstName&quot;</span> dojoType=<span class="st0">&quot;dijit.form.TextBox&quot;</span> /<span class="kw2">&gt;</span></span></p>
<p><span class="sc2"><a href="http://december.com/html/4/element/strong.html"><span class="kw2">&lt;strong&gt;</span></a></span>Website: &nbsp;<span class="sc2"><span class="kw2">&lt;/strong&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&quot;text&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;website&quot;</span> placeholder=<span class="st0">&quot;Your Website&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;website&quot;</span> dojoType=<span class="st0">&quot;dijit.form.TextBox&quot;</span> /<span class="kw2">&gt;</span></span></p>
<p><span class="sc2"><span class="coMULTI">&lt;!&#8211; radio buttons: &nbsp;dijit.form.FilteringSelect &#8211;&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/strong.html"><span class="kw2">&lt;strong&gt;</span></a></span>Favorite Color: <span class="sc2"><span class="kw2">&lt;/strong&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/select.html"><span class="kw2">&lt;select</span></a> <span class="kw3">name</span>=<span class="st0">&quot;color&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;color&quot;</span> dojoType=<span class="st0">&quot;dijit.form.FilteringSelect&quot;</span><span class="kw2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;&quot;</span><span class="kw2">&gt;</span></span>Select a Color<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;Red&quot;</span><span class="kw2">&gt;</span></span>Red<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;Green&quot;</span><span class="kw2">&gt;</span></span>Green<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;Blue&quot;</span><span class="kw2">&gt;</span></span>Blue<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
<span class="sc2"><span class="kw2">&lt;/select&gt;</span></span></p>
<p><span class="sc2"><span class="coMULTI">&lt;!&#8211; radio buttons: &nbsp;dijit.form.CheckBox &#8211;&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/strong.html"><span class="kw2">&lt;strong&gt;</span></a></span>Send Emails? <span class="sc2"><span class="kw2">&lt;/strong&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&quot;checkbox&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;checkbox&quot;</span> <span class="kw3">checked</span>=<span class="st0">&quot;checked&quot;</span> dojoType=<span class="st0">&quot;dijit.form.CheckBox&quot;</span> /<span class="kw2">&gt;</span></span></p>
<p><span class="sc2"><span class="coMULTI">&lt;!&#8211; radio buttons: &nbsp;dijit.form.RadioButton &#8211;&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/strong.html"><span class="kw2">&lt;strong&gt;</span></a></span>Email Format: <span class="sc2"><span class="kw2">&lt;/strong&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&quot;radio&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;radio1&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;format&quot;</span> <span class="kw3">checked</span>=<span class="st0">&quot;checked&quot;</span> dojoType=<span class="st0">&quot;dijit.form.RadioButton&quot;</span> /<span class="kw2">&gt;</span></span> <br />
<span class="sc2"><a href="http://december.com/html/4/element/label.html"><span class="kw2">&lt;label</span></a> <span class="kw3">for</span>=<span class="st0">&quot;radio1&quot;</span><span class="kw2">&gt;</span></span>HTML<span class="sc2"><span class="kw2">&lt;/label&gt;</span></span><br />
&nbsp; &nbsp;<br />
<span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&quot;radio&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;radio2&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;format&quot;</span> dojoType=<span class="st0">&quot;dijit.form.RadioButton&quot;</span> /<span class="kw2">&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/label.html"><span class="kw2">&lt;label</span></a> <span class="kw3">for</span>=<span class="st0">&quot;radio2&quot;</span><span class="kw2">&gt;</span></span>Text<span class="sc2"><span class="kw2">&lt;/label&gt;</span></span></p>
<p><span class="sc2"><span class="coMULTI">&lt;!&#8211; submit button: &nbsp;dijit.form.Button &#8211;&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&quot;submit&quot;</span> <span class="kw3">value</span>=<span class="st0">&quot;Submit Form&quot;</span> <span class="kw3">label</span>=<span class="st0">&quot;Submit Form&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;submitButton&quot;</span> dojoType=<span class="st0">&quot;dijit.form.Button&quot;</span> /<span class="kw2">&gt;</span></span><br />
<span class="sc2"><span class="kw2">&lt;/form&gt;</span></span><br />
&nbsp;</div>
<p>Boom:  our static, boring form elements have been themed and extended with extra functionality.  A few notes with regard to widgets we created above:</p>
<ul>
<li>The <code>FilteringSelect</code> widget duplicates the functionality of a native <code>SELECT</code> element by:
<ul>
<li>Setting an initial value based on one of its options having a selected attribute.</li>
<li>Enforcing a fixed set of possible results based upon the values and text of each <code>OPTION</code> element.</li>
<li>Providing keyboard accessibility</li>
</ul>
</li>
<li>
		The <code>FilteringSelect</code> widget adds the following functionality to the basic <code>SELECT</code> element:</p>
<ul>
<li>You may type values into the <code>FilteringSelect</code>; autocomplete is employed.</li>
<li>If an invalid value is provided, an error message is provided to the user.</li>
<li>You get more control over the display when the widget is disabled.</li>
</ul>
</li>
<li>Dojo 1.5 introduces strategies for employing placeholder text within <code>INPUT</code> elements to save you time in adding focus/blur events to accomplish the same functionality.</li>
</ul>
<p>Now that the basic form has been widget-ized and themed, we can add basic validation functionality.</p>
<h2>Form Validation with Dojo</h2>
<p>As with just about every client-side problem, Dojo&#8217;s got a great solution for form validation. Luckily there are only a few key components required for basic form validation.</p>
<h3>dijit.form.ValidationTextBox</h3>
<p>A precursor to the overall validation of a form is deciding which elements are required.  Say we want to require the <code>first</code> field;  the <code>dojoType</code> of that will will change from <code>dijit.form.TextBox</code> to <code>dijit.form.ValidationTextBox</code>:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> dojoType=<span class="st0">&quot;dijit.form.ValidationTextBox&quot;</span> required=<span class="st0">&quot;true&quot;</span> placeholder=<span class="st0">&quot;Your Name&quot;</span> missingMessage=<span class="st0">&quot;Ooops! &nbsp;You forgot your first name!&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;firstName&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;firstName&quot;</span> <span class="kw3">type</span>=<span class="st0">&quot;text&quot;</span> /<span class="kw2">&gt;</span></span><br />
&nbsp;</div>
<p>Since <code>required="true"</code> has been added to the widget, an error icon and tooltip (with error message) will display if the user fails to place text into the box.  A custom error message can be placed within the <code>missingMessage</code> attribute, otherwise a generic message will display.</p>
<p>What if the field requires special validation of the pattern of input?  That&#8217;s where the <code>regExp</code> attribute comes in:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> dojoType=<span class="st0">&quot;dijit.form.ValidationTextBox&quot;</span> required=<span class="st0">&quot;true&quot;</span> regExp=<span class="st0">&quot;(https?|ftp)://[A-Za-z0-9-_]+\.[A-Za-z0-9-_%&amp;\?\/\.=]+&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;website&quot;</span> placeholder=<span class="st0">&quot;Your Website&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;website&quot;</span> <span class="kw3">type</span>=<span class="st0">&quot;text&quot;</span> /<span class="kw2">&gt;</span></span><br />
&nbsp;</div>
<p>Not only is the <code>website</code> field now required but the value of the field must pass the regular expression test provided by the <code>regExp</code> attribute.</p>
<h3>Validation Utilities with dojox.validate</h3>
<p>
The <code>dojox.validate</code> library includes numerous utility functions and regular expressions for validating form element values. These utility functions can validate email addresses, URLs, phone numbers, ZIP codes, and much more.  An example usage of <code>dojox.validate</code> with a <code>ValidationTextBox</code> would look like:
</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/script.html"><span class="kw2">&lt;script</span></a> <span class="kw3">type</span>=<span class="st0">&quot;text/javascript&quot;</span><span class="kw2">&gt;</span></span><br />
dojo.require(&quot;dojox.validate&quot;);<br />
dojo.require(&quot;dojox.validate.web&quot;);<br />
<span class="sc2"><span class="kw2">&lt;/script&gt;</span></span></p>
<p><span class="sc2"><a href="http://december.com/html/4/element/strong.html"><span class="kw2">&lt;strong&gt;</span></a></span>Email:<span class="sc2"><span class="kw2">&lt;/strong&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&quot;text&quot;</span> required=<span class="st0">&quot;true&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;email&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;email&quot;</span> dojoType=<span class="st0">&quot;dijit.form.ValidationTextBox&quot;</span> validator=<span class="st0">&quot;dojox.validate.isEmailAddress&quot;</span> /<span class="kw2">&gt;</span></span><br />
&nbsp;</div>
<p>The <code>validator</code> attribute is a link to the validation function for emails. <code>dojox.validate</code> is especially helpful if you don&#8217;t feel comfortable with regular expressions.  There are also locale-specific packages within <code>dojox.validate</code>.  The API docs provide a <a href="http://dojotoolkit.org/api/dojox/validate.html">complete listing of <code>dojox.validate</code> helpers</a>.</p>
<h3>dijit.form.Form with the onSubmit Event</h3>
<p>Now that our required fields are in place, we&#8217;ll enhance the wrapping <code>form</code> element with a <code>dijit.form.Form dojoType</code>:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/form.html"><span class="kw2">&lt;form</span></a> dojoType=<span class="st0">&quot;dijit.form.Form&quot;</span> <span class="kw3">action</span>=<span class="st0">&quot;process.php&quot;</span> <span class="kw3">method</span>=<span class="st0">&quot;get&quot;</span><span class="kw2">&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/script.html"><span class="kw2">&lt;script</span></a> <span class="kw3">type</span>=<span class="st0">&quot;dojo/method&quot;</span> event=<span class="st0">&quot;onSubmit&quot;</span><span class="kw2">&gt;</span></span><br />
<span class="sc2"><span class="coMULTI">&lt;!&#8211;<br />
if (this.validate()) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; alert(&#8217;Form is valid, submitting!&#8217;);<br />
} else {<br />
&nbsp; &nbsp; &nbsp; &nbsp; alert(&#8217;Form contains invalid. &nbsp;Please complete all required fields.&#8217;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; return false;<br />
}<br />
return true;<br />
&#8211;&gt;</span></span><br />
<span class="sc2"><span class="kw2">&lt;/script&gt;</span></span><br />
<span class="sc2"><span class="coMULTI">&lt;!&#8211; form fields here &#8211;&gt;</span></span><br />
<span class="sc2"><span class="kw2">&lt;/form&gt;</span></span><br />
&nbsp;</div>
<p>Accompanying the <code>dijit.form.Form</code> is a special <code>script</code> element.  Within this Dojo-specific script is a <code>this.validate()</code> test, acting on the <code>dijit.form.Form</code> instance, which validates the entire form based on <code>require="true"</code> inputs. You could also add your own custom validation within the code block as well.</p>
<h2>The dijit.form Collection</h2>
<p>I&#8217;ve only touched the most-used Dijit widgets within my example above.  There are several more outstanding form widgets within Dijit; let&#8217;s take a look at a few other helpful widgets!
</p>
<h3>DateTextBox</h3>
<p><a href="https://user.sitepen.com/~dwalsh/dijit-forms.html"><img src="https://user.sitepen.com/~dwalsh/Form5.png" style="display:block;margin:0 0 20px 20px;float:right;" /></a></p>
<p>Asking the user to format a date properly can be a nightmare, especially if other form fields within the page rely on the contents of a given date field.  Dijit provides a <code>dijit.form.DateTextBox</code> class which displays a user-friendly calendar widget for users to select a date on.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><span class="coMULTI">&lt;!&#8211; when the user focuses on this element, the calendar appears &#8211;&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> dojoType=<span class="st0">&quot;dijit.form.DateTextBox&quot;</span> required=<span class="st0">&quot;true&quot;</span> invalidMessage=<span class="st0">&quot;Please provide a valid date.&quot;</span> <span class="kw3">type</span>=<span class="st0">&quot;text&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;date&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;date&quot;</span> /<span class="kw2">&gt;</span></span><br />
&nbsp;</div>
<h3>CurrencyTextBox</h3>
<p>The <code>dijit.form.CurrencyTextBox</code> class helps the user to properly format and validate currency per a given locale.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><span class="coMULTI">&lt;!&#8211; {fractional:true} means you must provide cents &#8211;&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> dojoType=<span class="st0">&quot;dijit.form.CurrencyTextBox&quot;</span> required=<span class="st0">&quot;true&quot;</span> constraints=<span class="st0">&quot;{fractional:true}&quot;</span> currency=<span class="st0">&quot;USD&quot;</span> invalidMessage=<span class="st0">&quot;Please provide both dollars and cents.&quot;</span> <span class="kw3">type</span>=<span class="st0">&quot;text&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;weekly_wages&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;weekly_wages&quot;</span> <span class="kw3">value</span>=<span class="st0">&quot;2000&quot;</span> /<span class="kw2">&gt;</span></span><br />
&nbsp;</div>
<h3>Textarea</h3>
<p>The <code>dojox.form.Textarea</code> class enhances a given <code>TEXTAREA</code> element so that the element grows in height as the user types.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/textarea.html"><span class="kw2">&lt;textarea</span></a> dojoType=<span class="st0">&quot;dijit.form.Textarea&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;comments&quot;</span><span class="kw2">&gt;</span></span><span class="sc2"><span class="kw2">&lt;/textarea&gt;</span></span><br />
&nbsp;</div>
<h2>Enhancing Basic Dijit Widgets with DojoX Alternatives</h2>
<p><a href="https://user.sitepen.com/~dwalsh/dijit-forms.html"><img src="https://user.sitepen.com/~dwalsh/Form2.png" style="display:block;margin:0 0 20px 20px;float:right;" /></a></p>
<p>As nice as many of the Dijit widgets are, DojoX hosts numerous enhanced widgets that solve problems that many basic widgets cannot. The following are a few notable DojoX form widgets.</p>
<h3>BusyButton</h3>
<p><code>dijit.form.Button</code> works great but what if I want to disable the button (to avoid double submissions) and provide a feedback message (i.e. &#8220;Sending form&#8230;.&#8221;) when clicked? We could use <code>dojox.form.BusyButton</code> to do just that:
</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><span class="coMULTI">&lt;!&#8211; assuming dojox.form.BusyButton has been required&#8230; &#8211;&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/button.html"><span class="kw2">&lt;button</span></a> dojoType=<span class="st0">&quot;dojox.form.BusyButton&quot;</span> busyLabel=<span class="st0">&quot;Sending Data&#8230;&quot;</span><span class="kw2">&gt;</span></span><br />
&nbsp; &nbsp; Send Data<br />
<span class="sc2"><span class="kw2">&lt;/button&gt;</span></span><br />
&nbsp;</div>
<h3>CheckedMultiSelect</h3>
<p><a href="https://user.sitepen.com/~dwalsh/dijit-forms.html"><img src="https://user.sitepen.com/~dwalsh/Form3.png" style="display:block;margin:0 0 20px 20px;float:right;" /></a></p>
<p>What if our SELECT element allows for multiple selections?  That&#8217;s the perfect opportunity to use the <code>dojox.form.CheckedMultiSelect</code> widget:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><span class="coMULTI">&lt;!&#8211; assuming dojox.form.CheckedMultiSelect has been required&#8230; &#8211;&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/select.html"><span class="kw2">&lt;select</span></a> <span class="kw3">multiple</span>=<span class="st0">&quot;true&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;rockers&quot;</span> dojoType=<span class="st0">&quot;dojox.form.CheckedMultiSelect&quot;</span><span class="kw2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;Oasis&quot;</span><span class="kw2">&gt;</span></span>Oasis<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;Rod Stewart&quot;</span> <span class="kw3">selected</span>=<span class="st0">&quot;selected&quot;</span><span class="kw2">&gt;</span></span>Rod Stewart<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;Coldplay&quot;</span> <span class="kw3">selected</span>=<span class="st0">&quot;selected&quot;</span><span class="kw2">&gt;</span></span>Coldplay<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;Kings of Leon&quot;</span><span class="kw2">&gt;</span></span>Kings of Leon<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
<span class="sc2"><span class="kw2">&lt;/select&gt;</span></span><br />
&nbsp;</div>
<h3>PasswordValidator</h3>
<p><a href="https://user.sitepen.com/~dwalsh/dijit-forms.html"><img src="https://user.sitepen.com/~dwalsh/Form4.png" style="display:block;margin:0 0 20px 20px;float:right;" /></a></p>
<p>What if our website features a form that allows updating/changing of passwords?  The <code>dojox.form.PasswordValidator</code> provides all the functionality you need to quickly implement that system:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><span class="coMULTI">&lt;!&#8211; assuming dojox.form.PasswordValidator has been required&#8230; &#8211;&gt;</span></span><br />
<span class="sc2"><span class="coMULTI">&lt;!&#8211; pwValidate is the name of your function that verifies the current password is correct &#8211;&gt;</span></span>&nbsp; <br />
<span class="sc2"><a href="http://december.com/html/4/element/div.html"><span class="kw2">&lt;div</span></a> dojoType=<span class="st0">&quot;dojox.form.PasswordValidator&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;pwValidate&quot;</span><span class="kw2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><span class="coMULTI">&lt;!&#8211; pwType=old is where the user must place their current password &#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&quot;password&quot;</span> pwType=<span class="st0">&quot;old&quot;</span> /<span class="kw2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><span class="coMULTI">&lt;!&#8211; pwType=new is where the proposed new password must be placed &#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&quot;password&quot;</span> pwType=<span class="st0">&quot;new&quot;</span> /<span class="kw2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><span class="coMULTI">&lt;!&#8211; pwType=new is where the user verifies their new pass &#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&quot;password&quot;</span> pwType=<span class="st0">&quot;verify&quot;</span> /<span class="kw2">&gt;</span></span><br />
<span class="sc2"><span class="kw2">&lt;/div&gt;</span></span><br />
&nbsp;</div>
<p><em>Reminder:  JavaScript validation is not a substitute for server-side validation;  JavaScript simply enhances the user experience by providing instant feedback to the user.</em></p>
<h3>FileUploader</h3>
<p>File uploading without enhancement is probably the worst form control available.  The <code>dojox.form.FileUploader</code> provides a great solution for making the process more streamlined:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><span class="coMULTI">&lt;!&#8211; assuming dojox.form.FileUploader has been required&#8230; &#8211;&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/div.html"><span class="kw2">&lt;div</span></a> <span class="kw3">class</span>=<span class="st0">&quot;uploadBtn&quot;</span> dojoType=<span class="st0">&quot;dojox.form.FileUploader&quot;</span> hoverClass=<span class="st0">&quot;uploadHover&quot;</span> pressClas=<span class="st0">&quot;uploadPress&quot;</span> activeClass=<span class="st0">&quot;uploadBtn&quot;</span> disabledClass=<span class="st0">&quot;uploadDisable&quot;</span> uploadUrl=<span class="st0">&quot;/upload-files.php&quot;</span><span class="kw2">&gt;</span></span>Select Files<span class="sc2"><span class="kw2">&lt;/div&gt;</span></span><br />
&nbsp;</div>
<h2>The dojox.form Collection</h2>
<p>
	The dojox.form namespace hosts a huge host of widget enhancements, including:
</p>
<ul>
<li>BusyButton &#8211; button with special disable/busy label states</li>
<li>CheckedMultiSelect &#8211; turns your SELECT element with the &#8220;multiple&#8221; attribute into a series of checkboxes for usability</li>
<li>DropDownStack &#8211; disable/enable form elements based upon a selection</li>
<li>RangeSlider &#8211; allows values to be chosen via a sliding scale</li>
<li>Rating &#8211; easily creates UI for ratings (star ratings)</li>
<li>&#8230;and much more!&trade;</li>
</ul>
<h2>Great Dijit &amp; Dojox Resources</h2>
<ul>
<li><a href="https://user.sitepen.com/~dwalsh/dijit-forms.html">View Working Example</a></li>
<li><a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dijit/themes/themeTester.html">Dijit Theme Tester</a></li>
<li><a href="http://www.sitepen.com/blog/2010/07/12/dive-into-dijit/">Dive Into Dijit</a></li>
<li><a href="http://www.dojotoolkit.org/reference-guide/dojox/form.html#dojox-form">DojoX Form Reference Guide</a></li>
<li><a href="http://www.dojotoolkit.org/reference-guide/dijit/form.html">Dijit Form Reference Guide</a></li>
<li><a href="http://www.dojotoolkit.org/api/dojox/validate.html">dojox.validate Reference</a></li>
</ul>
<p>The Dijit library is much more than just gloss on your elements &#8212; it&#8217;s a hugely functional set of classes to make life easier for both you, the developer, and your users.</p>


<p>Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/07/12/dive-into-dijit/' rel='bookmark' title='Permanent Link: Dive Into Dijit'>Dive Into Dijit</a></li><li><a href='http://www.sitepen.com/blog/2009/11/05/dijit-prepackaged/' rel='bookmark' title='Permanent Link: Dijit: Prepackaged'>Dijit: Prepackaged</a></li><li><a href='http://www.sitepen.com/blog/2009/02/25/styling-dijit-form-elements/' rel='bookmark' title='Permanent Link: Styling Dijit Form Elements'>Styling Dijit Form Elements</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/08/11/dive-into-dijit-forms/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<series:name><![CDATA[Dive Into Dojo]]></series:name>
	</item>
		<item>
		<title>Dive Into Dojo Chart Theming</title>
		<link>http://www.sitepen.com/blog/2010/07/26/dojo-chart-theming/</link>
		<comments>http://www.sitepen.com/blog/2010/07/26/dojo-chart-theming/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 19:31:36 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[UI Design]]></category>
		<category><![CDATA[Vector Graphics]]></category>
		<category><![CDATA[charting]]></category>
		<category><![CDATA[gfx]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1611</guid>
		<description><![CDATA[The previous installment of the Dive Into Dojo series shows how easy it is to Dive Into Dojo Charting to get started with Dojo&#8217;s charting library. It comes with dozens of stylish themes you can effortlessly plug into any chart. But what if you want your charts to match your website&#8217;s design or business&#8217; branding? [...]


Related posts:<ol><li><a href='http://www.sitepen.com/blog/2008/06/16/a-beginners-guide-to-dojo-charting-part-2-of-2/' rel='bookmark' title='Permanent Link: A Beginner&#8217;s Guide to Dojo Charting, Part 2 of 2'>A Beginner&#8217;s Guide to Dojo Charting, Part 2 of 2</a></li><li><a href='http://www.sitepen.com/blog/2008/05/15/zooming-scrolling-and-panning-in-dojo-charting/' rel='bookmark' title='Permanent Link: Zooming, Scrolling, and Panning in Dojo Charting'>Zooming, Scrolling, and Panning in Dojo Charting</a></li><li><a href='http://www.sitepen.com/blog/2010/07/13/dive-into-dojo-charting/' rel='bookmark' title='Permanent Link: Dive Into Dojo Charting'>Dive Into Dojo Charting</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>The previous installment of the Dive Into Dojo series shows how easy it is to <a href="http://www.sitepen.com/blog/2010/07/13/dive-into-dojo-charting/">Dive Into Dojo Charting</a> to get started with Dojo&#8217;s charting library. It comes with dozens of stylish themes you can effortlessly plug into any chart. But what if you want your charts to match your website&#8217;s design or business&#8217; branding? No worries: Dojo&#8217;s charting library allows you to create custom themes!</p>
<p><a href="http://download.dojotoolkit.org/release-1.5.0/dojo-release-1.5.0/dojox/charting/tests/theme_preview.html"><img src="http://davidwalsh.name/dw-content/dojo-chart-creation1.jpg" alt="Dojo Chart Themes" /></a></p>
<p><span id="more-1611"></span></p>
<h2>Setting Up Your Namespace</h2>
<p>
	All of Dojo&#8217;s charting themes live with in the <code>dojox.charting.themes</code> namespace. As is the best practice with custom class creation with Dojo, we&#8217;ll create our own namespace for our custom chart themes.  Let&#8217;s give our custom theme the <code>davidwalsh.charting.themes</code> namespace.  <code>davidwalsh</code> serves as my namespace for all custom Dojo classes and I&#8217;ve chosen to mimic the path dojox uses to themes.
</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="coMULTI">/* declaring charts within my namespace */</span><br />
dojo.<span class="me1">provide</span><span class="br0">&#40;</span><span class="st0">&quot;davidwalsh.charting.themes.SitePen&quot;</span><span class="br0">&#41;</span>;<br />
dojo.<span class="me1">provide</span><span class="br0">&#40;</span><span class="st0">&quot;davidwalsh.charting.themes.SitePenFTW&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<h2>Quick Peek at a Simple Theme</h2>
<p>The levels of complexity within Dojo themes can vary greatly depending on your desire to use simply define colors or implement advanced features like gradations, markers, scrolling, panning, and chart events. Let&#8217;s start by looking at a basic theme called <code>Dollar</code>:</p>
<div class="dean_ch" style="white-space: wrap;">
dojo.<span class="me1">provide</span><span class="br0">&#40;</span><span class="st0">&quot;dojox.charting.themes.Dollar&quot;</span><span class="br0">&#41;</span>;<br />
dojo.<span class="me1">require</span><span class="br0">&#40;</span><span class="st0">&quot;dojox.charting.Theme&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dojox.<span class="me1">charting</span>.<span class="me1">themes</span>.<span class="me1">Dollar</span> = <span class="kw2">new</span> dojox.<span class="me1">charting</span>.<span class="me1">Theme</span><span class="br0">&#40;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colors: <span class="br0">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;#A4CE67&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;#739363&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;#6B824A&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;#343434&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;#636563&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p><code>Dollar</code> illustrates how simple creating a theme can be; provide a list of colors and you&#8217;ve created a custom theme!</p>
<h2>Basic Custom Theme: SitePen</h2>
<p>Now that we&#8217;ve seen how simple it is to make a basic theme, we can easily create a custom SitePen theme base on the SitePen logo colors:</p>
<div class="dean_ch" style="white-space: wrap;">dojo.<span class="me1">provide</span><span class="br0">&#40;</span><span class="st0">&quot;davidwalsh.charting.themes.SitePen&quot;</span><span class="br0">&#41;</span>;<br />
dojo.<span class="me1">require</span><span class="br0">&#40;</span><span class="st0">&quot;dojox.charting.Theme&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; davidwalsh.<span class="me1">charting</span>.<span class="me1">themes</span>.<span class="me1">SitePen</span> = <span class="kw2">new</span> dojox.<span class="me1">charting</span>.<span class="me1">Theme</span><span class="br0">&#40;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colors: <span class="br0">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;#f2f2f2&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;#bed3d9&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;#7fc25d&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;#60b32b&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;#277085&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;#333&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p>Your <code>colors</code> array can have any number of colors.  Colors are repeated in sequence within the chart, if necessary.  Check out a few different charts using the new SitePen theme:</p>
<p><a href="https://user.sitepen.com/~dwalsh/dojo-charting-custom.html"><img src="http://www.sitepen.com/blog/wp-content/uploads/2010/07/SitePenChart1.jpg" alt="Basic Custom Chart Theme" /></a></p>
<h2>Advanced Chart Theming</h2>
<p>With the basic SitePen chart theme, we simply defined a series of colors we&#8217;d like used in the chart.  With advanced chart theming, you can customize everything from default plotareas, axis, series, and marker colors, fonts, and strokes.  The amount of control you can have over your charts by creating your own theme is truly incredible.  Using Tom Trenka&#8217;s &#8220;Tom&#8221; theme as a template, let&#8217;s further customize and enhance the SitePen theme.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="coMULTI">/* requires and provides */</span><br />
dojo.<span class="me1">provide</span><span class="br0">&#40;</span><span class="st0">&quot;dojox.charting.themes.SitePenFTW&quot;</span><span class="br0">&#41;</span>;<br />
dojo.<span class="me1">require</span><span class="br0">&#40;</span><span class="st0">&quot;dojox.gfx.gradutils&quot;</span><span class="br0">&#41;</span>;<br />
dojo.<span class="me1">require</span><span class="br0">&#40;</span><span class="st0">&quot;dojox.charting.Theme&quot;</span><span class="br0">&#41;</span>;</p>
<p><span class="coMULTI">/* define the theme */</span><br />
<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p><span class="coMULTI">/* create shortcut references to dojox classes */</span><br />
<span class="kw2">var</span> dc = dojox.<span class="me1">charting</span>, themes = dc.<span class="me1">themes</span>, Theme = dc.<span class="me1">Theme</span>, g = Theme.<span class="me1">generateGradient</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* fill settings for gradation */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; defaultFill = <span class="br0">&#123;</span>type: <span class="st0">&quot;linear&quot;</span>, space: <span class="st0">&quot;shape&quot;</span>, x1: <span class="nu0">0</span>, y1: <span class="nu0">0</span>, x2: <span class="nu0">0</span>, y2: <span class="nu0">100</span><span class="br0">&#125;</span>;</p>
<p><span class="coMULTI">/* create theme */</span><br />
davidwalsh.<span class="me1">charting</span>.<span class="me1">themes</span>.<span class="me1">SitePenFTW</span> = <span class="kw2">new</span> dc.<span class="me1">Theme</span><span class="br0">&#40;</span><span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* customize the chart wrapper */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; chart: <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fill: <span class="st0">&quot;#333&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stroke: <span class="br0">&#123;</span> color: <span class="st0">&quot;#333&quot;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageStyle: <span class="br0">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; backgroundColor: <span class="st0">&quot;#000&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: <span class="st0">&quot;#fff&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* plotarea definition */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; plotarea: <span class="br0">&#123;</span> fill: <span class="st0">&quot;#000&quot;</span> <span class="br0">&#125;</span>,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* axis definition */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; axis:<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stroke:&nbsp;<span class="br0">&#123;</span> <span class="co1">// the axis itself</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: <span class="st0">&quot;#fff&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: <span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tick: <span class="br0">&#123;</span>&nbsp;<span class="co1">// used as a foundation for all ticks</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: <span class="st0">&quot;#fff&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position: <span class="st0">&quot;center&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font: <span class="st0">&quot;normal normal normal 7pt Helvetica, Arial, sans-serif&quot;</span>,&nbsp; <span class="co1">// labels on axis</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fontColor: <span class="st0">&quot;#fff&quot;</span> <span class="co1">// color of labels</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* series definition */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; series: <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stroke: <span class="br0">&#123;</span> width: <span class="nu0">2.5</span>, color: <span class="st0">&quot;#fff&quot;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outline: <span class="kw2">null</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font: <span class="st0">&quot;normal normal normal 8pt Helvetica, Arial, sans-serif&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fontColor: <span class="st0">&quot;#fff&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* marker definition */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; marker: <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stroke: <span class="br0">&#123;</span> width: <span class="nu0">1.25</span>, color: <span class="st0">&quot;#fff&quot;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outline: <span class="br0">&#123;</span> width: <span class="nu0">1.25</span>, color: <span class="st0">&quot;#fff&quot;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font: <span class="st0">&quot;normal normal normal 8pt Helvetica, Arial, sans-serif&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fontColor: <span class="st0">&quot;#fff&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* series theme with gradations! */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//light =&gt; dark</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//from above: &nbsp; g = dojox.charting.Theme.generateGradient</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//defaultFill object holds all of our gradation settings</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; seriesThemes: <span class="br0">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> fill: g<span class="br0">&#40;</span>defaultFill, <span class="st0">&quot;#fff&quot;</span>, <span class="st0">&quot;#f2f2f2&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> fill: g<span class="br0">&#40;</span>defaultFill, <span class="st0">&quot;#d5ecf3&quot;</span>, <span class="st0">&quot;#bed3d9&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> fill: g<span class="br0">&#40;</span>defaultFill, <span class="st0">&quot;#9ff275&quot;</span>, <span class="st0">&quot;#7fc25d&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> fill: g<span class="br0">&#40;</span>defaultFill, <span class="st0">&quot;#81ee3b&quot;</span>, <span class="st0">&quot;#60b32b&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> fill: g<span class="br0">&#40;</span>defaultFill, <span class="st0">&quot;#4dcff4&quot;</span>, <span class="st0">&quot;#277085&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> fill: g<span class="br0">&#40;</span>defaultFill, <span class="st0">&quot;#666&quot;</span>, <span class="st0">&quot;#333&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#93;</span>,&nbsp; &nbsp; &nbsp; </p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* marker theme */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; markerThemes: <span class="br0">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>fill: <span class="st0">&quot;#bf9e0a&quot;</span>, stroke: <span class="br0">&#123;</span>color: <span class="st0">&quot;#ecc20c&quot;</span><span class="br0">&#125;</span><span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>fill: <span class="st0">&quot;#73b086&quot;</span>, stroke: <span class="br0">&#123;</span>color: <span class="st0">&quot;#95e5af&quot;</span><span class="br0">&#125;</span><span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>fill: <span class="st0">&quot;#216071&quot;</span>, stroke: <span class="br0">&#123;</span>color: <span class="st0">&quot;#277084&quot;</span><span class="br0">&#125;</span><span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>fill: <span class="st0">&quot;#c7212d&quot;</span>, stroke: <span class="br0">&#123;</span>color: <span class="st0">&quot;#ed2835&quot;</span><span class="br0">&#125;</span><span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>fill: <span class="st0">&quot;#87ab41&quot;</span>, stroke: <span class="br0">&#123;</span>color: <span class="st0">&quot;#b6e557&quot;</span><span class="br0">&#125;</span><span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#93;</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p>You wont need to define every property created above;  if a given property isn&#8217;t defined, a default value will be used.  On the flip side, you may also override each and any of these settings when creating chart instances.  This custom theme acts as a middle-ground between chart defaults and chart instance settings.</p>
<p>Take a look at some of our enhanced theme examples:</p>
<p><a href="https://user.sitepen.com/~dwalsh/dojo-charting-custom.html"><img src="http://www.sitepen.com/blog/wp-content/uploads/2010/07/SitePenChart2.jpg" alt="Advanced Custom Chart Theme" /></a></p>
<h2>Using Gradients</h2>
<p>The advanced SitePen theme we created above made use of gradients thanks to the <code>dojox.gfx.gradutils</code> class and <code>dojox.charting.Theme.generateGradient</code> method introduced in Dojo 1.5.  Gradation instances can be passed to any property that desires a color (usually the <code>fill</code> property.)  The signature of the new <code>generateGradient</code> method is:</p>
<div class="dean_ch" style="white-space: wrap;">
generateGradient: <span class="kw2">function</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fillPattern, <br />
&nbsp; &nbsp; &nbsp; &nbsp; colorFrom, <br />
&nbsp; &nbsp; &nbsp; &nbsp; colorTo<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>The fill pattern holds the gradient settings:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; type: <span class="st0">&quot;linear&quot;</span>,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//or &quot;radial&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; space: <span class="st0">&quot;shape&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; x1: <span class="nu0">0</span>, &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//gradation direction</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; y1: <span class="nu0">0</span>, &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//gradation direction</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; x2: <span class="nu0">0</span>, &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//gradation direction</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; y2: <span class="nu0">100</span>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//gradation direction</span><br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Feel free to experiment with the gradation settings to find just the right gradient for you.</p>
<h2>New to Dojo 1.5: Gradients, Themes, and Extended Support!</h2>
<p>Dojo 1.5 introduces great new features to the charting library, including:</p>
<ul>
<li><a href="http://download.dojotoolkit.org/release-1.5.0/dojo-release-1.5.0/dojox/charting/tests/theme_preview.html">New Themes</a></li>
<li><a href="http://download.dojotoolkit.org/release-1.5.0/dojo-release-1.5.0/dojox/charting/tests/theme_preview.html">Gradients</a></li>
<li>Experimental support for the <a href="http://code.google.com/p/svgweb/">SVGWeb plugin</a> in addition to the existing support for SVG, VML, Canvas, Flash, and Silverlight.</li>
</ul>
<p></p>
<h2>Great Charting Resources</h2>
<ul>
<li><a href="http://www.sitepen.com/blog/2010/07/13/dive-into-dojo-charting/">Dive Into Dojo Charting</a></li>
<li><a href="http://www.dojotoolkit.org/reference-guide/dojox/charting.html#dojox-charting">Dojox Charting Reference Guide</a></li>
<li><a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/charting/tests/">Nightly Tests</a></li>
<li><a href="http://download.dojotoolkit.org/release-1.5.0/dojo-release-1.5.0/dojox/charting/tests/theme_preview.html">Theme Preview</a></li>
<li><a href="http://www.sitepen.com/blog/2009/03/30/introducing-dojox-datachart/">Introducing Dojox Data Chart</a></li>
<li><a href="http://www.sitepen.com/blog/2008/06/06/a-beginners-guide-to-dojo-charting-part-1-of-2/">A Beginner’s Guide to Dojo Charting, Part 1 of 2</a></li>
<li><a href="http://www.sitepen.com/blog/2008/06/16/a-beginners-guide-to-dojo-charting-part-2-of-2/">A Beginner’s Guide to Dojo Charting, Part 2 of 2</a></li>
<li><a href="http://www.sitepen.com/blog/2008/05/15/zooming-scrolling-and-panning-in-dojo-charting/">Zooming, Scrolling, and Panning in Dojo Charting</a></li>
</ul>
<p></p>
<h2>Create Your Theme!</h2>
<p>While Dojo&#8217;s charting library comes with a variety of stylish themes, don&#8217;t feel as though you need to choose an existing theme;  take a few minutes to create an eye-catching custom theme to match your branding!</p>


<p>Related posts:<ol><li><a href='http://www.sitepen.com/blog/2008/06/16/a-beginners-guide-to-dojo-charting-part-2-of-2/' rel='bookmark' title='Permanent Link: A Beginner&#8217;s Guide to Dojo Charting, Part 2 of 2'>A Beginner&#8217;s Guide to Dojo Charting, Part 2 of 2</a></li><li><a href='http://www.sitepen.com/blog/2008/05/15/zooming-scrolling-and-panning-in-dojo-charting/' rel='bookmark' title='Permanent Link: Zooming, Scrolling, and Panning in Dojo Charting'>Zooming, Scrolling, and Panning in Dojo Charting</a></li><li><a href='http://www.sitepen.com/blog/2010/07/13/dive-into-dojo-charting/' rel='bookmark' title='Permanent Link: Dive Into Dojo Charting'>Dive Into Dojo Charting</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/07/26/dojo-chart-theming/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<series:name><![CDATA[Dive Into Dojo]]></series:name>
	</item>
		<item>
		<title>Dojo 1.5: Ready to power your web app</title>
		<link>http://www.sitepen.com/blog/2010/07/22/dojo-1-5-ready-to-power-your-web-app/</link>
		<comments>http://www.sitepen.com/blog/2010/07/22/dojo-1-5-ready-to-power-your-web-app/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 16:00:35 +0000</pubDate>
		<dc:creator>Dylan</dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[dojo15]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1474</guid>
		<description><![CDATA[Dojo Toolkit 1.5 is now available for immediate download. Dojo is a JavaScript toolkit that is lean enough for use on a simple blog, yet powerful enough to scale to solve the most advanced web application engineering challenges, allowing you to use just the features and flexibility needed for your application. The 11th major Dojo [...]


Related posts:<ol><li><a href='http://www.sitepen.com/blog/2009/01/22/platform-optimization-strategies-for-ajax-toolkits/' rel='bookmark' title='Permanent Link: Platform Optimization Strategies for Ajax Toolkits'>Platform Optimization Strategies for Ajax Toolkits</a></li><li><a href='http://www.sitepen.com/blog/2008/02/25/adobe-engages-sitepen-to-make-dojo-toolkit-compatible-with-adobe-air/' rel='bookmark' title='Permanent Link: SitePen Announces Updates to the Dojo Toolkit to Enable Adobe AIR Compatibility'>SitePen Announces Updates to the Dojo Toolkit to Enable Adobe AIR Compatibility</a></li><li><a href='http://www.sitepen.com/blog/2008/12/17/end-of-the-year-dojo-news-and-updates/' rel='bookmark' title='Permanent Link: End of the Year Dojo News and Updates'>End of the Year Dojo News and Updates</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://dojotoolkit.org/download/">Dojo Toolkit 1.5 is now available for immediate download</a>. Dojo is a JavaScript toolkit that is lean enough for use on a simple blog, yet powerful enough to scale to solve the most advanced web application engineering challenges, allowing you to use just the features and flexibility needed for your application. The 11th major Dojo release, version 1.5 offers many important improvements and enhancements and remains as IP-safe, freely-licensed, and free to use as the first release over five years ago.</p>
<p><span id="more-1474"></span></p>
<h2>Substantial user interface improvements</h2>
<p>The new version of Dojo offers substantial user interface improvements in the form of the <a href="http://download.dojotoolkit.org/release-1.5.0/dojo-release-1.5.0/dijit/themes/themeTester.html">beautiful Claro theme</a>. <img src="http://sitepen.com/blog/wp-content/uploads/2010/07/claro_calendar.png" alt="" style="float:right; margin:10px 0 10px 10px;" /> Claro delivers a modern and engaging visual style for rich internet applications with Dojo&#8217;s Dijit library, with the visual enhancements of transparent gradient background images, drop shadows, and appropriate CSS animations (on Webkit and Mozilla-based browsers). It delivers a &#8216;fit and finish&#8217; that surpasses previous themes and releases, and significantly improves ease of customizing the theme CSS, so you can easily create your own theme by styling elements such as padding and color, without needing to design new background images. All of this while being fully accessible and internationalizable across a growing collection of user interface widgets! Learn more in our <a href="http://www.sitepen.com/blog/2010/07/12/dive-into-dijit/">Dive into Dijit</a> article.</p>
<p>The portal layout mechanism found in sites such as the <a href="http://hpp.orange.fr/">personalized Orange home page</a> is provided with Dojo, making it easy to create user-customizable application interfaces. Dojo’s very powerful native vector graphics, <a href="http://www.sitepen.com/blog/2010/07/13/dive-into-dojo-charting/">charting</a>, and <a href="http://www.sitepen.com/services/showcase/asu_andes/">drawing</a> components have received many improvements, including <a href="http://download.dojotoolkit.org/release-1.5.0/dojo-release-1.5.0/dojox/charting/tests/theme_preview.html">new themes, gradients</a>, and experimental support for the SVGWeb plugin in addition to the existing support for SVG, VML, Canvas, Flash, and Silverlight. This guarantees that your graphics will work natively when possible, but will work everywhere with the simple dojox.gfx APIs.</p>
<h2>Stable, backwards and forwards compatible Core</h2>
<p><img src="http://sitepen.com/blog/wp-content/uploads/2010/07/core.jpg" alt="" style="float:left; margin:10px 10px 10px 0px;" /><br />
The core Dojo libraries are remarkably stable, making it very easy to upgrade your application from earlier versions of Dojo while receiving a number of improvements to make development easier. <a href="http://doughays.dojotoolkit.org/ImageResizeDemo/">dojo.Stateful</a> was added, and <a href="http://www.sitepen.com/blog/2010/05/03/robust-promises-with-dojo-deferred-1-5/">dojo.Deferred is improved through an underlying Promises-based API</a>. The core libraries provide everything you need for simple and advanced web sites and apps.
</p>
<h2>HTML5 &#038; CSS3</h2>
<p><img src="http://sitepen.com/blog/wp-content/uploads/2010/07/html5.jpg" alt="" style="float:right; margin:10px 0 10px 10px;" /> Dojo supports many HTML5 features, and has supported many capabilities before they were supported in any browser such as local storage. The Dojo approach is to wrap native support where possible, fixing any glitches, adding API capabilities or simplifications, and offering compatible solutions for older browsers. Whether it’s improved browser history, placeholder hint text for all TextBox-based widgets, a new dojox.style extension to support the CSS transform and transform-origin properties, local storage, <a href="http://jaredj.dojotoolkit.org/editor_dojo15/">rich-text editing</a>, multi-file uploading, or gfx for Canvas/SVG support, we have you covered now, with much more to come in future releases. Check out the <a href="http://www.nicolarizzo.com/gamesroom/demos/presentation15/demo.html">presentation</a> and <a href="http://www.nicolarizzo.com/gamesroom/demos150/Gorillas/">Gorillas</a> examples for a glimpse at what&#8217;s possible!
</p>
<h2>Dojo Mobile</h2>
<p>
<img src="http://sitepen.com/blog/wp-content/uploads/2010/07/mobileDojo.jpg" alt="" style="float:right; margin:10px 0 10px 10px;" /> The mobile app space is evolving far too rapidly for the API stability promised by Dojo’s core libraries, so we have many initiatives underway to solve your mobile app development challenges now, and converge on more stable solutions soon. We strive to address challenges for both mobile web apps, and mobile installed apps that embed a web browser. A number of new Dojo Toolkit and Dojo Foundation initiatives are underway:</p>
<ul>
<li><a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/mobile/">dojox.mobile</a>: lightweight mobile web widgets</li>
<li><a href="http://shaneosullivan.wordpress.com/2010/06/13/dojos-new-mobile-app-project/">dojox.mobile.app</a>: mobile application development framework</li>
<li><a href="http://github.com/uxebu/embedjs">embedjs</a>: Dojo API, optimized for mobile</li>
<li><a href="http://uxebu.com/blog/2010/04/27/touchscroll-a-scrolling-layer-for-webkit-mobile/">TouchScroll</a>: scrolling layer for WebKit Mobile</li>
<li><a href="http://winktoolkit.org/ ">wink</a>: mobile web app toolkit</li>
</ul>
<p></p>
<h2  style="clear:both">Browser Support</h2>
<p>
Dojo supports a wide variety of modern browsers. The complete list of officially supported and tested browsers: Chrome 5, Firefox 3.5 and 3.6; Internet Explorer 6, 7, and 8; Opera 10.6 (Dojo Core only); Safari 4.1 and 5. Other browser versions may work fine with Dojo even if they are not officially supported by the project.
</p>
<h2>Performance and Stability</h2>
<p>Not only is Dojo consistently rated among the best performing Ajax toolkits, we also provide solid tools for scaling and growing your application. It’s easy to run into performance problems with any development tool, but the community and various vendors offer solutions to diagnose and solve all of your performance obstacles.</p>
<h2>Integrations</h2>
<p><img src="http://sitepen.com/blog/wp-content/uploads/2010/07/Eclipse.png" alt="" style="float:left; margin:10px 10px 10px 0px" />It’s always easier to get started when you start with something you already know. Whether you use Zend Framework, Spring, Persevere, Node, Narwhal, cometD-Jetty, DWR, Compuware Uniface, Ruby on Rails, Django, WebSphere, Apache, IIS, or any other HTTP-compliant server-side environment, there’s a simple drop in integration solution to get you up and running quickly. Dojo also offers easy integration with AIR 1.5 and 2.0, Appcelerator Titanium, PhoneGap, and integration hooks with various IDEs and developer tools including Aptana Studio, Komodo, Eclipse, and more.
</p>
<h2>Demos</h2>
<p>We&#8217;re in the process of collating the 150 best Dojo 1.5.0 demos. Some of the initial demos were linked to from within this post, and other community plug-in efforts like the <a href="http://developers.sirika.com/mfu/">MFU</a> project offers an alternative to the dojox.form.FileUploader widget, or the <a href="http://gist.github.com/474401">Dojo port of mustache.js</a> as an alternative to dojox.dtl.</p>
<h2>Documentation, Support and Assistance</h2>
<p><img src="http://sitepen.com/blog/wp-content/uploads/2010/07/kfm.png" alt="" style="float:right; margin:10px 0 10px 10px" />The <a href="http://dojotoolkit.org/">Dojo web site</a> has a plethora of <a href="http://dojotoolkit.org/documentation/">documentation</a>, and the thriving <a href="http://dojotoolkit.org/community/">community</a> offers assistance for everything that&#8217;s not already covered. For professional support and assistance, companies like <a href="http://sitepen.com/">SitePen</a> are available to help you get the most out of Dojo and the open web!</p>
<p><a href="http://dojotoolkit.org/download/">Download Dojo 1.5</a> today and let us know what you think, and tell us about the great apps you&#8217;re building with Dojo!</a></p>
<h2>A Dojo Foundation project</h2>
<p>
The Dojo Toolkit is part of the <a href="http://dojofoundation.org/">Dojo Foundation</a>, the open home for the open web. Beyond the Dojo Toolkit, the foundation has welcomed 3 new projects recently: <a href="http://www.zazl.org/">Zazl</a>, <a href="http://animej.codeplex.com/">AnimeJ</a>, and <a href="http://winktoolkit.org/">wink</a>. <a href="http://cometd.org/">cometD-Jetty</a> recently announced a 2.0 release, and <a href="http://www.persvr.org/">Persevere</a> 2.0 beta is due this summer.</p>


<p>Related posts:<ol><li><a href='http://www.sitepen.com/blog/2009/01/22/platform-optimization-strategies-for-ajax-toolkits/' rel='bookmark' title='Permanent Link: Platform Optimization Strategies for Ajax Toolkits'>Platform Optimization Strategies for Ajax Toolkits</a></li><li><a href='http://www.sitepen.com/blog/2008/02/25/adobe-engages-sitepen-to-make-dojo-toolkit-compatible-with-adobe-air/' rel='bookmark' title='Permanent Link: SitePen Announces Updates to the Dojo Toolkit to Enable Adobe AIR Compatibility'>SitePen Announces Updates to the Dojo Toolkit to Enable Adobe AIR Compatibility</a></li><li><a href='http://www.sitepen.com/blog/2008/12/17/end-of-the-year-dojo-news-and-updates/' rel='bookmark' title='Permanent Link: End of the Year Dojo News and Updates'>End of the Year Dojo News and Updates</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/07/22/dojo-1-5-ready-to-power-your-web-app/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Real-time Comet Applications on Node with Tunguska</title>
		<link>http://www.sitepen.com/blog/2010/07/19/real-time-comet-applications-on-node-with-tunguska/</link>
		<comments>http://www.sitepen.com/blog/2010/07/19/real-time-comet-applications-on-node-with-tunguska/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 11:34:39 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[CommonJS]]></category>
		<category><![CDATA[Persevere]]></category>
		<category><![CDATA[comet]]></category>
		<category><![CDATA[jack]]></category>
		<category><![CDATA[narwhal]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[tunguska]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1289</guid>
		<description><![CDATA[Node is a server-side JavaScript platform that is known for being well suited for Comet-style applications that utilize long-lived connections to allow applications to send messages from the server to the browser asynchronously. In fact, the beginning of the front page of nodejs.org starts out with an example of a web application that delays for [...]


Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/07/14/multi-node-concurrent-nodejs-http-server/' rel='bookmark' title='Permanent Link: Multi-node: Concurrent NodeJS HTTP Server'>Multi-node: Concurrent NodeJS HTTP Server</a></li><li><a href='http://www.sitepen.com/blog/2008/03/27/which-comet-implementation/' rel='bookmark' title='Permanent Link: Which Comet Implementation?'>Which Comet Implementation?</a></li><li><a href='http://www.sitepen.com/blog/2010/06/11/jsgi-vs-connect-for-node-middleware/' rel='bookmark' title='Permanent Link: JSGI vs Connect for Node Middleware'>JSGI vs Connect for Node Middleware</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Node is a server-side JavaScript platform that is known for being well suited for Comet-style applications that utilize long-lived connections to allow applications to send messages from the server to the browser asynchronously. In fact, the beginning of the front page of <a href="http://nodejs.org/">nodejs.org</a> starts out with an example of a web application that delays for a couple seconds before sending a response without any type of blocking; the code is asynchronous and efficient.</p>
<p>However, building a real-life real-time application involves more than just a platform that gives you asynchronous communication, there are a number of other important techniques to understand. We will look at these techniques and introduce <a href="http://github.com/kriszyp/tunguska">project Tunguska</a> that provides some helpful tools to assist in building applications. While a number of Comet projects out there attempt to provide a black box solution to Comet, Tunguska recognizes that most real-time applications involve deep integration into the application and its security, messaging, and data structures. Consequently Tunguska is a set of tools for building real-time applications rather than a closed black box that can&#8217;t easily be integrated with. Let&#8217;s look at some of these tools.</p>
<p><span id="more-1289"></span></p>
<h2>Connections/Message Queues</h2>
<p>The most fundamental concept of building real-time applications is providing the ability to asynchronously deliver messages that occur at any time, to the browser. With new technologies like WebSockets, this is relatively straightforward. You can create a connection to the server and then the server is free to send messages to the browser at any time. However, WebSocket capable browsers are still nowhere near widely distributed enough to rely on (and the WebSocket protocol is still in development, a recent revision rendered the first version incompatible, and more changes are likely to occur due to <a href="http://blogs.webtide.com/gregw/entry/websocket_chat">the immature design</a>).</p>
<p>The most widely used technique for Comet is known as <a href="http://cometdaily.com/2007/11/16/more-on-long-polling/">long-polling</a>. With long-polling an HTTP request is made to the server which waits until it has a message to send. The message is sent to the browser as the response, and upon receiving the response the client makes another request to the server. However, the complication of this strategy is that the server does not have a continuous, uninterrupted stream to send messages to the client. There is a time gap between each message/response being sent from the server and the next long-polling request from the browser reaching the server.</p>
<p>However, we obviously don&#8217;t want to miss the messages that were generated during these time gaps. Therefore we need to provide a virtual connection by creating a queue where messages can be stored during these time gaps between the response and request. These message queues allow us to effectively emulate a continuous connection, whereby messages can be delivered at any time to the virtual connection. If an active long-polling response is waiting the server can immediately deliver the message, otherwise the message can go into a queue to wait for the next request from the browser to send a message.</p>
<p>Tunguska provides this type of virtual connection or message queueing with its <strong>queue</strong> module, and message queues can be accessed with the <code>getQueue(clientId)</code> function. Queues are identified by a client id. This function will return a queue object for a given client id passed as the argument. The queue object extends JavaScript&#8217;s Array (providing array functions for accessing queued events), provides a send() for delivering events, a <code>message</code> event, and a <code>close</code> event (accessible via addListener() or observe()) as the key functionality.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> getQueue = require<span class="br0">&#40;</span><span class="st0">&quot;tunguska/queue&quot;</span><span class="br0">&#41;</span>.<span class="me1">getQueue</span>;<br />
<span class="co1">// get the virtual connection for this request</span><br />
<span class="kw2">var</span> queue = getQueue<span class="br0">&#40;</span><span class="st0">&quot;32a3fa5&quot;</span><span class="br0">&#41;</span>;<br />
queue.<span class="me1">addListener</span><span class="br0">&#40;</span><span class="st0">&quot;message&quot;</span>, <span class="kw2">function</span><span class="br0">&#40;</span>message<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="co1">// this will be fired by the send call below</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
<span class="co1">// put a message in the queue identified by this request.</span><br />
queue.<span class="me1">send</span><span class="br0">&#40;</span><span class="st0">&quot;Hello&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>The queue module does more than just provide a queue object, it also handles timeouts of non-active connections so as to avoid unbounded memory usage, or memory leakage due to accumulation of unused connection objects. It also handles distributed message queues (across multiple processes/machines) as we will see later.</p>
<p>Tunguska also provides a &#8220;jsgi/comet&#8221; module for the higher HTTP level interaction. A message queue, which acts as a virtual connection to a client, can be obtained from a request object (<a href="http://www.sitepen.com/blog/2010/01/19/commonjsjsgi-the-emerging-javascript-application-server-platform/">JSGI</a> or Node) passed into its getClientConnection(request) function. This function will look for the &#8220;Client-Id&#8221; header to use as the client id for connection identification. If a connection exists with the given client id, it will be returned, otherwise a new connection object/message queue will be created and returned.</p>
<p>The connection object is based on the WebSocket API. Sending a message to the client is as simple as calling the <code>send(message)</code> function. The connection object is also an array. New messages are pushed onto the array and any listeners are notified.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> comet = require<span class="br0">&#40;</span><span class="st0">&quot;tunguska/jsgi/comet&quot;</span><span class="br0">&#41;</span>;<br />
<span class="co1">// get the virtual connection for this request</span><br />
<span class="kw2">var</span> connection = comet.<span class="me1">getClientConnection</span><span class="br0">&#40;</span>request<span class="br0">&#41;</span>;<br />
<span class="co1">// put a message in the queue identified by this request.</span><br />
connection.<span class="me1">send</span><span class="br0">&#40;</span><span class="br0">&#123;</span>from:<span class="st0">&quot;Kris&quot;</span>, body:<span class="st0">&quot;hi&quot;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<h2>Long-polling handler</h2>
<p>A long-polling request handler can now listen for messages by initially checking for any messages in the queue for instant delivery and if no messages are ready to deliver to the client, it can listen for <code>message</code> events to trigger delivery. A JSGI middleware appliance is provided with the <code>jsgi/comet</code> module to do just that. We can easily insert this appliance into a middleware stack:</p>
<div class="dean_ch" style="white-space: wrap;">
cometApp = comet.<span class="me1">Broadcaster</span><span class="br0">&#40;</span>nextApp<span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>The optional <code>nextApp</code> parameter can be used to define another middleware appliance that can handle any needed subscriptions and/or other wiring to event triggers to relay messages to the virtual connection object. For example:</p>
<div class="dean_ch" style="white-space: wrap;">
cometApp = comet.<span class="me1">Broadcaster</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>request<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; <span class="kw2">var</span> connection = comet.<span class="me1">getClientConnection</span><span class="br0">&#40;</span>request<span class="br0">&#41;</span>;<br />
&nbsp; <span class="co1">// user function for registering a user to get user events</span><br />
&nbsp; listenForMessagesForUser<span class="br0">&#40;</span><br />
&nbsp; &nbsp; request.<span class="me1">remoteUser</span>, <span class="co1">// if we have an authenticated user</span><br />
&nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span>message<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="co1">// this can be called when a message needs to be sent to the user</span><br />
&nbsp; &nbsp; &nbsp; connection.<span class="me1">send</span><span class="br0">&#40;</span>JSON.<span class="me1">stringify</span><span class="br0">&#40;</span>message<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<h2>Message Routing</h2>
<p>One of the key aspects of real-time applications is message routing. When an action or event occurs, the message needs to be routed to the appropriate party. In a chat application, when someone sends a chat message, we need to route and send the message to the recipient. A frequently used mechanism for this is a <a href="http://cometdaily.com/2007/10/26/pubsub-and-topics/">publish/subscribe</a> hub. A hub consists of &#8220;channels&#8221; or &#8220;topics&#8221;, and messages can be published to channels on the hub, and subscribers can listen for messages on these channels. This paradigm decouples sending messages and listening for messages. Message publishers do not need to be concerned with who is subscribed and subscribers do not need to be concerned with the details of who and how messages are published.</p>
<h3>Pub/Sub Hub</h3>
<p>There are certainly alternate approaches to topic-based pub/sub hubs for message routing. Rather than subscribing to a particular named topic, subscribers could provide message filter functions that filter through all messages for appropriate messages of interests. However, this approach does not scale well. As more diverse topics and messages are being sent through a system, a filter often begins to process more non-matching functions and performance suffers. On the other hand, pub/sub hubs scale well. A normal hash-table based hub maintains constant time message routing regardless of the number of topics. This highly scalable approach fits well with the Node philosophy of making it easy to do the right thing (in terms of scalability) and hard to do the wrong thing. By using the named topics, it is very easy to create highly scalable message routing systems.</p>
<p>Creating a very simple pub/sub hub in JavaScript is actually extremely easy. JavaScript&#8217;s hash-based objects make a great core of a hub. You can simply create properties on a &#8220;hub&#8221; object with names that correspond to topics and values that are arrays of listeners. However, more interesting features than simple topic based message routing are often desirable. Tunguska provides a &#8220;hub&#8221; module with a rich set of publish/subscribe capabilities.</p>
<p>The Tunguska hub supports globbing or wildcard-based subscriptions for listening for a set of topics (rather than only being able to register for a single topic at once).</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> hub = require<span class="br0">&#40;</span><span class="st0">&quot;tunguska/hub&quot;</span><span class="br0">&#41;</span>;<br />
hub.<span class="me1">subscribe</span><span class="br0">&#40;</span><span class="st0">&quot;foo/*&quot;</span>, <span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// will be fired by the publish below</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
hub.<span class="me1">publish</span><span class="br0">&#40;</span><span class="st0">&quot;foo/bar&quot;</span>, <span class="st0">&quot;hi&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>It allows for &#8220;typed&#8221; messages to allow for filtering/routing by type as well as topic (essentially allowing a hub to have a two-dimensional namespace). This hub uses &#8220;typed&#8221; messages for broadcasting subscription events, sending out a &#8220;monitored&#8221; event when a topic starts to have subscribers or ceases to have any subscribers.</p>
<div class="dean_ch" style="white-space: wrap;">
hub.<span class="me1">subscribe</span><span class="br0">&#40;</span><span class="st0">&quot;foo/*&quot;</span>, <span class="st0">&quot;monitored&quot;</span>, <span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// will be fired by the subscribe action below</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
hub.<span class="me1">subscribe</span><span class="br0">&#40;</span><span class="st0">&quot;foo/bar&quot;</span>, <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>&#8230;<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Tunguska&#8217;s hub also supports echo suppression, allowing subscribers and publishers to indicate a client identifier so that published messages do not echo back to the client. These components are important for various applications and are essential for Tunguska&#8217;s distributed messaging system, where hubs can be linked together in a cluster.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> clientHub = hub.<span class="me1">fromClient</span><span class="br0">&#40;</span><span class="st0">&quot;42a92f3&quot;</span><span class="br0">&#41;</span>;<br />
clientHub.<span class="me1">subscribe</span><span class="br0">&#40;</span><span class="st0">&quot;foo/bar&quot;</span>, <span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// will be *not* fired by the publish below</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
clientHub.<span class="me1">publish</span><span class="br0">&#40;</span><span class="st0">&quot;foo/bar&quot;</span>, <span class="st0">&quot;for everyone else&quot;</span><span class="br0">&#41;</span>;</div>
<h2>Clustering</h2>
<p>Building Comet applications on a single Node process (single threaded) can be deceptively easy. But, scaling Comet applications to multiple concurrent processes is where the bulk of complexity usually arises. Inter-process communication, non-deterministic request delegation, message framing, and true parallel concurrency can combine to create a formidable challenge compared to the rather simple single-process application development. Fortunately, Tunguska provides clustering support for linking distributed pub/sub hubs together, allowing applications to maintain a simple hub-based message routing system without having to worry about the inter-process interaction complexity.</p>
<p>The core component for building Tunguska clusters is the &#8220;connector&#8221; module. The &#8220;connector&#8221; module provides a means of using connections to other processes to coherently link hubs together. These connectors will listen for subscriptions and send subscription requests to other processes, allowing for collection of all messages for a given topic from all processes. A connector can be created by passing it a connection object (that follows the WebSocket API), and the connector will handle the messaging concerns for propagating subscriptions and message routing.</p>
<p><a href="http://www.sitepen.com/blog/wp-content/uploads/2010/06/Tunguska.png"><img class="size-full wp-image-1334" title="Tunguska" src="http://www.sitepen.com/blog/wp-content/uploads/2010/06/Tunguska.png" alt="Tunguska" /></a><br />
WebSocket connection objects can easily be utilized with actual WebSocket TCP connections to other machines. For inter-process communication on a single machine, <a href="http://www.sitepen.com/blog/2010/07/14/multi-node-concurrent-nodejs-http-server/">multi-node</a> provides easy access to a WebSocket-based connection to the sibling HTTP serving processes. Connecting a set of processes with Tunguska with multi-node is very simple:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> multiNode = require<span class="br0">&#40;</span><span class="st0">&quot;multi-node/multi-node&quot;</span><span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; Connector = require<span class="br0">&#40;</span><span class="st0">&quot;tunguska/connector&quot;</span><span class="br0">&#41;</span>.<span class="me1">Connector</span>;<br />
<span class="co1">// start the multiple processes</span><br />
<span class="kw2">var</span> nodes = multiNode.<span class="me1">listen</span><span class="br0">&#40;</span><span class="br0">&#123;</span>port: <span class="nu0">80</span>, nodes: <span class="nu0">4</span><span class="br0">&#125;</span>, serverObject<span class="br0">&#41;</span>;</p>
<p><span class="co1">// add a listener for each connection to the other sibling process</span><br />
nodes.<span class="me1">addListener</span><span class="br0">&#40;</span><span class="st0">&quot;node&quot;</span>, <span class="kw2">function</span><span class="br0">&#40;</span>stream<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// create a new connector using the framed WS stream</span><br />
&nbsp; Connector<span class="br0">&#40;</span><span class="st0">&quot;local-workers&quot;</span>, multiNode.<span class="me1">frameStream</span><span class="br0">&#40;</span>stream<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<h2>Jack/JSGI Support on Other Platforms</h2>
<p>In addition to Node, Tunguska can run on any other JSGI-compliant server including <a href="http://github.com/kriszyp/jack">Jack 0.3</a>, and should be easily adaptable to RingoJS as well. The main complication with different platforms is creating connectors with alternate forms of cross-process or cross-thread communication. Jack 0.3 utilizes threads for concurrency that implement the WebWorker API and includes special events for connecting to other workers for fully connected workers (achieving fully connected workers, where every worker can talk to every other worker is problematic with the standard worker API since it relies on the less ubiquitous SharedWorker API and requires a high level of name coordination). To use the Tunguska connectors with Jack, you utilize the &#8220;jack-connector&#8221; module, which provides &#8220;worker&#8221; events that return connection streams that can be used with the connectors:</p>
<div class="dean_ch" style="white-space: wrap;">
require<span class="br0">&#40;</span><span class="st0">&quot;tunguska/jack-connector&quot;</span><span class="br0">&#41;</span>.<span class="me1">addListener</span><span class="br0">&#40;</span><span class="st0">&quot;worker&quot;</span>, <span class="kw2">function</span><span class="br0">&#40;</span>connection<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; Connector<span class="br0">&#40;</span><span class="st0">&quot;local-workers&quot;</span>, connection<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Tunguska provides a solid set of tools for building scalable, non-blocking, real-time applications with the flexibility and modularity needed to integrate with essential application logic including custom serialization, authorization/access-control, and middleware routing to truly leverage the asynchronous I/O capabilities of Node. Next up, we will look at how Tunguska, which is a sub-project of Persevere, fits into the greater Persevere ecosystem for data integration with real-time data views, and RESTful content negotiation.</p>


<p>Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/07/14/multi-node-concurrent-nodejs-http-server/' rel='bookmark' title='Permanent Link: Multi-node: Concurrent NodeJS HTTP Server'>Multi-node: Concurrent NodeJS HTTP Server</a></li><li><a href='http://www.sitepen.com/blog/2008/03/27/which-comet-implementation/' rel='bookmark' title='Permanent Link: Which Comet Implementation?'>Which Comet Implementation?</a></li><li><a href='http://www.sitepen.com/blog/2010/06/11/jsgi-vs-connect-for-node-middleware/' rel='bookmark' title='Permanent Link: JSGI vs Connect for Node Middleware'>JSGI vs Connect for Node Middleware</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/07/19/real-time-comet-applications-on-node-with-tunguska/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<series:name><![CDATA[Server-Side JavaScript, Pintura, and Persevere 2.0]]></series:name>
	</item>
		<item>
		<title>Asynchronous CommonJS Modules for the Browser (and Introducing Transporter)</title>
		<link>http://www.sitepen.com/blog/2010/07/16/asynchronous-commonjs-modules-for-the-browser-and-introducing-transporter/</link>
		<comments>http://www.sitepen.com/blog/2010/07/16/asynchronous-commonjs-modules-for-the-browser-and-introducing-transporter/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 07:15:24 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[CommonJS]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[Persevere]]></category>
		<category><![CDATA[requiresjs]]></category>
		<category><![CDATA[transporter]]></category>
		<category><![CDATA[yabble]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1203</guid>
		<description><![CDATA[Modules are an integral architectural piece of robust application development since they allow individual components to be developed with proper dependency management. Modules can specify dependencies and these can be automatically resolved and loaded to bring various pieces together automatically. In application development this is vastly more scalable and easier than having to track all [...]


Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/03/04/pintura-jsgi-modules/' rel='bookmark' title='Permanent Link: Pintura JSGI Modules'>Pintura JSGI Modules</a></li><li><a href='http://www.sitepen.com/blog/2010/01/19/commonjsjsgi-the-emerging-javascript-application-server-platform/' rel='bookmark' title='Permanent Link: CommonJS/JSGI: The Emerging JavaScript Application Server Platform'>CommonJS/JSGI: The Emerging JavaScript Application Server Platform</a></li><li><a href='http://www.sitepen.com/blog/2010/01/22/introducing-pintura/' rel='bookmark' title='Permanent Link: Introducing Pintura'>Introducing Pintura</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Modules are an integral architectural piece of robust application development since they allow individual components to be developed with proper dependency management. Modules can specify dependencies and these can be automatically resolved and loaded to bring various pieces together automatically. In application development this is vastly more scalable and easier than having to track all the different dependencies and manually load modules or insert script tags.</p>
<p>The CommonJS module format is increasingly ubiquitous as the de facto module format for JavaScript. However, if CommonJS modules, by themselves, are directly executed, they require synchronous loading of modules. Synchronous loading is known to be very problematic in the browser since it locks the browser user interface, requires eval-based compilation of scripts which confuses debuggers, and is less efficient than using standard script tags.</p>
<p><span id="more-1203"></span></p>
<p>The CommonJS group has developed a specification for callback based module loading called the <a href="http://wiki.commonjs.org/wiki/Modules/Transport">module transport format</a>. The CommonJS module transport format is specifically designed to make it easy to wrap CommonJS modules (which also use synchronous <code>require</code> calls) with a callback wrapper to allow for asynchronous loading on the client. For example, if one had a CommonJS module:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">// complex-numbers/plus-two.js:</span><br />
<span class="kw2">var</span> sum = require<span class="br0">&#40;</span><span class="st0">&quot;./math&quot;</span><span class="br0">&#41;</span>.<span class="me1">sum</span>;<br />
exports.<span class="me1">plusTwo</span> = <span class="kw2">function</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; <span class="kw1">return</span> sum<span class="br0">&#40;</span>a, <span class="nu0">2</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>This can be wrapped with the transport format to be loaded asynchronously and used on the client:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">// complex-numbers/plus-two.js received by browser:</span><br />
require.<span class="me1">define</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="st0">&quot;complex-numbers/plus-two&quot;</span>: <span class="kw2">function</span><span class="br0">&#40;</span>require, exports<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
<span class="co1">// define body in callback</span><br />
&nbsp; <span class="kw2">var</span> sum = require<span class="br0">&#40;</span><span class="st0">&quot;./complex-number&quot;</span><span class="br0">&#41;</span>.<span class="me1">sum</span>;<br />
&nbsp; exports.<span class="me1">plusTwo</span> = <span class="kw2">function</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; <span class="kw1">return</span> sum<span class="br0">&#40;</span>a, <span class="nu0">2</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span>;<br />
<span class="br0">&#125;</span>,<span class="br0">&#91;</span><span class="st0">&quot;complex-numbers/math&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span>; <span class="co1">// indicate the dependencies</span><br />
&nbsp;</div>
<p>There are a couple of excellent client-side libraries available for loading and handling modules in the CommonJS modules format including <a href="http://tagneto.blogspot.com/">James Burke&#8217;s</a> <a href="http://requirejs.org/">RequireJS</a> and <a href="http://github.com/jbrantly/yabble">Yabble</a>. RequireJS also provides a number of functions for easing construction of hand-crafted static modules with wrappers (rather than using server-side generation). These are well documented on the <a href="http://requirejs.org/docs/api.html">RequireJS API page</a>, so I won&#8217;t go into those here, but RequireJS makes it very easy to hand-craft modules for asynchronous loading. Yabble is designed to support direct loading of CommonJS modules (with or without wrapping), and supports other CommonJS APIs including require.ensure for explicit dynamic asynchronous loading of modules.</p>
<h2>Automated Module Wrapping</h2>
<p>A client-side library like RequireJS can then easily load modules and dependencies (like <code>complex-numbers/math.js</code>) asynchronously and execute the module body when they are available. Of course wrapping modules like this requires writing a lot of extra boilerplate code, especially if you are originally writing your code in CommonJS format, so naturally it is helpful to automate this. Fortunately, there are actually several projects that can automate this wrapping, allowing you to write plain CommonJS modules, and automatically wrap the modules so that they can be asynchronously loaded in the browser. <a href="http://github.com/khs4473/FlyScript">FlyScript</a> is a CommonJS module wrapper written in PHP, and <a href="http://github.com/codespeaks/modulr">modulr</a> is a Ruby-based module wrapper. Each of these come with their own client-side module loader, but with proper CommonJS transport format compliance they could be used with RequireJS as well.</p>
<p>Another module wrapper is my <a href="http://github.com/kriszyp/transporter">Transporter</a> project that is actually written as a CommonJS module for JSGI servers (like <a href="http://jackjs.org/">Jack</a>, and <a href="http://nodejs.org/">Node</a> with an <a href="http://github.com/kriszyp/jsgi-node/">adapter</a>). Transporter is particularly interesting since it is actually designed to run on a CommonJS server, and therefore paves the way for easily reusing the same modules on the server and client effortlessly. In fact, Transporter will default to loading modules using the same lookup path as the server-side CommonJS environment, to make it easy to share modules. Transporter has been primarily tested with RequireJS and Yabble.</p>
<p>Transporter includes the (configurable) ability to automatically resolve all dependencies on the server. This means that when a module is requested, all the dependencies are automatically included, rather than having to wait for the browser to individually request each dependency, which is much slower due to the latency and overhead of multiple HTTP request-response round trips. Transporter includes a number of other useful options including the ability to explicitly include and exclude dependencies from URLs (within script <code>src</code>), automatic running of the modules (in Yabble, modules aren&#8217;t auto-executed), specifying module source paths or loaders, and support for alternate converters (so that different formats besides CommonJS can be used). These are described in more detail in the <a href="http://github.com/kriszyp/transporter">Transporter documentation</a>.</p>
<h2>Dojo, RequireJS, CommonJS</h2>
<p>These new asynchronous loading capabilities are not just for CommonJS modules. James Burke has been hard at work in integrating RequireJS with Dojo. He has built a <a href="http://github.com/jrburke/requirejs/blob/master/build/convert/convertDojo.js">tool for a converting Dojo modules to RequireJS-compatible transport format</a>, integrated RequireJS with the Dojo bootstrap process, and created a full Dojo build that incorporates RequireJS with a fully converted copy of Dojo (including DojoX and Dijit). With this build you can use Dojo in fully asynchronous mode, getting the performance, usability, and debugging benefits of RequireJS&#8217;s loader with Dojo. After Dojo 1.5 is complete, we will be looking at various possibilities for using this type of technology and integrating asynchronous loading capabilities in core Dojo and possibly supporting CommonJS modules in Dojo as well. However, this is still a work in progress, and there are couple different options we are exploring, so I will save more details on this for a future post.</p>


<p>Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/03/04/pintura-jsgi-modules/' rel='bookmark' title='Permanent Link: Pintura JSGI Modules'>Pintura JSGI Modules</a></li><li><a href='http://www.sitepen.com/blog/2010/01/19/commonjsjsgi-the-emerging-javascript-application-server-platform/' rel='bookmark' title='Permanent Link: CommonJS/JSGI: The Emerging JavaScript Application Server Platform'>CommonJS/JSGI: The Emerging JavaScript Application Server Platform</a></li><li><a href='http://www.sitepen.com/blog/2010/01/22/introducing-pintura/' rel='bookmark' title='Permanent Link: Introducing Pintura'>Introducing Pintura</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/07/16/asynchronous-commonjs-modules-for-the-browser-and-introducing-transporter/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Multi-node: Concurrent NodeJS HTTP Server</title>
		<link>http://www.sitepen.com/blog/2010/07/14/multi-node-concurrent-nodejs-http-server/</link>
		<comments>http://www.sitepen.com/blog/2010/07/14/multi-node-concurrent-nodejs-http-server/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 16:55:54 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[Persevere]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1309</guid>
		<description><![CDATA[NodeJS has demonstrated impressive performance potential as an HTTP server. By combining highly optimized HTTP parsing algorithms with the speedy V8 JavaScript engine and using an event-based architecture, Node has posted eye-opening request handling numbers. However, Node historically has been limited by its inability to provide true concurrent request handling, greatly limiting its ability to [...]


Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/07/19/real-time-comet-applications-on-node-with-tunguska/' rel='bookmark' title='Permanent Link: Real-time Comet Applications on Node with Tunguska'>Real-time Comet Applications on Node with Tunguska</a></li><li><a href='http://www.sitepen.com/blog/2009/02/27/deterministic-clientserver-interaction/' rel='bookmark' title='Permanent Link: Deterministic Client/Server Interaction'>Deterministic Client/Server Interaction</a></li><li><a href='http://www.sitepen.com/blog/2010/01/19/commonjsjsgi-the-emerging-javascript-application-server-platform/' rel='bookmark' title='Permanent Link: CommonJS/JSGI: The Emerging JavaScript Application Server Platform'>CommonJS/JSGI: The Emerging JavaScript Application Server Platform</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://nodejs.org/">NodeJS</a> has demonstrated impressive performance potential as an HTTP server. By combining highly optimized HTTP parsing algorithms with the speedy V8 JavaScript engine and using an event-based architecture, Node has posted eye-opening request handling numbers. However, Node historically has been limited by its inability to provide true concurrent request handling, greatly limiting its ability to leverage increasingly multiple core servers. The latest release of Node introduced new functionality for socket sharing that can be coupled with child process spawning to achieve concurrent multi-process request handling for a single TCP socket. <a href="http://github.com/kriszyp/multi-node">Multi-node</a> exists to leverage this capability to make it simple to start up a multi-process Node server.</p>
<p><span id="more-1309"></span></p>
<p>Multi-node is very easy to use. You simply create an HTTP server object as you would normally do, and rather than immediately calling listen(), you pass it to the multi-node listen function:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> server = require<span class="br0">&#40;</span><span class="st0">&quot;http&quot;</span><span class="br0">&#41;</span>.<span class="me1">createServer</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>request, response<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8230; <span class="me1">standard</span> node request handler &#8230;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> nodes = require<span class="br0">&#40;</span><span class="st0">&quot;multi-node&quot;</span><span class="br0">&#41;</span>.<span class="me1">listen</span><span class="br0">&#40;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; port: <span class="nu0">80</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; nodes: <span class="nu0">4</span><br />
<span class="br0">&#125;</span>, server<span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>As you can see, we can indicate the number of &#8220;nodes&#8221; or processes to start up, and the port number to listen on. Multi-node will automatically spawn the appropriate processes and pass the socket to each child process to share. The OS kernel then essentially acts as the load balancer, queuing up incoming TCP requests and handing them off to processes as they can accept them. This can actually be more efficient than a separate load balancer, since socket connections are handled as processes request them, making the load balancing immediately dynamic. If a process is loaded with more difficult processor intense requests, it won&#8217;t grab as many requests and the other process will pick up the load.</p>
<p>One decision to make is choosing the number of processes (&#8221;nodes&#8221;) to run. Perhaps the most common suggestion would be to use the same number of processes as the number of CPU cores on the machine. Indeed this is likely to provide the most efficient utilization of the machine&#8217;s CPU resources, with minimal context switching overhead. However there can be reasons for using more or less processes. If you don&#8217;t want the Node server to consume all of the machine&#8217;s resources, you might specify less processes to keep other CPU cores available for other machine tasks.</p>
<p>Alternately, there can be &#8220;fairness&#8221; advantages to running more processes. Node won&#8217;t start processing another request until it finishes its current queue of events. If it has a large queue of expensive event handlers to process, this may increase the latency handling of a request even if it is small, lightweight request. With few processes, this request processing granularity is large. However, with the addition of more processes, the request processing granularity decreases and smaller requests have better odds of being serviced sooner rather than ending up being queued behind other performance expensive operations in the event queue of a few processes. More processes can slightly increase context switching overhead, but this may be worth the improved fairness and latency reduction for quick requests.</p>
<p>One of the core capabilities that multi-node also provides is setting up fully connected inter-process sockets so that processes can easily communicate with other processes. This is very important functionality for situations where data and messages need to be accessible between different processes. Real-time <a href="http://cometdaily.com/">Comet</a> applications like chat demonstrate this need. Chat messages received on one process may need to be sent to other processes so they can deliver messages to their connected client. Even for simple session management such capability can be useful. Since browsers utilize multiple connections to servers, it is very easy for one web application to be connected to different processes on the server, which may need to share session data.</p>
<p>To use the inter-process communication, simply add a &#8220;node&#8221; listener to the object returned from the listen() call. The &#8220;node&#8221; event is fired for each new Node process with a &#8220;stream&#8221; argument that can be used to communicate to that process:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> nodes = require<span class="br0">&#40;</span><span class="st0">&quot;multi-node&quot;</span><span class="br0">&#41;</span>.<span class="me1">listen</span><span class="br0">&#40;</span>&#8230;<span class="br0">&#41;</span>;<br />
nodes.<span class="me1">addListener</span><span class="br0">&#40;</span><span class="st0">&quot;node&quot;</span>, <span class="kw2">function</span><span class="br0">&#40;</span>stream<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; stream.<span class="me1">addListener</span><span class="br0">&#40;</span><span class="st0">&quot;data&quot;</span>, <span class="kw2">function</span><span class="br0">&#40;</span>data<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8230; <span class="me1">receiving</span> data from <span class="kw1">this</span> other node process &#8230;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="me1">stream</span>.<span class="kw1">write</span><span class="br0">&#40;</span>&#8230; <span class="kw1">write</span> data to other process&#8230;<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>This event should be fired for every other Node process (for this server) that is created, allowing you to communicate to any other process.</p>
<p>Multi-node allows you to easily leverage Node&#8217;s new socket sharing capabilities and build truly scalable, concurrent, Node-based web application servers. While multi-node is intended to be usable on its own, multi-node is a sub-project of <a href="http://www.persvr.org/">Persevere</a> and the <a href="http://github.com/kriszyp/persevere-example-wiki/blob/master/lib/index.js">Persevere example wiki demonstrates</a> how multi-node can easily be used with Persevere applications. Next we will look at how another Persevere sub-project, <a href="http://github.com/kriszyp/tunguska">Tunguska</a>, can leverage multi-node&#8217;s inter-process communication for building scalable multi-process real-time applications.</p>


<p>Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/07/19/real-time-comet-applications-on-node-with-tunguska/' rel='bookmark' title='Permanent Link: Real-time Comet Applications on Node with Tunguska'>Real-time Comet Applications on Node with Tunguska</a></li><li><a href='http://www.sitepen.com/blog/2009/02/27/deterministic-clientserver-interaction/' rel='bookmark' title='Permanent Link: Deterministic Client/Server Interaction'>Deterministic Client/Server Interaction</a></li><li><a href='http://www.sitepen.com/blog/2010/01/19/commonjsjsgi-the-emerging-javascript-application-server-platform/' rel='bookmark' title='Permanent Link: CommonJS/JSGI: The Emerging JavaScript Application Server Platform'>CommonJS/JSGI: The Emerging JavaScript Application Server Platform</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/07/14/multi-node-concurrent-nodejs-http-server/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<series:name><![CDATA[Server-Side JavaScript, Pintura, and Persevere 2.0]]></series:name>
	</item>
		<item>
		<title>Dive Into Dojo Charting</title>
		<link>http://www.sitepen.com/blog/2010/07/13/dive-into-dojo-charting/</link>
		<comments>http://www.sitepen.com/blog/2010/07/13/dive-into-dojo-charting/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 07:01:20 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1378</guid>
		<description><![CDATA[
One of the most powerful pieces of Dojo is also one of the most underutilized: Charting.  The Dojo Charting library lives within the DojoX (extensions) branch of Dojo, and features numerous chart types, options, and a variety of themes. This post introduce you to the charting library and show you how you can take a [...]


Related posts:<ol><li><a href='http://www.sitepen.com/blog/2008/06/16/a-beginners-guide-to-dojo-charting-part-2-of-2/' rel='bookmark' title='Permanent Link: A Beginner&#8217;s Guide to Dojo Charting, Part 2 of 2'>A Beginner&#8217;s Guide to Dojo Charting, Part 2 of 2</a></li><li><a href='http://www.sitepen.com/blog/2008/06/06/a-beginners-guide-to-dojo-charting-part-1-of-2/' rel='bookmark' title='Permanent Link: A Beginner&#8217;s Guide to Dojo Charting, Part 1 of 2'>A Beginner&#8217;s Guide to Dojo Charting, Part 1 of 2</a></li><li><a href='http://www.sitepen.com/blog/2008/05/15/zooming-scrolling-and-panning-in-dojo-charting/' rel='bookmark' title='Permanent Link: Zooming, Scrolling, and Panning in Dojo Charting'>Zooming, Scrolling, and Panning in Dojo Charting</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepen.com/blog/wp-content/uploads/2010/07/Chart-List.jpg" alt="Chart Types" /></p>
<p>One of the most powerful pieces of Dojo is also one of the most underutilized: Charting.  The Dojo Charting library lives within the DojoX (extensions) branch of Dojo, and features numerous chart types, options, and a variety of themes. This post introduce you to the charting library and show you how you can take a boring data collection and make it a beautiful visual chart in any modern web browser.</p>
<p><span id="more-1378"></span></p>
<h2>Requiring Charting Classes</h2>
<p>A common misconception with many Dojo libraries is that they require a large number of dependencies &#8212; this is not the case.  Dependencies change depending on the chart you&#8217;d like to make, but a simple chart can be created with only the following Dojo classes:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="coMULTI">/* required modules for the basic chart */</span><br />
<span class="co1">//Main 2d chart class</span><br />
dojo.<span class="me1">require</span><span class="br0">&#40;</span><span class="st0">&quot;dojox.charting.widget.Chart2D&quot;</span><span class="br0">&#41;</span>;<br />
<span class="co1">//Class to create a legend for our chart</span><br />
dojo.<span class="me1">require</span><span class="br0">&#40;</span><span class="st0">&quot;dojox.charting.widget.Legend&quot;</span><span class="br0">&#41;</span>;<br />
<span class="co1">//A theme for our chart</span><br />
dojo.<span class="me1">require</span><span class="br0">&#40;</span><span class="st0">&quot;dojox.charting.themes.PlotKit.green&quot;</span><span class="br0">&#41;</span>;</div>
<h2>Two Ways to Play</h2>
<p>Much like <a href="http://www.sitepen.com/blog/2010/07/12/dive-into-dijit/">most of the widgets within the Dijit and DojoX libraries</a>, the charting library allows you to create charts programmatically and declaratively.  Let&#8217;s take a look at an example of each approach.</p>
<h3>Sample Data</h3>
<p>The following JSON is used for our simple Pie chart:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> pieData = <span class="br0">&#91;</span><span class="br0">&#123;</span> <span class="st0">&quot;x&quot;</span>: <span class="st0">&quot;1&quot;</span>, <span class="st0">&quot;y&quot;</span>: <span class="st0">&quot;9420&quot;</span> <span class="br0">&#125;</span>, <span class="br0">&#123;</span> <span class="st0">&quot;x&quot;</span>:<span class="st0">&quot;2&quot;</span>, <span class="st0">&quot;y&quot;</span>:<span class="st0">&quot;10126&quot;</span> <span class="br0">&#125;</span>, <span class="br0">&#123;</span> <span class="st0">&quot;x&quot;</span>: <span class="st0">&quot;3&quot;</span>, <span class="st0">&quot;y&quot;</span>: <span class="st0">&quot;9803&quot;</span> <span class="br0">&#125;</span>, <span class="br0">&#123;</span> <span class="st0">&quot;x&quot;</span>: <span class="st0">&quot;4&quot;</span>, <span class="st0">&quot;y&quot;</span>: <span class="st0">&quot;15965&quot;</span> <span class="br0">&#125;</span>, <span class="br0">&#123;</span> <span class="st0">&quot;x&quot;</span>: <span class="st0">&quot;5&quot;</span>, <span class="st0">&quot;y&quot;</span>: <span class="st0">&quot;17290&quot;</span> <span class="br0">&#125;</span>, <span class="br0">&#123;</span> <span class="st0">&quot;x&quot;</span>: <span class="st0">&quot;6&quot;</span>, <span class="st0">&quot;y&quot;</span>: <span class="st0">&quot;15667&quot;</span> <span class="br0">&#125;</span>, <span class="br0">&#123;</span> <span class="st0">&quot;x&quot;</span>: <span class="st0">&quot;7&quot;</span>, <span class="st0">&quot;y&quot;</span>: <span class="st0">&quot;17762&quot;</span> <span class="br0">&#125;</span><span class="br0">&#93;</span>;</div>
<h3>Declarative Chart Creation</h3>
<p>The declarative method of creating charts is done with HTML markup.  All chart data such as type, axis, plot, and other chart information is created within the chart container.  Here&#8217;s a simple pie chart:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><span class="coMULTI">&lt;!&#8211; create the chart &#8211;&gt;</span></span><br />
<span class="sc2"><a href="http://december.com/html/4/element/div.html"><span class="kw2">&lt;div</span></a> dojoType=<span class="st0">&quot;dojox.charting.widget.Chart2D&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;viewsChart&quot;</span> theme=<span class="st0">&quot;dojox.charting.themes.PlotKit.green&quot;</span> <span class="kw3">style</span>=<span class="st0">&quot;width: 550px; height: 550px;&quot;</span><span class="kw2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><span class="coMULTI">&lt;!&#8211; add the plot &#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/div.html"><span class="kw2">&lt;div</span></a> <span class="kw3">class</span>=<span class="st0">&quot;plot&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;default&quot;</span> <span class="kw3">type</span>=<span class="st0">&quot;Pie&quot;</span> radius=<span class="st0">&quot;200&quot;</span> fontColor=<span class="st0">&quot;black&quot;</span> labelOffset=<span class="st0">&quot;-20&quot;</span><span class="kw2">&gt;</span></span><span class="sc2"><span class="kw2">&lt;/div&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><span class="coMULTI">&lt;!&#8211; pieData is the data source &#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/div.html"><span class="kw2">&lt;div</span></a> <span class="kw3">class</span>=<span class="st0">&quot;series&quot;</span> <span class="kw3">name</span>=<span class="st0">&quot;January&quot;</span> array=<span class="st0">&quot;pieData&quot;</span><span class="kw2">&gt;</span></span><span class="sc2"><span class="kw2">&lt;/div&gt;</span></span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><span class="coMULTI">&lt;!&#8211; tooltips! &#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/div.html"><span class="kw2">&lt;div</span></a> <span class="kw3">class</span>=<span class="st0">&quot;action&quot;</span> <span class="kw3">type</span>=<span class="st0">&quot;Tooltip&quot;</span><span class="kw2">&gt;</span></span><span class="sc2"><span class="kw2">&lt;/div&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><span class="coMULTI">&lt;!&#8211; slice animation! &#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/div.html"><span class="kw2">&lt;div</span></a> <span class="kw3">class</span>=<span class="st0">&quot;action&quot;</span> <span class="kw3">type</span>=<span class="st0">&quot;MoveSlice&quot;</span> shift=<span class="st0">&quot;2&quot;</span><span class="kw2">&gt;</span></span><span class="sc2"><span class="kw2">&lt;/div&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="sc2"><span class="kw2">&lt;/div&gt;</span></span></div>
<p><a href="http://user.sitepen.com/~dwalsh/dojo-charting.html#piechart"><img src="http://www.sitepen.com/blog/wp-content/uploads/2010/07/Chart2-Pie.jpg" alt="Dojo Pie Chart" /></a></p>
<h3>Programmatic Chart Creation</h3>
<p>Programmatic chart creation is also quite easy.  The JavaScript code below reproduces the chart above:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//create the chart</span><br />
<span class="kw2">var</span> pieChart = <span class="kw2">new</span> dojox.<span class="me1">charting</span>.<span class="me1">Chart2D</span><span class="br0">&#40;</span><span class="st0">&quot;pieChart&quot;</span><span class="br0">&#41;</span>;<br />
<span class="co1">//set the theme</span><br />
pieChart.<span class="me1">setTheme</span><span class="br0">&#40;</span>dojox.<span class="me1">charting</span>.<span class="me1">themes</span>.<span class="me1">PlotKit</span>.<span class="me1">green</span><span class="br0">&#41;</span>;<br />
<span class="co1">//add plot </span><br />
pieChart.<span class="me1">addPlot</span><span class="br0">&#40;</span><span class="st0">&quot;default&quot;</span>, <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; type: <span class="st0">&quot;Pie&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; radius: <span class="nu0">200</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; fontColor: <span class="st0">&quot;black&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; labelOffset: <span class="st0">&quot;-20&quot;</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
<span class="co1">//add the data series</span><br />
pieChart.<span class="me1">addSeries</span><span class="br0">&#40;</span><span class="st0">&quot;January&quot;</span>,pieData<span class="br0">&#41;</span>;<br />
<span class="co1">//slice animation!</span><br />
<span class="kw2">new</span> dojox.<span class="me1">charting</span>.<span class="me1">action2d</span>.<span class="me1">MoveSlice</span><span class="br0">&#40;</span>pieChart,<span class="st0">&quot;default&quot;</span><span class="br0">&#41;</span>;<br />
<span class="co1">//tooltips!</span><br />
<span class="kw2">new</span> dojox.<span class="me1">charting</span>.<span class="me1">action2d</span>.<span class="me1">Tooltip</span><span class="br0">&#40;</span>pieChart,<span class="st0">&quot;default&quot;</span><span class="br0">&#41;</span>;<br />
<span class="co1">//render the chart</span><br />
pieChart.<span class="me1">render</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p>Take a moment to compare the two methods and you will quickly notice how similar the structures are.  It&#8217;s the developer&#8217;s choice which method, declarative or programmatic, to use.  This tutorial will focus on the programmatic method of chart creation though all examples work with both approaches.</p>
<h2>Creating a Simple Chart</h2>
<p>Now that the fundamental pieces of the charting puzzle have been explained, let&#8217;s create a very basic chart.  This chart will depict website visits over 3 months time.  The chart will be a basic StackedAreas chart with different colors to denote the different months.  We&#8217;ll take it step by step.</p>
<p>The first step is to place our data in the format with which the chart works.  The data for our StackedArea chart must hold each value in one array:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> json = <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; January: <span class="br0">&#91;</span><span class="nu0">9420</span>,<span class="nu0">10126</span>,<span class="nu0">9803</span>,<span class="nu0">15965</span>,<span class="nu0">17290</span>, <span class="coMULTI">/* &#8230; */</span> &nbsp;<span class="nu0">13165</span>,<span class="nu0">12390</span><span class="br0">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; February: <span class="br0">&#91;</span><span class="nu0">23990</span>,<span class="nu0">32975</span>,<span class="nu0">23477</span>,<span class="nu0">22513</span>,<span class="nu0">18069</span>, <span class="coMULTI">/* &#8230; */</span> <span class="nu0">12956</span>,<span class="nu0">12815</span><span class="br0">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; March: <span class="br0">&#91;</span><span class="nu0">22477</span>,<span class="nu0">24014</span>,<span class="nu0">21116</span>,<span class="nu0">20404</span>,<span class="nu0">19142</span>, <span class="coMULTI">/* &#8230; */</span> <span class="nu0">22077</span>,<span class="nu0">20263</span><span class="br0">&#93;</span><br />
<span class="br0">&#125;</span>;</div>
<p>The next step is to create the chart programatically.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//we have a DIV element with an ID of &quot;chart1&quot;; &nbsp;that&#8217;s the argument</span><br />
<span class="kw2">var</span> chart1 = <span class="kw2">new</span> dojox.<span class="me1">charting</span>.<span class="me1">Chart2D</span><span class="br0">&#40;</span><span class="st0">&#8216;chart1&#8242;</span><span class="br0">&#41;</span>;</div>
<p>Then it&#8217;s time to add the plot.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//add the default plot</span><br />
chart1.<span class="me1">addPlot</span><span class="br0">&#40;</span><span class="st0">&quot;default&quot;</span>, <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//type of chart</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; type: <span class="st0">&quot;StackedAreas&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//show markers at number points?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; markers: <span class="kw2">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//curve the lines on the plot?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tension: <span class="st0">&quot;S&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//show lines?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; lines: <span class="kw2">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//fill in areas?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; areas: <span class="kw2">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//offset position for label</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; labelOffset: <span class="nu0">-30</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//add shadows to lines</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; shadows: <span class="br0">&#123;</span> dx:<span class="nu0">2</span>, dy:<span class="nu0">2</span>, dw:<span class="nu0">2</span> <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
<p>Since the numbers in this type of chart are significant, we want to add an X axis (representing days) and a Y axis (representing traffic / views).</p>
<div class="dean_ch" style="white-space: wrap;">
chart1.<span class="me1">addAxis</span><span class="br0">&#40;</span><span class="st0">&quot;x&quot;</span><span class="br0">&#41;</span>;<br />
chart1.<span class="me1">addAxis</span><span class="br0">&#40;</span><span class="st0">&quot;y&quot;</span>, <span class="br0">&#123;</span> vertical:<span class="kw2">true</span> <span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Now that the frame of the chart has been created, it is time to add the information for each month.  Since we want to fill areas, we&#8217;ll need to pass a stroke and fill color within the series:</p>
<div class="dean_ch" style="white-space: wrap;">
chart1.<span class="me1">addSeries</span><span class="br0">&#40;</span><span class="st0">&quot;January Visits&quot;</span>,json<span class="br0">&#91;</span><span class="st0">&quot;January&quot;</span><span class="br0">&#93;</span>, <span class="br0">&#123;</span> stroke: <span class="st0">&quot;red&quot;</span>, fill: <span class="st0">&quot;pink&quot;</span> <span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
chart1.<span class="me1">addSeries</span><span class="br0">&#40;</span><span class="st0">&quot;February Visits&quot;</span>,json<span class="br0">&#91;</span><span class="st0">&quot;February&quot;</span><span class="br0">&#93;</span>, <span class="br0">&#123;</span> stroke: <span class="st0">&quot;green&quot;</span>, fill: <span class="st0">&quot;lightgreen&quot;</span> <span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
chart1.<span class="me1">addSeries</span><span class="br0">&#40;</span><span class="st0">&quot;March Visits&quot;</span>,json<span class="br0">&#91;</span><span class="st0">&quot;March&quot;</span><span class="br0">&#93;</span>, <span class="br0">&#123;</span> stroke: <span class="st0">&quot;blue&quot;</span>, fill: <span class="st0">&quot;lightblue&quot;</span> <span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Now that our chart is built and the data is in place, we can render the chart:</p>
<div class="dean_ch" style="white-space: wrap;">
chart1.<span class="me1">render</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>The result is a beautiful chart:</p>
<p><a href="http://user.sitepen.com/~dwalsh/dojo-charting.html#stackedchart"><img src="http://www.sitepen.com/blog/wp-content/uploads/2010/07/DWBasicChart.jpg" alt="Plain Chart" /></a></p>
<h2>Chart Animations, Legends, and Extras</h2>
<p>As with every other piece of the Dojo library, the Dojo charting library is flexible enough to incorporate other Dojo classes and widgets.  The basic chart we created above is nice but could use a few useful enhancements.</p>
<h3>Legend</h3>
<p>Adding a legend to the chart is very simple:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//this legend is created within an element with a &quot;legend1&quot; ID.</span><br />
<span class="kw2">var</span> legend1 = <span class="kw2">new</span> dojox.<span class="me1">charting</span>.<span class="me1">widget</span>.<span class="me1">Legend</span><span class="br0">&#40;</span><span class="br0">&#123;</span>chart: chart1<span class="br0">&#125;</span>, <span class="st0">&quot;legend1&quot;</span><span class="br0">&#41;</span>;</div>
<h3>Chart Animations</h3>
<p>As with basic content on a web page, animation adds some flare and shows focus on an element. Each chart type supports different sets of animations.  To keep it simple, we can add a &#8220;magnify&#8221; animation to the markers on our chart so that the marker grows when the mouse enters it:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//add the magnification animation to our chart for the default plot</span><br />
<span class="kw2">var</span> magnify = <span class="kw2">new</span> dojox.<span class="me1">charting</span>.<span class="me1">action2d</span>.<span class="me1">Magnify</span><span class="br0">&#40;</span>chart1, <span class="st0">&quot;default&quot;</span><span class="br0">&#41;</span>;</div>
<h3>Tooltips</h3>
<p>Our chart features markers at the designated x/y axis numbers but we can enhance those markers with tooltips so that the user can see the exact number the marker represents.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> tip = <span class="kw2">new</span> dojox.<span class="me1">charting</span>.<span class="me1">action2d</span>.<span class="me1">Tooltip</span><span class="br0">&#40;</span>chart1, <span class="st0">&quot;default&quot;</span><span class="br0">&#41;</span>;</div>
<p><a href="http://user.sitepen.com/~dwalsh/dojo-charting.html#stackedchart"><img src="http://www.sitepen.com/blog/wp-content/uploads/2010/07/DWEnhancedChart.jpg" alt="Enhanced Chart" /></a></p>
<p>You may also pair Dijit themes with your chart to style the tooltip;  I&#8217;ve added the &#8220;tundra&#8221; theme&#8217;s CSS file to enhance the style of my tooltip.</p>
<h3>Zooming, Scrolling, and Panning</h3>
<p>As you would expect from a vector graphic generation tool, Dojo&#8217;s charting library provides a method by which you may flawlessly zoom in and out on any chart.  You may also accommodate panning and scrolling on your charts.  Read <a href="http://www.sitepen.com/blog/2008/05/15/zooming-scrolling-and-panning-in-dojo-charting/">Zooming, Scrolling, and Panning in Dojo Charting</a> to learn more about these features or <a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/charting/tests/test_win2d.html">view a great example of panning, scrolling, and zooming!</a></p>
<h2>Chart Events</h2>
<p>Dojo&#8217;s charting library also has its own event connection method: connectToPlot.  This method allows you to add event listeners to your chart and trigger functionality based on the given event.</p>
<div class="dean_ch" style="white-space: wrap;">
chart1.<span class="me1">connectToPlot</span><span class="br0">&#40;</span><span class="st0">&quot;default&quot;</span>,<span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* do something */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Chart events provide you a wealth of information, including the type of event, coordinates, plot, shape, and more.  Visit the <a href="http://www.dojotoolkit.org/reference-guide/dojox/charting.html#chart-events">Dojo Charting Reference Guide</a> to get detailed information about chart events.</p>
<h2>Chart Themes</h2>
<p>The Dojo charting library provides over 30 themes to make your charts stand out.  You may see a complete list of bundled themes (with preview) in the <a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/charting/tests/theme_preview.html">Dojo Nightly Theme Previewer</a>.  SitePen will publish a post in the near future detailing how to create a custom theme!
</p>
<p><a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/charting/tests/theme_preview.html"><img src="http://www.sitepen.com/blog/wp-content/uploads/2010/07/charting-themes.jpg" alt="Chart Themes" /></a></p>
<h2>Chart Families</h2>
<p>This tutorial showed you how to create a basic, but beautiful 2D chart; Dojo&#8217;s charting library has many advanced charting capabilities though.  The different families of Dojo charts includes:
</p>
<ul>
<li>2D Charts: The basic two-dimensional chart.  (<a href="http://user.sitepen.com/~dwalsh/dojo-charting.html">examples</a>)</li>
<li>3D Charts: Three-dimensional representations of data.  (<a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/charting/tests/test_bars.html">example</a>)</li>
<li>Data Charts:  Advanced chart, connectable to an external data source.  (<a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/charting/tests/test_DataChart.html">example</a> | <a href="http://persevere.sitepen.com/stocker.html">advanced example: Stocker</a> | <a href="http://www.sitepen.com/blog/2009/03/30/introducing-dojox-datachart/">tutorial</a>)</li>
</ul>
<p></p>
<h2>New Charting Features in Dojo 1.5</h2>
<p>The release of Dojo 1.5 introduces new features to the charting library; most notably:</p>
<ul>
<li>Linear and radial gradients can now be used with virtually all chart types (and they even work in IE).</li>
<li>Flexible data sourcing via DataSeries adds the ability to back charts with practically any sort of data store one would like, even supplying secondary information like tooltips.</li>
<li>Plot and series order manipulations enable one to programmatically shuffle plots and series as needed.</li>
<li>Indirect events that effectively make it possible for linked plots to share events.</li>
<li>External events allow a developer to programmatically generate things like mouseovers and apply them to charting objects.</li>
<li>The default axis is now smarter regarding scaling and sizing.</li>
<li>A new lightweight invisible axis type allows the user to control plot scale and alignment without displaying an axis.</li>
</ul>
<h2>Great Charting Resources</h2>
<ul>
<li><a href="http://www.dojotoolkit.org/reference-guide/dojox/charting.html#dojox-charting">Dojox Charting Reference Guide</a></li>
<li><a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/charting/tests/">Nightly Tests</a></li>
<li><a href="http://www.sitepen.com/blog/2009/03/30/introducing-dojox-datachart/">Introducing Dojox Data Chart</a></li>
<li><a href="http://www.sitepen.com/blog/2008/06/06/a-beginners-guide-to-dojo-charting-part-1-of-2/">A Beginner’s Guide to Dojo Charting, Part 1 of 2</a></li>
<li><a href="http://www.sitepen.com/blog/2008/06/16/a-beginners-guide-to-dojo-charting-part-2-of-2/">A Beginner’s Guide to Dojo Charting, Part 2 of 2</a></li>
<li><a href="http://www.sitepen.com/blog/2008/05/15/zooming-scrolling-and-panning-in-dojo-charting/">Zooming, Scrolling, and Panning in Dojo Charting</a></li>
</ul>
<p></p>
<p>The concept of charting can seem difficult, but Dojo&#8217;s charting library is a breeze to dive into!</p>


<p>Related posts:<ol><li><a href='http://www.sitepen.com/blog/2008/06/16/a-beginners-guide-to-dojo-charting-part-2-of-2/' rel='bookmark' title='Permanent Link: A Beginner&#8217;s Guide to Dojo Charting, Part 2 of 2'>A Beginner&#8217;s Guide to Dojo Charting, Part 2 of 2</a></li><li><a href='http://www.sitepen.com/blog/2008/06/06/a-beginners-guide-to-dojo-charting-part-1-of-2/' rel='bookmark' title='Permanent Link: A Beginner&#8217;s Guide to Dojo Charting, Part 1 of 2'>A Beginner&#8217;s Guide to Dojo Charting, Part 1 of 2</a></li><li><a href='http://www.sitepen.com/blog/2008/05/15/zooming-scrolling-and-panning-in-dojo-charting/' rel='bookmark' title='Permanent Link: Zooming, Scrolling, and Panning in Dojo Charting'>Zooming, Scrolling, and Panning in Dojo Charting</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/07/13/dive-into-dojo-charting/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<series:name><![CDATA[Dive Into Dojo]]></series:name>
	</item>
		<item>
		<title>Dive Into Dijit</title>
		<link>http://www.sitepen.com/blog/2010/07/12/dive-into-dijit/</link>
		<comments>http://www.sitepen.com/blog/2010/07/12/dive-into-dijit/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 14:37:18 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[UI Design]]></category>
		<category><![CDATA[dijit]]></category>
		<category><![CDATA[dojo15]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1351</guid>
		<description><![CDATA[One huge feature that sets the Dojo Toolkit apart from other JavaScript libraries is its UI component system: Dijit.  A flexible, comprehensive collection of Dojo classes (complemented by corresponding assets like images, CSS files, etc.), Dijit allows you to create flexible, extensible, stylish widgets.  To learn how to install, configure, and use basic [...]


Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/08/11/dive-into-dijit-forms/' rel='bookmark' title='Permanent Link: Dive Into Dijit Forms'>Dive Into Dijit Forms</a></li><li><a href='http://www.sitepen.com/blog/2009/02/25/styling-dijit-form-elements/' rel='bookmark' title='Permanent Link: Styling Dijit Form Elements'>Styling Dijit Form Elements</a></li><li><a href='http://www.sitepen.com/blog/2009/11/05/dijit-prepackaged/' rel='bookmark' title='Permanent Link: Dijit: Prepackaged'>Dijit: Prepackaged</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>One huge feature that sets the Dojo Toolkit apart from other JavaScript libraries is its UI component system: Dijit.  A flexible, comprehensive collection of Dojo classes (complemented by corresponding assets like images, CSS files, etc.), Dijit allows you to create flexible, extensible, stylish widgets.  To learn how to install, configure, and use basic Dijits within your web application, keep reading!</p>
<p>
<a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dijit/themes/themeTester.html"><img src="http://www.sitepen.com/blog/wp-content/uploads/2010/07/DijitBlogPost2.jpg" alt="Dijit Widgets" /></a></p>
<p><span id="more-1351"></span></p>
<h2>Requiring Proper Classes and Assets</h2>
<p>Since Dijit includes a collection of UI components, it comes bundled with four supported themes:  nihilo, soria, tundra, and (as of Dojo 1.5) claro.  Each theme contains images and CSS files to control the overall display of the widgets.  CSS files must be explicitly included into each HTML page:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/style.html"><span class="kw2">&lt;style</span></a> <span class="kw3">type</span>=<span class="st0">&quot;text/css&quot;</span><span class="kw2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; /* use the tundra theme */<br />
&nbsp; &nbsp; &nbsp; &nbsp; @import &quot;http://ajax.googleapis.com/ajax/libs/dojo/1.5/dijit/themes/tundra/tundra.css&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; /* Note that if you don&#8217;t specify a minor revision, e.g. 1.5.0 or 1.4.3, the CDN will deliver the latest version */<br />
<span class="sc2"><span class="kw2">&lt;/style&gt;</span></span><br />
&nbsp;</div>
<p>You can view each theme set using the <a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dijit/themes/themeTester.html">Dijit Theme Tester</a>.  You may also define your own theme.</p>
<p>There is another consideration that must be made when Dojo is included in the page: <code>parseOnLoad</code>.  Adding <code>parseOnLoad</code> to the <code>djConfig</code> attribute will direct Dojo to scour the page for elements that should become Dijit widgets and make them such:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/script.html"><span class="kw2">&lt;script</span></a> <span class="kw3">type</span>=<span class="st0">&quot;text/javascript&quot;</span> <span class="kw3">src</span>=<span class="st0">&quot;http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js.uncompressed.js&quot;</span> djConfig=<span class="st0">&quot;parseOnLoad:true&quot;</span><span class="kw2">&gt;</span></span><span class="sc2"><span class="kw2">&lt;/script&gt;</span></span><br />
&nbsp;</div>
<p>This parseOnLoad setting must be true if you prefer automatically instantiating Dijits defined declaratively (which we&#8217;ll examine below). You will also need to require Dijit classes you plan to use within the page. For example, If you want the <code>dijit.form.Button</code> widget within your pages, you&#8217;ll need to require that class:</p>
<div class="dean_ch" style="white-space: wrap;">
dojo.<span class="me1">require</span><span class="br0">&#40;</span><span class="st0">&#8216;dijit.form.Button&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>In the body tag of your page, or in a parent node of your widgets, you need to define the class name based upon the theme you would like to use.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/body.html"><span class="kw2">&lt;body</span></a> <span class="kw3">class</span>=<span class="st0">&quot;claro&quot;</span><span class="kw2">&gt;</span></span><br />
&nbsp;</div>
<p>Now let&#8217;s learn the two ways you can create Dijit widgets.</p>
<h2>Two Ways to Play</h2>
<p>One of the most notable features of the Dijit system is that you may create a Dijit widget in one of two ways:  declaratively (using HTML markup and custom attributes) or programmatically (using raw JavaScript, mostly likely within a <code>dojo.ready</code> block).  Let&#8217;s take a look at how we can make a basic SELECT element an Dojo-enhanced widget.</p>
<h3>The Basic SELECT Element</h3>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc2"><a href="http://december.com/html/4/element/select.html"><span class="kw2">&lt;select</span></a> <span class="kw3">name</span>=<span class="st0">&quot;players&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;players&quot;</span><span class="kw2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;&quot;</span><span class="kw2">&gt;</span></span>Select an Arsenal Player<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;Arshavin&quot;</span> <span class="kw3">selected</span>=<span class="st0">&quot;selected&quot;</span><span class="kw2">&gt;</span></span>Andrey Arshavin<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;Vermaelen&quot;</span><span class="kw2">&gt;</span></span>Thomas Vermaelen<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><span class="coMULTI">&lt;!&#8211; more OPTION elements here &#8211;&gt;</span></span><br />
<span class="sc2"><span class="kw2">&lt;/select&gt;</span></span><br />
&nbsp;</div>
<p>This is a static SELECT element containing a series of OPTION elements.  We know we&#8217;d like for this SELECT element to become a <code>dijit.form.FilteringSelect</code> widget so we must require this class:</p>
<div class="dean_ch" style="white-space: wrap;">dojo.<span class="me1">require</span><span class="br0">&#40;</span><span class="st0">&#8216;dijit.form.FilteringSelect&#8217;</span><span class="br0">&#41;</span>;</div>
<p>Now that the <code>dijit.form.FilteringSelect</code> class is available, we can use the declarative or programmatic method of enhancing our SELECT element.</p>
<h3>Declarative Method</h3>
<p>The declarative method of enhancing the SELECT element is done within the HTML element itself:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="sc2"><a href="http://december.com/html/4/element/select.html"><span class="kw2">&lt;select</span></a> <span class="kw3">name</span>=<span class="st0">&quot;players&quot;</span> <span class="kw3">id</span>=<span class="st0">&quot;players&quot;</span> dojoType=<span class="st0">&quot;dijit.form.FilteringSelect&quot;</span> autoComplete=<span class="st0">&quot;true&quot;</span> pageSize=<span class="st0">&quot;10&quot;</span><span class="kw2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;&quot;</span><span class="kw2">&gt;</span></span>Select an Arsenal Player<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;Arshavin&quot;</span> <span class="kw3">selected</span>=<span class="st0">&quot;selected&quot;</span><span class="kw2">&gt;</span></span>Andrey Arshavin<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><a href="http://december.com/html/4/element/option.html"><span class="kw2">&lt;option</span></a> <span class="kw3">value</span>=<span class="st0">&quot;Vermaelen&quot;</span><span class="kw2">&gt;</span></span>Thomas Vermaelen<span class="sc2"><span class="kw2">&lt;/option&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2"><span class="coMULTI">&lt;!&#8211; more OPTION elements here &#8211;&gt;</span></span><br />
<span class="sc2"><span class="kw2">&lt;/select&gt;</span></span></div>
<p>This declarative example illustrates the use of the dojoType attribute which identifies which Dijit the given element should become.  Since we have declared <code>parseOnLoad</code> &#8220;true&#8221; in our <code>djConfig</code>, Dojo will find this element immediately upon load (due to the element having a <code>dojoType</code> attribute) and instantiate and initialize the widget.</p>
<p>Options of the <code>dijit.form.FilteringSelect</code> class are also custom attributes.  This widget will autocomplete when the user types a value and page every 10 items.  Just as with a normal SELECT element, &#8220;Arshavin&#8221; will be selected initially.</p>
<h3>Programmatic Method</h3>
<p>The programmatic method of enhancing the SELECT element is done completely with JavaScript:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//require the class</span><br />
dojo.<span class="me1">require</span><span class="br0">&#40;</span><span class="st0">&#8216;dijit.form.FilteringSelect&#8217;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//when the class has been loaded&#8230;</span><br />
dojo.<span class="me1">ready</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//dijitize my SELECT!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> enhancedSelect = <span class="kw2">new</span> dijit.<span class="me1">Form</span>.<span class="me1">FilteringSelect</span><span class="br0">&#40;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; autoComplete: <span class="kw2">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageSize: <span class="nu0">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,<span class="st0">&#8216;players&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//dijitize any form field individually</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> inputDijit = <span class="kw2">new</span> dijit.<span class="me1">form</span>.<span class="me1">Textbox</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="coMULTI">/*options*/</span><span class="br0">&#125;</span>,<span class="st0">&#8216;myInput&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> textareaDijit = <span class="kw2">new</span> dijit.<span class="me1">form</span>.<span class="me1">Textarea</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="coMULTI">/*options*/</span><span class="br0">&#125;</span>,<span class="st0">&#8216;myTextarea&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> mySelectDijit = <span class="kw2">new</span> dijit.<span class="me1">form</span>.<span class="me1">FilteringSelect</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="coMULTI">/*options*/</span><span class="br0">&#125;</span>,<span class="st0">&#8216;mySelect&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> dateDijit = <span class="kw2">new</span> dijit.<span class="me1">form</span>.<span class="me1">DateTextBox</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="coMULTI">/*options*/</span><span class="br0">&#125;</span>,<span class="st0">&#8216;myDate&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> timeDijit = <span class="kw2">new</span> dijit.<span class="me1">form</span>.<span class="me1">TimeTextBox</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="coMULTI">/*options*/</span><span class="br0">&#125;</span>,<span class="st0">&#8216;myTime&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> checkboxDijit = <span class="kw2">new</span> dijit.<span class="me1">form</span>.<span class="me1">CheckBox</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="coMULTI">/*options*/</span><span class="br0">&#125;</span>,<span class="st0">&#8216;myCheckbox&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> radioDijit1 = <span class="kw2">new</span> dijit.<span class="me1">form</span>.<span class="me1">RadioButton</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="coMULTI">/*options*/</span><span class="br0">&#125;</span>,<span class="st0">&#8216;myRadio1&#8242;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> radioDijit2 = <span class="kw2">new</span> dijit.<span class="me1">form</span>.<span class="me1">RadioButton</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="coMULTI">/*options*/</span><span class="br0">&#125;</span>,<span class="st0">&#8216;myRadio2&#8242;</span><span class="br0">&#41;</span>;</p>
<p><span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p><em>Reminder:  the programmatic method of Dijit creation does not require you to enable </em><code><em>parseOnLoad</em></code><em> within the djConfig settings.</em></p>
<p>Since you may have a large form and may not have the desire to target individual elements for Dijitization, you may want to use <code>dojo.behavior</code> to create Dijits by selector:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//require the class</span><br />
dojo.<span class="me1">require</span><span class="br0">&#40;</span><span class="st0">&#8216;dojo.behavior&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="co1">//when it has loaded&#8230;</span><br />
dojo.<span class="me1">ready</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//add two behaviors:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dojo.<span class="me1">behavior</span>.<span class="me1">add</span><span class="br0">&#40;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//find SELECT elements, make them a FilteringSelect Dijit</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8217;select&#8217;</span>: <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found: <span class="kw2">function</span><span class="br0">&#40;</span><span class="kw1">item</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> dijit.<span class="me1">form</span>.<span class="me1">FilteringSelect</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="coMULTI">/*options*/</span><span class="br0">&#125;</span>,<span class="kw1">item</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//find all radio buttons, make them dijit.form.RadioButton Dijits</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;input[type=&quot;radio&quot;]&#8216;</span>: <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found: <span class="kw2">function</span><span class="br0">&#40;</span><span class="kw1">item</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> dijit.<span class="me1">form</span>.<span class="me1">RadioButton</span><span class="br0">&#40;</span><span class="br0">&#123;</span> &nbsp;<span class="br0">&#125;</span>,<span class="kw1">item</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="coMULTI">/* ,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; more selector =&gt; Dijit creation&#8230;.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
<p>You could also run a few <code>dojo.query</code> searches for elements and loop through them to create new widgets, but the <code>dojo.behavior</code> method provides a cleaner syntax.</p>
<p><img src="http://www.sitepen.com/blog/wp-content/uploads/2010/07/DijitBlogPost4.jpg" alt="Dijit Form Controls" /><br />
<em>A Dijitized form; easily customizable and themeable.</em></p>
<h2>Accessing Dijit Widgets and Widget Properties</h2>
<p>Accessing a specific DOM element can be easily accomplished by using Dojo&#8217;s <code>byId</code> method.  Dijit features its own byId method which retrieves the Dijit widget registered within the widget system with the ID specified.  If the element to be made a Dijit has an ID, the widget ID will be that same value.  If the source element doesn&#8217;t have an ID attribute, a widget ID will be generated.  If we wanted to retrieve the widget object for the declaratively created &#8220;players&#8221; Dijit above, we would code:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//grab the players dijit.form.Filtering widget</span><br />
<span class="kw2">var</span> enhancedSelect = dijit.<span class="me1">byId</span><span class="br0">&#40;</span><span class="st0">&#8216;players&#8217;</span><span class="br0">&#41;</span>;</div>
<p><img src="http://www.sitepen.com/blog/wp-content/uploads/2010/07/DijitBlogPost1.jpg" alt="Dijit Form" /></p>
<p><em>View a listing of all properties and methods of a Dijit using dijit.byId within Firebug.</em></p>
<p>If we wanted to access the <code>pageSize</code> for which the Dijit widget was created from, we would code:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">var</span> pageSize = dijit.<span class="me1">byId</span><span class="br0">&#40;</span><span class="st0">&#8216;players&#8217;</span><span class="br0">&#41;</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">&#8216;pageSize&#8217;</span><span class="br0">&#41;</span>; <span class="co1">// returns 10</span></div>
<p>If we wanted to change the <code>pageSize</code> for the widget, we would code:</p>
<div class="dean_ch" style="white-space: wrap;">dijit.<span class="me1">byId</span><span class="br0">&#40;</span><span class="st0">&#8216;players&#8217;</span><span class="br0">&#41;</span>.<span class="me1">set</span><span class="br0">&#40;</span><span class="st0">&#8216;pageSize&#8217;</span>,<span class="nu0">20</span><span class="br0">&#41;</span>; <span class="co1">//now pageSize is 20</span></div>
<p><em>Note:  Dojo 1.5 introduces &#8220;get&#8221; and &#8220;set&#8221; methods to handle property values. In Dojo 1.4 and earlier, you would use &#8220;attr&#8221; in place of both get and set in the previous example.</em></p>
<h2>Listening to Widget Events</h2>
<p>Dijit widgets use Dojo&#8217;s native <code>connect</code> method to listen to events on the given widget:</p>
<div class="dean_ch" style="white-space: wrap;">dojo.<span class="me1">connect</span><span class="br0">&#40;</span>dijit.<span class="me1">byId</span><span class="br0">&#40;</span><span class="st0">&#8216;player&#8217;</span><span class="br0">&#41;</span>,<span class="st0">&#8216;onChange&#8217;</span>,<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//the user has selected a new item</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//do something</span></p>
<p><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
<p>Each widget supports a select number of events so be sure to view the documentation for the widget to ensure the event you&#8217;d like to listen to is supported.</p>
<h2>The Dijit Collection</h2>
<p>Dijit is also an incredible UI library that has the potential to enhance your website and save you an immense amount of time in doing so.  Dijit includes a large number of solid widgets including:</p>
<ul>
<li>Context, popup, and dropdown menus</li>
<li>Form element replacements like buttons, combo boxes, checkboxes, radio buttons, and text boxes</li>
<li>Date and time selection widgets</li>
<li>WYSIWYG Editors</li>
<li>Horizontal and Vertical Sliders</li>
<li>Progress Bars</li>
<li>Tabs and Accordions</li>
<li>Tree Structures (including Drag and Drop)</li>
<li>Dialogs and Tooltips</li>
<li>Layout widgets with slide controls and splitters</li>
</ul>
<p>And if Dijit doesn&#8217;t have a widget you want, there&#8217;s a good chance it&#8217;s available in DojoX (Dojo Extensions)! To see many of these widgets in action, visit the <a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dijit/themes/themeTester.html">Dojo Theme Tester</a>.</p>
<h2>Dijit Resources</h2>
<p>The following resources will help to aid your Dijit education:</p>
<ul>
<li><a href="http://www.dojotoolkit.org/api/dijit.html">Dijit API Documentation</a></li>
<li><a href="http://www.dojotoolkit.org/reference-guide/dijit/index.html#dijit-index">Dijit Reference Guide</a></li>
<li><a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dijit/tests/">Dojo Nightlies</a> (test files)</li>
<li><a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dijit/themes/themeTester.html">Dijit Theme Tester</a></li>
</ul>
<p>Stay tuned for more in-depth Dijit posts in the future.  This amazing library contains a vast array of useful widgets to enhance your website!</p>


<p>Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/08/11/dive-into-dijit-forms/' rel='bookmark' title='Permanent Link: Dive Into Dijit Forms'>Dive Into Dijit Forms</a></li><li><a href='http://www.sitepen.com/blog/2009/02/25/styling-dijit-form-elements/' rel='bookmark' title='Permanent Link: Styling Dijit Form Elements'>Styling Dijit Form Elements</a></li><li><a href='http://www.sitepen.com/blog/2009/11/05/dijit-prepackaged/' rel='bookmark' title='Permanent Link: Dijit: Prepackaged'>Dijit: Prepackaged</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/07/12/dive-into-dijit/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<series:name><![CDATA[Dive Into Dojo]]></series:name>
	</item>
		<item>
		<title>Creating and Enhancing Dojo Classes</title>
		<link>http://www.sitepen.com/blog/2010/07/01/creating-and-enhancing-dojo-classes/</link>
		<comments>http://www.sitepen.com/blog/2010/07/01/creating-and-enhancing-dojo-classes/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 07:02:14 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[api]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1317</guid>
		<description><![CDATA[Creating and Enhancing Dojo Classes
Like all top-notch JavaScript toolkits, Dojo tries to make its classes as flexible as possible, knowing that users of the toolkit may have different ideas about how a given class or class method should work.  Luckily, Dojo provides you a number of methods by which you can subclass or modify existing [...]


Related posts:<ol><li><a href='http://www.sitepen.com/blog/2008/06/24/creating-dojo-widgets-with-inline-templates/' rel='bookmark' title='Permanent Link: Creating Dojo Widgets with Inline Templates'>Creating Dojo Widgets with Inline Templates</a></li><li><a href='http://www.sitepen.com/blog/2008/05/19/creating-asynchronous-tests-with-the-dojo-objective-harness/' rel='bookmark' title='Permanent Link: Creating Asynchronous Tests with the Dojo Objective Harness'>Creating Asynchronous Tests with the Dojo Objective Harness</a></li><li><a href='http://www.sitepen.com/blog/2010/01/20/managing-widget-templates-in-dojo-1-4/' rel='bookmark' title='Permanent Link: Managing Widget Templates in Dojo 1.4'>Managing Widget Templates in Dojo 1.4</a></li></ol>]]></description>
			<content:encoded><![CDATA[<h1>Creating and Enhancing Dojo Classes</h1>
<p>Like all top-notch JavaScript toolkits, Dojo tries to make its classes as flexible as possible, knowing that users of the toolkit may have different ideas about how a given class or class method should work.  Luckily, Dojo provides you a number of methods by which you can subclass or modify existing classes.  Let&#8217;s examine a few ways you can make Dojo classes exactly the way you like.</p>
<p><span id="more-1317"></span></p>
<h2>Creating Dojo Subclasses</h2>
<p>The typical method by which you can create a Dojo class (or subclass) is by using the <code>dojo.declare</code> method.  The <code>dojo.declare</code> method registers your given class within its designated namespace, while also subclassing any number of classes passed in the method&#8217;s second argument.</p>
<p>The following code shows the basic method by which one can create a subclass:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//dojo.declare(&#8217;your.name.space&#8217;,superClass,{ customProperties });</span><br />
<span class="co1">//or</span><br />
<span class="co1">//dojo.declare(&#8217;your.name.space&#8217;,[superClass1,superClass2,superClass3],{ customProperties });</span></p>
<p>dojo.<span class="me1">provide</span><span class="br0">&#40;</span><span class="st0">&#8216;davidwalsh.Menu&#8217;</span><span class="br0">&#41;</span>;<br />
dojo.<span class="me1">declare</span><span class="br0">&#40;</span><span class="st0">&#8216;davidwalsh.Menu&#8217;</span>,dijit.<span class="me1">Menu</span>,<span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* your custom properties and methods here */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myCustomProperty: <span class="kw2">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; myCustomMethod: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* do stuff */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//all of dijit.Menu&#8217;s methods are also part of this new class</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>The new class above, <code>davidwalsh.Menu</code>, is a shiny new custom Dojo class that inherits all methods and properties of the <code>dijit.Menu</code> class.  My new class also features a new custom property and a new custom method which can do anything I want.  Now that we know how to create a subclass, let&#8217;s create a realistic example of a useful subclass:  <code>davidwalsh.Menu</code>.</p>
<div class="dean_ch" style="white-space: wrap;">
dojo.<span class="me1">provide</span><span class="br0">&#40;</span><span class="st0">&#8216;davidwalsh.Menu&#8217;</span><span class="br0">&#41;</span>;<br />
dojo.<span class="me1">declare</span><span class="br0">&#40;</span><span class="st0">&#8216;davidwalsh.Menu&#8217;</span>,dijit.<span class="me1">Menu</span>,<span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//new option</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; allowSubmenuHover: <span class="kw2">true</span>,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//another new option</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; popupDelay: <span class="nu0">500</span>,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//override the dijit.Menu method</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onItemHover: <span class="kw2">function</span><span class="br0">&#40;</span><span class="kw1">item</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">isActive</span> || <span class="kw1">this</span>.<span class="me1">allowSubmenuHover</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">focusChild</span><span class="br0">&#40;</span><span class="kw1">item</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//use the new settings to trigger popup</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">focusedChild</span>.<span class="me1">popup</span> &amp;&amp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; !<span class="kw1">this</span>.<span class="me1">focusedChild</span>.<span class="me1">disabled</span> &amp;&amp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; !<span class="kw1">this</span>.<span class="me1">hover_timer</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">hover_timer</span> = setTimeout<span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dojo.<span class="me1">hitch</span><span class="br0">&#40;</span><span class="kw1">this</span>, <span class="st0">&#8216;_openPopup&#8217;</span><span class="br0">&#41;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">popupDelay</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">focusedChild</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">focusChild</span><span class="br0">&#40;</span><span class="kw1">item</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>._hoveredChild = <span class="kw1">item</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p><span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p><code>davidwalsh.Menu</code> is an enhancement of the original <code>dijit.Menu</code> class; it features two new options and overrides a <code>dijit.Menu</code> method with an end goal of providing a Menu whose PopupMenuItem opens when hovered over instead of clicked.</p>
<p>You may be wondering how to call methods of a superclass from your new subclass.  That&#8217;s simple too:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//more methods above</span><br />
someMethod: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* do anything you want here */</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// call the superclass&#8217; &quot;someMethod&quot; method </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// to execute superclass&#8217; original functionality</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> result = <span class="kw1">this</span>.<span class="me1">inherited</span><span class="br0">&#40;</span>arguments<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* do anything you want here */</span><br />
<span class="br0">&#125;</span><br />
<span class="co1">//more methods below</span><br />
&nbsp;</div>
<p>As you can see, creating subclasses is a breeze!  But what can you do if you simply want to modify an existing Dojo class?  Monkey patch it!</p>
<p><!-- ACT TWO --></p>
<h2>Prototype Modification or &#8220;Monkey Patching&#8221;</h2>
<p>Sometimes subclassing existing Dojo classes isn&#8217;t the best option or an option at all.  You may find yourself in the position where you can only patch an existing Dojo install;  in that case, monkey patching is the ideal strategy.  Monkey patching is the process by which you modify the prototype of an existing object (in our case a Dojo class).  Positives of using a monkey patch approach are:</p>
<ul>
<li>All existing objects of this type are instantly changed.</li>
<li>You don&#8217;t need access to the core Dojo files.</li>
<li>Since you aren&#8217;t modifying the core Dojo files themselves, upgrading your Dojo builds will be exponentially easier as you wont have to hunt for your past changes.</li>
<li>Your patches are more portable as they aren&#8217;t placed directly in the core Dojo files themselves.</li>
</ul>
<p>The following code shows the monkey patching format:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//save the old prototype method</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> oldPrototypeSomeMethod = <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dijit.<span class="me1">someDijit</span>.<span class="me1">prototype</span>.<span class="me1">someMethod</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//modify the prototype</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dijit.<span class="me1">someDijit</span>.<span class="me1">prototype</span>.<span class="me1">someMethod</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* some new stuff here */</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// call the old method *only if you </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// want to keep some of the original functionality*</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; oldPrototypeSomeMethod.<span class="me1">call</span><span class="br0">&#40;</span><span class="kw1">this</span>, arguments<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Now let&#8217;s take a look at a realistic example.  I was recently working with the FilteringSelect Dijit when I noticed that if the first <code>option</code> element within the srcNode (<code>select</code> element) has no value (an empty string value attribute), the element&#8217;s label will not display.  A very odd bug and definitely not a desired result.  What I was able to do was patch the Dijit&#8217;s <code>postMixInProperties'</code> method to fix the problem.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> dffsp = dijit.<span class="me1">form</span>.<span class="me1">FilteringSelect</span>.<span class="me1">prototype</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//save the old prototype method</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> oldPMIP = dffsp.<span class="me1">postMixInProperties</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//modify the Dijit&#8217;s prototype</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dffsp.<span class="me1">postMixInProperties</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//if this select has no value and the first option is blank:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//set the displayedValue of this dijit to that label initially</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!<span class="kw1">this</span>.<span class="me1">store</span> &amp;amp;&amp;amp; <span class="kw1">this</span>.<span class="me1">srcNodeRef</span>.<span class="me1">value</span> == <span class="st0">&#8221;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> srcNodeRef = <span class="kw1">this</span>.<span class="me1">srcNodeRef</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nodes = dojo.<span class="me1">query</span><span class="br0">&#40;</span><span class="st0">&quot;&gt; option[value='']&quot;</span>, srcNodeRef<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>nodes.<span class="me1">length</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">displayedValue</span> = <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dojo.<span class="me1">trim</span><span class="br0">&#40;</span>nodes<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="me1">innerHTML</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// call the original prototype method;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// we still want the original functionality to fire</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; oldPMIP.<span class="me1">call</span><span class="br0">&#40;</span><span class="kw1">this</span>, arguments<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>That&#8217;s just one great example of monkey patching.  While monkey patching may not look like the most beautiful technique, it&#8217;s an essential part of patching your Dojo installs.</p>
<h2>Extending Dojo Classes</h2>
<p>The <code>dojo.extend</code> method allows us to add new methods to the class&#8217; prototype, thus providing the new methods to every instance of a given class.  If a method name is passed to <code>dojo.extend</code> that already exists for a given class, the new method overrides the original method.</p>
<p>The following code illustrates extending the <code>dijit.Menu</code> class, allowing popup menu items to open during its label&#8217;s hover event in addition to the click event.</p>
<div class="dean_ch" style="white-space: wrap;">
dojo.<span class="me1">extend</span><span class="br0">&#40;</span>dijit.<span class="me1">Menu</span>,<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; allowSubmenuHover: <span class="kw2">true</span>, <span class="co1">//new setting</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; popupDelay: <span class="nu0">500</span>, <span class="co1">//new setting</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onItemHover: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co1">//overriding original method</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">isActive</span> || <span class="kw1">this</span>.<span class="me1">allowSubmenuHover</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">focusChild</span><span class="br0">&#40;</span><span class="kw1">item</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//use the new settings to trigger popup</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">focusedChild</span>.<span class="me1">popup</span> &amp;&amp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; !<span class="kw1">this</span>.<span class="me1">focusedChild</span>.<span class="me1">disabled</span> &amp;&amp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; !<span class="kw1">this</span>.<span class="me1">hover_timer</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">hover_timer</span> = setTimeout<span class="br0">&#40;</span>dojo.<span class="me1">hitch</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>, <span class="st0">&#8216;_openPopup&#8217;</span><span class="br0">&#41;</span>, <span class="kw1">this</span>.<span class="me1">popupDelay</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">focusedChild</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">focusChild</span><span class="br0">&#40;</span><span class="kw1">item</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>._hoveredChild = <span class="kw1">item</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
<p>Note that the original <code>onItemHover</code> method isn&#8217;t saved to be executed later &#8212; the entire prototype has been rewritten.  Essentially we&#8217;re rubbishing the original functionality of this method.  Now we have a <code>dijit.Menu</code> class that conforms to our needs and doesn&#8217;t interfere with our Dojo build itself.</p>
<h2>To Subclass, Extend, or Monkey Patch?</h2>
<p>There are no hard and fast rules for when to extend a class or monkey patch a class.  I do have a few recommendations though:</p>
<ul>
<li>Do *not* change one of the core Dojo files; monkey patch or extend.</li>
<li>Monkey patch the class if you need access to the original prototype.</li>
<li>Subclass with your own namespace when looking to share code between projects.</li>
<li>If portability is an important goal, extend the class.</li>
</ul>
<h2>Extend Away!</h2>
<p>Extending Dojo classes is the perfect way to fix bugs, enhance native Dojo classes, and prevent you from needing to repeat code. The limitations of Dojo are only those that you put on it!</p>


<p>Related posts:<ol><li><a href='http://www.sitepen.com/blog/2008/06/24/creating-dojo-widgets-with-inline-templates/' rel='bookmark' title='Permanent Link: Creating Dojo Widgets with Inline Templates'>Creating Dojo Widgets with Inline Templates</a></li><li><a href='http://www.sitepen.com/blog/2008/05/19/creating-asynchronous-tests-with-the-dojo-objective-harness/' rel='bookmark' title='Permanent Link: Creating Asynchronous Tests with the Dojo Objective Harness'>Creating Asynchronous Tests with the Dojo Objective Harness</a></li><li><a href='http://www.sitepen.com/blog/2010/01/20/managing-widget-templates-in-dojo-1-4/' rel='bookmark' title='Permanent Link: Managing Widget Templates in Dojo 1.4'>Managing Widget Templates in Dojo 1.4</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/07/01/creating-and-enhancing-dojo-classes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JSGI vs Connect for Node Middleware</title>
		<link>http://www.sitepen.com/blog/2010/06/11/jsgi-vs-connect-for-node-middleware/</link>
		<comments>http://www.sitepen.com/blog/2010/06/11/jsgi-vs-connect-for-node-middleware/#comments</comments>
		<pubDate>Fri, 11 Jun 2010 23:08:35 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[CommonJS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Persevere]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1291</guid>
		<description><![CDATA[JSGI (JavaScript gateway interface) is the broadly adopted standard for server-side JavaScript web applications and middleware. JSGI is designed specifically for ease of use in asynchronous, evented environments, and consequently JSGI has been a perfect fit building applications on Node. JSGI-Node has existed for sometime as lightweight JSGI server/adapter for running JSGI-based applications on Node. [...]


Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/03/04/pintura-jsgi-modules/' rel='bookmark' title='Permanent Link: Pintura JSGI Modules'>Pintura JSGI Modules</a></li><li><a href='http://www.sitepen.com/blog/2010/01/19/commonjsjsgi-the-emerging-javascript-application-server-platform/' rel='bookmark' title='Permanent Link: CommonJS/JSGI: The Emerging JavaScript Application Server Platform'>CommonJS/JSGI: The Emerging JavaScript Application Server Platform</a></li><li><a href='http://www.sitepen.com/blog/2010/07/19/real-time-comet-applications-on-node-with-tunguska/' rel='bookmark' title='Permanent Link: Real-time Comet Applications on Node with Tunguska'>Real-time Comet Applications on Node with Tunguska</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://wiki.commonjs.org/wiki/JSGI/Level0/A">JSGI</a> (JavaScript gateway interface) is the broadly adopted standard for server-side JavaScript web applications and middleware. JSGI is designed specifically for ease of use in asynchronous, evented environments, and consequently JSGI has been a perfect fit building applications on Node. <a href="http://github.com/kriszyp/jsgi-node">JSGI-Node</a> has existed for sometime as lightweight JSGI server/adapter for running JSGI-based applications on Node. However, &#8220;<a href="http://github.com/extjs/Connect">Connect</a>&#8221; was recently released as a new alternate middleware system for Node. This spurred me to make sure JSGI-Node was optimized, and compare the Connect middleware design to JSGI.</p>
<p><span id="more-1291"></span></p>
<h2>Performance</h2>
<p>To begin with, I did some performance comparisons. The first test is the simplest possible &#8220;hello world&#8221; application. This was run against a mock server to eliminate any overhead of HTTP parsing and isolate the test to simple request delegation. Here are the results in requests per second:</p>
<p>JSGI-Node: 730K/s<br />
Connect: 204K/s</p>
<p>Here JSGI-Node is shown to be about 257% faster. Next, I created a stack of 9 middleware apps in front of the &#8220;hello world&#8221; app. These middleware apps do nothing but delegate to the next layer. This was designed to isolate the middleware delegation mechanism. The results are in middleware delegations per second:</p>
<p>JSGI-Node: 80,600K/s<br />
Connect: 2,230K/s</p>
<p>Middleware delegation is over 3000% faster with JSGI than Connect! Why? JSGI is based on a simple, lightweight, pure JavaScript approach to connecting middleware. Middleware apps are simple function closures that accept another app that they can delegate to. Nothing is actually required to wire middleware together. Connect on the other hand has to go through a heavy delegation layer to wire the middleware together. Middleware delegation might not pose a significant bottleneck for simple applications, but as middleware layers accumulate, the speed of delegation becomes increasingly important.</p>
<p>Putting these test together, the full request delegation and middleware delegation through nine middleware layers to a simple &#8220;hello world&#8221; app results in following requests per second:</p>
<p>JSGI-Node: 675K/s<br />
Connect: 112K/s</p>
<p>With JSGI providing about a 503% better performance with isolated request and middleware delegation.</p>
<p>Here are the <a href='http://www.sitepen.com/blog/wp-content/uploads/2010/06/jsgi-connect1.zip'>test files</a>.</p>
<h2>Flexibilty</h2>
<p>One of the powerful aspects of JSGI is that middleware is connected by simply passing apps as arguments. This means you can not only create linear stacks, but you can create rich branching tree type request routing structures. Connect on the other hand is really only designed for creating linear middleware stacks. This is useful for demonstrating simple middleware integration, but often real applications require much more sophisticated logic than these simple stacks. While Connect comes with routing middleware, it is not clear if it is possible to create middleware stacks under the different routes (especially with the same ease as JSGI). </p>
<h2>Layer Isolation</h2>
<p>One of the key concepts of middleware is layering. JSGI provides a simple mechanism for allow each layer to provide a controlled view of the request for the next layer and control the response from that layer. On the other hand, Connect does not provide any mechanism for wrapping and creating new request objects or handling response objects. Altering the view for the next layer means actually altering the request and response objects in place. This fundamentally breaks the layering concept of middleware because when the request object is altered, it is altered across all layers, below and above, not just for the next layer.</p>
<h2>Ease of use</h2>
<p>Ease of use is in the eye of the beholder, so I will just show so code comparisons. Here is a sample of how you create middleware with JSGI:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">function</span> Middleware<span class="br0">&#40;</span>nextApp<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; <span class="kw1">return</span> <span class="kw2">function</span><span class="br0">&#40;</span>request<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> response = nextApp<span class="br0">&#40;</span>request or our own request object<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> response or any modified response we want to <span class="kw1">return</span>;<br />
&nbsp; <span class="br0">&#125;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="co1">// now wire it up</span><br />
jsgiNode.<span class="me1">start</span><span class="br0">&#40;</span>Middleware<span class="br0">&#40;</span>app<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>And in Connect:</p>
<div class="dean_ch" style="white-space: wrap;">
Middleware = <span class="br0">&#123;</span><br />
&nbsp; handle: <span class="kw2">function</span><span class="br0">&#40;</span>req, res<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// must modify req in place</span><br />
&nbsp; &nbsp; <span class="co1">// modify res handlers in place as well</span><br />
&nbsp; &nbsp; next<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="co1">// restore any res handlers that might be used lower down in the stack</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span>;<br />
<span class="co1">// now wire it up</span><br />
<span class="kw2">var</span> connectServer = <span class="kw2">new</span> Server<span class="br0">&#40;</span><span class="br0">&#91;</span><br />
&nbsp; <span class="br0">&#123;</span>module: Middleware<span class="br0">&#125;</span>,<br />
&nbsp; <span class="br0">&#123;</span>module: app<span class="br0">&#125;</span><br />
<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
connectServer.<span class="me1">listen</span><span class="br0">&#40;</span>&#8230;<span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Pintura has a wealth of <a href="http://github.com/kriszyp/pintura/tree/master/lib/jsgi/">examples of other middleware applications</a>.</p>
<p>One complaint that has been made about JSGI is that it was not designed for specifically for Node. However, it was designed for asynchronous event-oriented architecture, which is exactly the point of Node, and so there really is no credibility to discounting JSGI for not being Node specific. It is a perfect fit for Node.</p>
<h2>Middleware Availability</h2>
<p>Simply take a look at the rapidly growing list of <a href="http://wiki.github.com/ry/node/modules#middleware">middleware modules for Node</a> to see what is available for JSGI. JSGI is based on a collaborative effort of large number of participants in the CommonJS group. Furthermore, JSGI-Node is licensed under the Dojo foundation, which has the known for rock solid CLA-protected IP-encumberance-free liberally licensed software with a track record of being safe to use.</p>
<p>Now to be fair, Connect is about more than just inventing yet another middleware interface. It has some other extremely useful features including a powerful command line startup mechanism and middleware configuration techniques. In fact, Tim Caswell has actually talked about possibly including support for JSGI in Connect. This would be a great addition, and would allow Connect to actually combine the superior JSGI middleware interface with the other cool features in Connect.</p>
<p>The elegant design of JSGI, based on simple idiomatic JavaScript, allows for simple, intuitive middleware, with flexible connections, incredible performance, and connected to a growing ecosystem of reusable appliances.</p>


<p>Related posts:<ol><li><a href='http://www.sitepen.com/blog/2010/03/04/pintura-jsgi-modules/' rel='bookmark' title='Permanent Link: Pintura JSGI Modules'>Pintura JSGI Modules</a></li><li><a href='http://www.sitepen.com/blog/2010/01/19/commonjsjsgi-the-emerging-javascript-application-server-platform/' rel='bookmark' title='Permanent Link: CommonJS/JSGI: The Emerging JavaScript Application Server Platform'>CommonJS/JSGI: The Emerging JavaScript Application Server Platform</a></li><li><a href='http://www.sitepen.com/blog/2010/07/19/real-time-comet-applications-on-node-with-tunguska/' rel='bookmark' title='Permanent Link: Real-time Comet Applications on Node with Tunguska'>Real-time Comet Applications on Node with Tunguska</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/06/11/jsgi-vs-connect-for-node-middleware/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
