<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Otaqui.Com &#187; dojo</title>
	<atom:link href="http://otaqui.com/blog/tag/dojo/feed/" rel="self" type="application/rss+xml" />
	<link>http://otaqui.com/blog</link>
	<description>Pete Otaqui's blog about web development and everything else</description>
	<lastBuildDate>Tue, 31 Jan 2012 23:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>CSSP: Loading CSS with Javascript &#8211; and getting an onload callback.</title>
		<link>http://otaqui.com/blog/890/cssp-loading-css-with-javascript-and-getting-an-onload-callback/</link>
		<comments>http://otaqui.com/blog/890/cssp-loading-css-with-javascript-and-getting-an-onload-callback/#comments</comments>
		<pubDate>Tue, 28 Sep 2010 14:16:18 +0000</pubDate>
		<dc:creator>pete</dc:creator>
				<category><![CDATA[Professional]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">http://otaqui.com/blog/?p=890</guid>
		<description><![CDATA[It seems fairly straightforward to require CSS with Javascript. The most obvious method that I thought of was creating a &#60;link&#62; tag and appending it to the head of the document. An alternative would be to load the text of the css file with an ajax XHR call, and then inject that into a &#60;style&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>It seems fairly straightforward to require CSS with Javascript.  The most obvious method that I thought of was creating a &lt;link&gt; tag and appending it to the head of the document. An alternative would be to load the text of the css file with an ajax XHR call, and then inject that into a &lt;style&gt; tag.</p>
<p>These are both fine for most cases, but what about a situation where you have a hard dependency on the css for the javascript to work correctly?  You could take a best-guess at a wait time for the CSS to load, or even set the XHR to be synchronous &#8211; however both of these are very poor for both actual and perceived performance of your page (and the former may simply fail).</p>
<p>When might this be the case?  Let&#8217;s say you have some asynchronously loaded javascript that is intended to create a widget on the page, something like this (which assumes you are using jQuery as your dom library):</p>
<pre class="brush: javascript">

function moveLargePanels() {
    // make a panel container for large panels
    $(&#039;&lt;div id=&quot;largePanelContainer&quot;&gt;&lt;/div&gt;&#039;)
        .appendTo(&#039;body&#039;);
    $(&#039;.panel&#039;).each( function(i, panel) {
        if ( panel.outerWidth() &gt; 300 ) {
            panel.appendTo(&#039;#largePanelContainer&#039;);
        }
    }
}
</pre>
<p>And some corresponding CSS:</p>
<pre class="brush: css">
.panel {
    border:1px solid #000;
    padding: 20px;
}
</pre>
<p>The idea here is that, since there is some interaction between the logic in the javascript (a test for the &#8220;outerWidth&#8221;) and the styling in the CSS (setting the padding) that the javascript can only work correctly once the CSS has been loaded and applied.</p>
<p>This should be fine right?  Start the javascript and the css loading, and have events attached to the load of each letting you know when everything is ready and it&#8217;s safe to fire &#8220;moveLargePanels()&#8221;.  Wrong, or at least tricky, and essentially impossible if the CSS is loaded from a different domain to the hosting webpage.</p>
<p>You can get James Burke&#8217;s excellent description of this problem here:<br />
<a href="http://requirejs.org/docs/faq-advanced.html#css">http://requirejs.org/docs/faq-advanced.html#css</a><br />
And a first pass at solving this (in some browsers) here:<br />
<a href="http://bugs.dojotoolkit.org/ticket/5402">http://bugs.dojotoolkit.org/ticket/5402</a></p>
<p>Burke mentions a using a &#8220;well known&#8221; style rule to test whether the style is loaded.  I suggest a standard pattern for the rule, and also a mechanism for dynamically setting it.</p>
<h2>Enter CSSP</h2>
<p>You may have heard of <a href="http://remysharp.com/2007/10/08/what-is-jsonp/">JsonP</a> &#8211; a way of serving dynamically generated JSON which is padded with a method call (the method name is supplied in the query string of the URL), which allows for cross domain loading of javascript.  The idea behind CSSP is similar &#8211; it defines a pattern for loading css cross domain.  Instead of wrapping in a callback though, we can use the URL query string mechanism to supply the special &#8220;test rule&#8221; that we use.</p>
<h3>Dynamic Rule Pattern</h3>
<p>Given this url:</p>
<p>http://someserver.com/stylefile.css?cssp=123456</p>
<pre class="brush: php">
.panel { ... }
.largePanelsContainer { .. }

/* and the special rule: */
cssp { zIndex:123456; }
</pre>
<p>So, in whatever loader we want to build, we can add something like this (n.b. this isn&#8217;t tested code):</p>
<pre class="brush: javascript">
var cssp_counter = 0;
function loadCss(url, callback, className, zIndex) {
    // create a counter for special class names, so they are unique
    className = className || &#039;cssp&#039; + (cssp_counter++);
    zIndex = zIndex || 123456;
    url = url + &#039;?&#039; + className + &#039;=&#039; + zIndex;
    $(&#039;&lt;link&gt;&#039;)
        .attr({
            rel: &quot;stylesheet&quot;,
            type: &quot;text/css&quot;,
            href: url
        .appendTo(&#039;head&#039;);
    // append a dummy div to the body for the test
    var div = $(&#039;&lt;div&gt;&lt;/div&gt;&#039;)
        .addClass(className)
        .appendTo(&#039;body&#039;);
    // now poll for the z-index value:
    var cssp_interval = setInterval( function() {
        // if the zIndex is applied, we know the css has loaded
        if ( div.css(&#039;zIndex&#039;) == zIndex ) {
            div.remove();
            // callback:
            callback();
            clearInterval( cssp_interval );
        }
    }, 100);

}
</pre>
<p>The code above to do the loading and fire a callback is very naive.  It would be much better to have an event that can have listeners added, and to have some tests and some failure management like a maximum timeout.</p>
<h3>Default Rule Pattern</h3>
<p>This obviously presupposes that the CSS is dynamically generated to some degree, even just by adding the special rule to an otherwise static file.  I think that an alternative to the performance-conscious would be a &#8220;default&#8221; special rule, based on the css filename.  This needs some thought, and could well be tied to a build-step in your deployment process (you do have one, right?).  The pattern comes in two parts:</p>
<ul>
<li>There is a standard zIndex that is always used.</li>
<li>The filename includes the &#8220;special class name&#8221; that is used within.</li>
</ul>
<p>For example:</p>
<p>Given the file: <strong>styles.fhddgso9j.css</strong></p>
<pre class="brush: css">
.someclass { ... }
.otherclass { ... }

/* and the special rule */
.fhddgso9j { z-index: 123456; }
</pre>
<p>The javascript to go alone with this might look something like:</p>
<pre class="brush: javascript">
var cssp_counter = 0;
function loadCss(url, callback) {
    // create a counter for special class names, so they are unique
    className = getSpecialClassName(url);
    zIndex = 123456;
    $(&#039;&lt;link&gt;&#039;)
        .attr({
            rel: &quot;stylesheet&quot;,
            type: &quot;text/css&quot;,
            href: url
        .appendTo(&#039;head&#039;);
    // append a dummy div to the body for the test
    var div = $(&#039;&lt;div&gt;&lt;/div&gt;&#039;)
        .addClass(className)
        .appendTo(&#039;body&#039;);
    // now poll for the z-index value:
    var cssp_interval = setInterval( function() {
        // if the zIndex is applied, we know the css has loaded
        if ( div.css(&#039;zIndex&#039;) == zIndex ) {
            div.remove();
            // callback:
            callback();
            clearInterval( cssp_interval );
        }
    }, 100);
}
function getSpecialClassName(url) {
  // get just the filename, e.g. &quot;styles.9ufosdfij.css&quot;
  var filename = url.substring( url.lastIndexOf(&#039;/&#039;)+1, url.length);
  // split on the &quot;.&quot; marks
  var parts = filename.split(&#039;.&#039;);
  // assume it will be the second-to-last part (since the last should be &quot;css&quot;)
  var className = parts[ parts.length-2 ];
  return className;
}
</pre>
<p>As noted above, this naive, untested code.</p>
<h2>Wrapping Up</h2>
<p>Ideally I&#8217;d like any &#8220;css loader&#8221; to use actual browser events (or properties) where possible, and to fallback on this setup as a last resort.</p>
<p>If I get time I&#8217;ll implement this and put the code on github.  Please do get in touch if you&#8217;d like to pair on this!</p>
]]></content:encoded>
			<wfw:commentRss>http://otaqui.com/blog/890/cssp-loading-css-with-javascript-and-getting-an-onload-callback/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Dojo Toolkit Book Reviews &#8211; &#8216;Dojo: The Definitive Guide&#8217;, &#8216;Mastering Dojo&#8217; and &#8216;Dojo: Using The Dojo Javascript Library To Build Ajax Applications&#8217;</title>
		<link>http://otaqui.com/blog/96/dojo-toolkit-book-reviews-dojo-the-definitive-guide-mastering-dojo/</link>
		<comments>http://otaqui.com/blog/96/dojo-toolkit-book-reviews-dojo-the-definitive-guide-mastering-dojo/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 14:55:42 +0000</pubDate>
		<dc:creator>pete</dc:creator>
				<category><![CDATA[Professional]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">http://otaqui.com/wp/2008/06/dojo-toolkit-book-reviews-dojo-the-definitive-guide-mastering-dojo/</guid>
		<description><![CDATA[Having been working extensively with the Dojo Toolkit, and found their documentation to be extremely useful &#8211; but not exhaustive. I was happy therefore to be able to get hold of the following of pre-release books about the toolkit: Dojo: The Definitive Guide Mastering Dojo Dojo: Using the Dojo JavaScript Library to Build AJAX Applications [...]]]></description>
			<content:encoded><![CDATA[<p>Having been working extensively with the <a href="http://dojotoolkit.org">Dojo Toolkit</a>, and found their <a href="http://doojtoolkit.org/docs">documentation</a> to be extremely useful &#8211; but not exhaustive. I was happy therefore to be able to get hold of the following of pre-release books about the toolkit:</p>
<ul>
<li><a href="http://oreilly.com/catalog/9780596516482/">Dojo: The Definitive Guide</a></li>
<li><a href="http://pragprog.com/titles/rgdojo/mastering-dojo">Mastering Dojo</a></li>
<li><a href="http://safari.oreilly.com/9780321563132">Dojo: Using the Dojo JavaScript Library to Build AJAX Applications</a></li>
</ul>
<p>All three books cover the core dojo library and dijit gui controls and also associated tools like the build system.</p>
<h3>Dojo: The Definitive Guide</h3>
<p><img src="http://otaqui.com/wp/wp-content/uploads/dojo_tdg.jpg" alt="Dojo: The Definitive Guide" style="float:left; margin-right:10px; margin-bottom:10px;" /></p>
<p>If I ever want to get a book on a subject and there is an O&#8217;Reilly Definitive Guide, I will choose that one &#8211; mostly based on having the Javascript and Actionscript equivalents.  I have to say though that I felt a bit let down with this version, as <acronym title="Dojo: The Definitive Guide">DTDG</acronym> isn&#8217;t as comprehensive as I&#8217;m used to from the series.  While the coverage is broad (more so than the other books in this review), it isn&#8217;t all that deep in some key areas &#8211; missing out on some of the gotcha&#8217;s that I have found indispensable when working with Dojo.</p>
<p>I should make it clear that book is very clearly written with clean and helpful examples.  It is also written with the clear intention of readers people build solid web applications beyond using Dojo.</p>
<p>DTDG does go into excellent detail about the Dojo environment &#8211; bootstrapping, the build system, the dijit life-cycle, browser utilities, OOP with Dojo, Event management and the Publish / Subscribe mechanism, Ajax / JSON / JSONP / JSON-RPC, and more besides.  I think few people who aren&#8217;t Dojo committers could read and grok the book without gaining considerable insight.
<p>This puts the book in context &#8211; it isn&#8217;t really for the average developer who wants to get an application up-and-running with Dojo.  I would say it is for someone who wants a deeper understanding of the structure of the library, rather than how to hack something together with it.  A good example is the explanation of dojo.byId(), a clear detailing of the vagaries of document.getElementById() &#8211; and why the former is more useful (however I&#8217;ll leave you to go and buy the book to find out the specifics ;).</p>
<h3 style="clear:left;">Mastering Dojo</h3>
<p><img src="http://otaqui.com/wp/wp-content/uploads/mastering_dojo.jpg" alt="Mastering Dojo" style="float:left; margin-right:10px; margin-bottom:10px;" /></p>
<p>I would recommend this book without hesitation.  I found it informative, helpful and really on-point while trying to create a fairly heavy application on top of Dojo.  Written in the usual Pragmatic Programmer style, it is as easy to read through a chapter as it is to dig into for a specific answer.</p>
<p>Perhaps one omission is the <acronym title="Dojo Objective Harness">DOH</acronym> (which the book incorrectly refers to as the Dojo Object Handler) &#8211; perhaps correctly citing the fact that it is out of it&#8217;s own scope.  Given that DOH is dojo agnostic (your project does need to use Dojo to be tested with DOH) I suppose that is fair enough, but I would love to have seen even an introductory chapter on it.</p>
<p>Mastering Dojo is structured in a slightly confusing way &#8211; confusing at least if you are used to The Book Of Dojo and it&#8217;s well worth searching for terms in the index (unless you&#8217;re reading a PDF!) when the chapter titles don&#8217;t look as though they contain what you need.</p>
<h3 style="clear:left;">Dojo: Using the Dojo JavaScript Library to Build AJAX Applications</h3>
<p><img src="http://otaqui.com/wp/wp-content/uploads/dojo_utdjsltbaa.jpg" alt="Dojo: Using the Dojo JavaScript Library to Build AJAX Applications" style="float:left; margin-right:10px; margin-bottom:10px;" /></p>
<p>I&#8217;m afraid to say that I wasn&#8217;t especially enamored of this book &#8211; from its tongue-tangling and strangely-chosen title onwards.  I felt that it missed a considerable amount about the basics of dojo &#8211; which is understandable given its title &#8211; without really giving enough detail or insight into building an RIA &#8211; which is not.</p>
<p>Using Dojo (the best shortening I can come up with) does give a reasonable introduction to dojo, and covers some of the fundamental forms of usage, but it really isn&#8217;t comparable to either of the previously covered titles.</p>
<p><em>Update 2008-07-03 : </em> Alex Russell, one of the core dojo committers has also <a href="http://alex.dojotoolkit.org/?p=682">reviewed these books</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://otaqui.com/blog/96/dojo-toolkit-book-reviews-dojo-the-definitive-guide-mastering-dojo/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Reacting To Events with the Dojo Dijit Editor</title>
		<link>http://otaqui.com/blog/95/reacting-to-events-with-the-dojo-dijit-editor/</link>
		<comments>http://otaqui.com/blog/95/reacting-to-events-with-the-dojo-dijit-editor/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 15:15:47 +0000</pubDate>
		<dc:creator>pete</dc:creator>
				<category><![CDATA[Professional]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">http://otaqui.com/wp/2008/06/reacting-to-events-with-the-dojo-dijit-editor/</guid>
		<description><![CDATA[I was writing a custom set of validation routines for a form, and using Dojo&#8217;s Dijit.Editor component &#8211; and having some trouble attaching my &#8220;invalid&#8221; class and a tooltip to a &#8216;required&#8217; editor. There were several parts to the problem: Iframe Transparency in IE Targeting the containing div node, the editor&#8217;s iframe tag, and that [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing a custom set of validation routines for a form, and using Dojo&#8217;s Dijit.Editor component &#8211; and having some trouble attaching my &#8220;invalid&#8221; class and a tooltip to a &#8216;required&#8217; editor.</p>
<p>There were several parts to the problem:</p>
<ul>
<li>Iframe Transparency in IE</li>
<li>Targeting the containing div node, the editor&#8217;s iframe tag, and that iframe&#8217;s body node</li>
<li>Ignoring the weird default content that Dojo, or possibly the browser itself, adds to the Editor (a &lt;br /&gt; tag, but with a special attribute in Mozilla browsers)</li>
<li>Removing the class once the user makes a change to the content</li>
</ul>
<p>Ignoring all the rest, and a lot of the specifics (since I&#8217;m sure there are better ways to achieve all this) I&#8217;m just going to note how to get use dojo.connect to respond to a &#8216;click&#8217; inside the editor area.  It&#8217;s worth noting that using the normal method&#8217;s you would expect <em>sort of</em> work &#8211; but only when you click on the Editor&#8217;s toolbar rather than the inside the editable area.  That being said, here you go:</p>
<p><code><br />
&lt;textarea dojoType="dijit.Editor" id="dijitEditor">Click Me&lt;/textarea&gt;<br />
&lt;script&gt;<br />
dojo.addOnLoad(function() {<br />
&nbsp;var dijitEditor = dijit.byId('dijitEditor');<br />
&nbsp;var eBody = dijitEditor.iframe.contentDocument.body;<br />
&nbsp;dojo.connect(eBody,'click',onEditorClick);<br />
});<br />
function onEditorClick() {<br />
&nbsp;alert('you clicked the editor window');<br />
}<br />
&lt;/script&gt;<br />
</code></p>
<p><strong>NB &#8211; Not IE-friendly</strong>.  This won&#8217;t work in IE because of the contentDocument property, which it doesn&#8217;t support &#8211; but you could use the document.frames[] array instead.  Note that the iframe&#8217;s id is widgetid+&#8217;_iframe&#8217; in all except Mozilla.  So something like this:</p>
<p><code><br />
dojo.addOnLoad(function() {<br />
&nbsp;var dijitEditor = dijit.byId('dijitEditor');<br />
&nbsp;var eBody;<br />
&nbsp;if ( dojo.isIE ) {<br />
&nbsp;&nbsp;eBody = document.frames['dijitEditor_iframe'].body;<br />
&nbsp;} else {<br />
&nbsp;&nbsp;eBody = dijitEditor.iframe.contentDocument.body;<br />
&nbsp;}<br />
&nbsp;dojo.connect(eBody,'click',onEditorClick);<br />
});<br />
</code></p>
<p>I really went down the rabbit hole for while chasing &#8216;ondijitclick&#8217; and the &#8216;events&#8217; / &#8216;captureEvents&#8217; properties of the Dijit.Editor, but ended up using this fairly straightforward custom method.</p>
]]></content:encoded>
			<wfw:commentRss>http://otaqui.com/blog/95/reacting-to-events-with-the-dojo-dijit-editor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

