<?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"
	>

<channel>
	<title>Rule52</title>
	<atom:link href="http://rule52.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://rule52.com</link>
	<description>Just another WordPress weblog</description>
	<pubDate>Tue, 20 Dec 2011 17:18:58 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Sass and Sprockets for Rails 3.1</title>
		<link>http://rule52.com/2011/06/sass-and-sprockets-for-rails-31/</link>
		<comments>http://rule52.com/2011/06/sass-and-sprockets-for-rails-31/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 00:39:22 +0000</pubDate>
		<dc:creator>Lenny</dc:creator>
		
		<category><![CDATA[CSS]]></category>

		<category><![CDATA[Rails]]></category>

		<category><![CDATA[Web Development]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[Sass]]></category>

		<category><![CDATA[sprockets]]></category>

		<guid isPermaLink="false">http://rule52.com/?p=14</guid>
		<description><![CDATA[UPDATE: This article is definitely out of date now. The new sass-rails gem will augment Sass to handle dependencies, load paths, and Rails helpers (like asset_path) in a Sprockets environment. I&#8217;m now including edge versions of sass-rails and compass in my Gemfile and everything is working smoothly in development. I no longer have to use [...]]]></description>
			<content:encoded><![CDATA[<p>UPDATE: This article is definitely out of date now. The new <a href="https://github.com/rails/sass-rails">sass-rails</a> gem will augment Sass to handle dependencies, load paths, and Rails helpers (like <code>asset_path</code>) in a Sprockets environment. I&#8217;m now including edge versions of sass-rails and compass in my Gemfile and everything is working smoothly in development. I no longer have to use Sprocket-style <code>*= require</code> directives, just Sass <code>@import</code>s.</p>
<p>I&#8217;m a big fan of the new asset pipeline features in <a href="https://github.com/rails/rails">Rails 3.1</a>. I&#8217;ve spent way too much time building my own JavaScript and CSS precompilers or forcing Sprockets 1.0 to do things it wasn&#8217;t intended to do. When I started using <a href="https://github.com/sstephenson/sprockets">Sprockets 2.0</a> with <a href="http://sass-lang.com/">Sass</a> and <a href="http://compass-style.org/">Compass</a>, there were some gotchas I didn&#8217;t anticipate and I was surprised not to see bug reports about them. As it turns out I was thinking about it all wrong. Here&#8217;s a quick explanation in case other people have the same problem.</p>
<p>I&#8217;m used to using Sass with one master stylesheet and several partials, like so:</p>
<div class="geshi no js">
<div class="head">// application.scss</div>
<ol>
<li class="li1">
<div class="de1">@import &quot;compass&quot;;
</div>
</li>
<li class="li1">
<div class="de1">@import &quot;common&quot;; // Defines variables and mixins
</div>
</li>
<li class="li1">
<div class="de1">@import &quot;home&quot;; // Uses variables and mixins defined in _common.scss
</div>
</li>
<li class="li1">
<div class="de1">@import &quot;checkout&quot;; // Like _home.scss</div>
</li>
</ol>
</div>
<p>In Rails 3.1 and Sprockets, this doesn&#8217;t work for two reasons:</p>
<ol>
<li>Sprockets isn&#8217;t aware of <code>@import</code> statements in Sass, so it doesn&#8217;t invalidate the cached master stylesheet when one of them changes. You can use the <code>*= depend_on</code> directive to handle this, but then you have to list your partial files twice.</li>
<li>If you reference images in <code>assets/images</code>, the Rails precompiler rewrites those files names with an md5 hash, like <code>assets/bg-9c0a079bdd7701d7e729bd956823d153.png</code> (as described in this post on <a href="https://moocode.com/posts/1-deploying-a-rails-3-1-application-to-production">Rails 3.1 in production</a>.) Sass can&#8217;t preprocess files with ERB, so you can&#8217;t use <code>asset_path</code>.</li>
</ol>
<p>Being able to use ERB is pretty huge, so I realized I had to use Sprockets <code>require</code> directives to get that preprocessing. But you can&#8217;t just <code>require "compass"</code> at the top (it&#8217;s not in the load path) or be able to share variables and mixins with a single <code>require</code>.</p>
<p>The trick is to think of Sass files like Ruby files: if you depend on functionality in another file, you have to explicitly include it each time. So my example above becomes:</p>
<div class="geshi no js">
<div class="head">// application.css.scss</div>
<ol>
<li class="li1">
<div class="de1">/*
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;*= require_self
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;*= require &quot;home&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;*= require &quot;checkout&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;*= depend_on &quot;_common&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;*/
</div>
</li>
<li class="li1">
<div class="de1">@import &quot;compass/reset&quot;</div>
</li>
</ol>
</div>
<div class="geshi no js">
<div class="head">// home.css.scss.erb</div>
<ol>
<li class="li1">
<div class="de1">@import &quot;compass&quot;;
</div>
</li>
<li class="li1">
<div class="de1">@import &quot;common&quot;;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">body {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; background: url();
</div>
</li>
<li class="li1">
<div class="de1">}</div>
</li>
</ol>
</div>
<div class="geshi no js">
<div class="head">// checkout.css.scss.erb</div>
<ol>
<li class="li1">
<div class="de1">@import &quot;compass&quot;; // again!
</div>
</li>
<li class="li1">
<div class="de1">@import &quot;common&quot;; // also again!</div>
</li>
</ol>
</div>
<p>I still had to add a <code>depend_on</code> directive for the partial so that <code>application.css</code> updates when that file changes, but otherwise this solution solves all my problems and I have maximum flexibility.</p>
<p>Let me know if I&#8217;m overlooking something simple! And this post will probably be out-of-date soon as the Sprockets guys starting adding documentation.</p>
<p>UPDATE: Came across another gotcha today. You can&#8217;t use <code>asset_path</code> inside partial Sass files included with <code>@import</code>, so any shared styles can&#8217;t reference image files. Arg.</p>
<p>PS: This is how you get Compass in the Sass load path (I forgot where I stole this from):</p>
<div class="geshi no ruby">
<div class="head"># config/initializers/sass.rb</div>
<ol>
<li class="li1">
<div class="de1"><span class="re2">Sass::Engine::DEFAULT_OPTIONS</span><span class="br0">&#91;</span><span class="re3">:load_paths</span><span class="br0">&#93;</span>.<span class="me1">tap</span> <span class="kw1">do</span> <span class="sy0">|</span>load_paths<span class="sy0">|</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; load_paths <span class="sy0">&amp;</span>lt;<span class="sy0">&amp;</span>lt; <span class="st0">&quot;#{Rails.root}/app/assets/stylesheets&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; load_paths <span class="sy0">&amp;</span>lt;<span class="sy0">&amp;</span>lt; <span class="st0">&quot;#{Gem.loaded_specs[&#39;compass&#39;].full_gem_path}/frameworks/compass/stylesheets&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://rule52.com/2011/06/sass-and-sprockets-for-rails-31/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Proof of Concept: Bindings in JavaScript</title>
		<link>http://rule52.com/2008/08/bindings-in-javascript/</link>
		<comments>http://rule52.com/2008/08/bindings-in-javascript/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 22:53:17 +0000</pubDate>
		<dc:creator>Lenny</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://rule52.com/?p=13</guid>
		<description><![CDATA[In another attempt to copy ideas from Actionscript 3 to JavaScript, I spent a few hours trying to implement bindings. I may try to implement an MVC framework like Cairngorm in JavaScript someday, and bindings play an important role in Flex and Actionscript.
Bindings keep UI elements in sync with data with minimal code. In Cairngorm, [...]]]></description>
			<content:encoded><![CDATA[<p>In another attempt to copy ideas from Actionscript 3 to JavaScript, I spent a few hours trying to implement bindings. I may try to implement an MVC framework like Cairngorm in JavaScript someday, and bindings play an important role in Flex and Actionscript.</p>
<p>Bindings keep UI elements in sync with data with minimal code. In Cairngorm, all application data lives in a <code>ModelLocator</code> singleton, so UI elements have a single point of reference for application data.</p>
<p>I’m sure there are many ways to implement binding in JavaScript, but I started with my ideal API:</p>
<div class="geshi no javascript">
<div class="head">Binding.bind(object, &#8220;property&#8221;, object2, &#8220;property2&#8243;);</pre>
</div>
<ol>
<li class="li1">
<div class="de1">That method call does a few simple things:</div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&lt;</span>ol<span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sy0">&lt;</span>li<span class="sy0">&gt;</span>Adds generic getter and setter methods. <span class="me1">The</span> setter has a hook into the binding utility.<span class="sy0">&lt;/</span>li<span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sy0">&lt;</span>li<span class="sy0">&gt;</span>If an object <span class="kw1">is</span> an applicable HTML element, it adds the appropriate event listeners that hook into the binding utility.<span class="sy0">&lt;/</span>li<span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sy0">&lt;</span>li<span class="sy0">&gt;</span>It stores the respective object<span class="re0">/property pairs <span class="kw1">for</span> each object.<span class="sy0">&lt;/|</span>>li<span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&lt;/</span>ol<span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">This</span> implementation forces you to <span class="kw2">use</span> <span class="sy0">&lt;</span>code<span class="sy0">&gt;</span>object.<span class="me1">set</span><span class="br0">&#40;</span><span class="st0">&quot;propertyName&quot;</span>, value<span class="br0">&#41;</span><span class="sy0">&lt;/</span>code<span class="sy0">&gt;</span> <span class="kw1">in</span> order to trigger binding. <span class="me1">The</span> binding trigger can easily be implemented <span class="kw1">in</span> custom objects <span class="kw1">as</span> well <span class="kw1">with</span> <span class="kw1">this</span> method call:</div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&lt;</span>pre<span class="sy0">&gt;</span>Binding.<span class="me1">trigger</span><span class="br0">&#40;</span>object, <span class="st0">&quot;propertyName&quot;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Where <code>object</code> is the bound object. This creates a cascade of calls setting the appropriate values on properties of other bound objects.</p>
<p>I avoided recursion by creating a unique transaction id every time <code>Binding.trigger</code> is called. The <code>set</code> method passes this id along internally so no object is changed twice in the same transaction.</p>
<p>So far I only support text and checkbox inputs, textareas, and select dropdowns. The selected option in a select dropdown doesn’t amount to a single property, but instead to <code>element.options[element.selectedIndex].value</code>. It was easy to override the  <code>get</code> and <code>set</code> methods to allow <code>Binding.trigger(element, "value")</code>.</p>
<p>See the <a href="http://test.rule52.com/jsbinding">example</a> and check out the <a href="http://svn.rule52.com/svn/jsbinding">code</a>. I used the <a href="http://flowjs.com">Flow</a> JavaScript library to normalize the DOM API (really just for <code>addEventListener</code>), but it doesn’t rely on any particular library.</p>
]]></content:encoded>
			<wfw:commentRss>http://rule52.com/2008/08/bindings-in-javascript/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Developing Silverlight 1.0 RIAs: JavaScript Optimization</title>
		<link>http://rule52.com/2008/06/developing-silverlight-js-optimization/</link>
		<comments>http://rule52.com/2008/06/developing-silverlight-js-optimization/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 21:14:21 +0000</pubDate>
		<dc:creator>Lenny</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://rule52.com/?p=12</guid>
		<description><![CDATA[(Read the intro here.)
My appreciation and respect for JavaScript increased tremendously while developing Silverlight projects. Not at all because of how easy it was to work with. But its flexibility and power made it interesting and fun to tackle challenges like optimization and application architecture.
Optimization
In Silverlight 1.0, the plugin is only a rendering and animation [...]]]></description>
			<content:encoded><![CDATA[<p>(Read the intro <a href="/2008/06/developing-silverlight-intr/">here</a>.)</p>
<p>My appreciation and respect for JavaScript increased tremendously while developing Silverlight projects. Not at all because of how easy it was to work with. But its flexibility and power made it interesting and fun to tackle challenges like optimization and application architecture.</p>
<h3 id="optimization">Optimization</h3>
<p>In Silverlight 1.0, the plugin is only a rendering and animation engine. All of the logic and much of the processing still resides in the browser’s JavaScript interpreter. All web applications need to make optimization a priority, but this is especially true for Silverlight 1.0 applications.</p>
<p>Doing a lot of small things can add up to make a big difference. Swap <code>myArray.push(obj)</code> with <code>myArray[myArray.length] = obj</code>. Avoid string concatenation when possible to give Internet Explorer a little boost.</p>
<p>I made two significant changes to my code between the WWE project and the last Library of Congress project that I believe made a big difference.</p>
<h3 id="avoid_functionbind">Avoid <code>Function.bind</code></h3>
<p>I usually resorted to a <code>Function.bind</code> method (usually the version in <a href="http://www.prototypejs.org/api/function#method-bind">prototype.js</a>) to manipulate function scope. This really comes in handy for event listeners:</p>
<div class="geshi no javascript">
<div class="head">// inside of a class</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> addMouseEvents<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">xaml</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">&#39;MouseEnter&#39;</span>, <span class="kw1">this</span>.<span class="me1">onMouseEnter</span>.<span class="me1">bind</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">xaml</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">&#39;MouseLeave&#39;</span>, <span class="kw1">this</span>.<span class="me1">onMouseLeave</span>.<span class="me1">bind</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">xaml</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">&#39;MouseLeftButtonDown&#39;</span>, <span class="kw1">this</span>.<span class="me1">onMouseDown</span>.<span class="me1">bind</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> onMouseEnter<span class="br0">&#40;</span>sender, mouseArgs<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// &quot;this&quot; still refers to the instance of the class</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>I really like this syntax, even though in an ideal world it wouldn’t be necessary. (Actionscript 3 is an ECMAScript implementation that fixes this). I’ve heard Mozilla might implement it natively, which would be lovely.</p>
<p>The problem with <code>bind()</code> is that it uses <code>apply()</code> internally, which is significantly slower than just executing the function. This makes a big difference for <code>MouseMove</code> or <code>ProgressChanged</code> event handlers.</p>
<p>Fortunately it is easy to avoid, and there are a number of ways to use depending on your personal preference. I usually used:</p>
<div class="geshi no javascript">
<div class="head">// inside of a class</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> addMouseEvents<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> that = <span class="kw1">this</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">xaml</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">&#39;MouseEnter&#39;</span>, <span class="kw2">function</span><span class="br0">&#40;</span>sender, args<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; that.<span class="me1">onMouseEvent</span><span class="br0">&#40;</span>sender, args<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// .. other events</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>I looked around for any documentation on the effects on <code>bind()</code> on performance and found nothing. This isn’t a Silverlight-specific issue but something other people might run into so it’s probably worth documenting.</p>
<h3 id="using_and_removing_event_listeners">Using and Removing Event Listeners</h3>
<p>This also isn’t necessarily Silverlight-specific, but it’s also worth mentioning because of a few difference from how HTML DOM event listeners are handled. The crux of the matter is still that adding event listeners is memory intensive and not disposing of them properly can cause leaks and sluggishness.</p>
<p>In HTML you can use event delegation to limit the number of event listeners added. Add a single <code>click</code> event listener to a parent object, and act upon the event based on on the <code>target</code> property of the event, which is usually a child of the parent object. This isn’t possible in Silverlight 1.0 because there isn’t a real event object. The <code>sender</code> argument sent to the listener is always the object to which the event was added.</p>
<p>The only way to combat the overhead from adding event listeners is to reuse objects when possible. For WWE, there is a big grid of thumbnails the user can click on. Instead of creating a whole new grid each page refresh, I just changed the data for each item.</p>
<p>Sometimes reuse isn’t an option, and you’re forced to create and destroy XAML objects repeatedly. Removing event listeners before destroying an object isn’t difficult.</p>
<div class="geshi no javascript">
<div class="head">var eventToken = xaml.addEventListener(&#39;MouseEnter&#39;, function() { &#8230; });</div>
<ol>
<li class="li1">
<div class="de1">xaml.<span class="me1">removeEventListener</span><span class="br0">&#40;</span><span class="st0">&#39;MouseEnter&#39;</span>, eventToken<span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>I came up with a way to store those event tokens in a simple, behind-the-scenes manner, which made this easier to do than not do. I’ll describe this in the next section on JavaScript/Silverlight Architecture.</p>
<ul>
<li><a href="/2008/08/developing-silverlight-intr">Intro</a></li>
<li><a href="/2008/06/developing-silverlight-xaml">Part 1: Xaml</a></li>
<li>Part 2: JavaScript Optimization</li>
<li>Part 3: JavaScript Architecture</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://rule52.com/2008/06/developing-silverlight-js-optimization/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Adding CSS rules with JavaScript to hide content on page load</title>
		<link>http://rule52.com/2008/06/css-rule-page-load/</link>
		<comments>http://rule52.com/2008/06/css-rule-page-load/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 00:09:25 +0000</pubDate>
		<dc:creator>Lenny</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://rule52.com/?p=11</guid>
		<description><![CDATA[Update: I&#8217;ve had to update the code to get it to work reliably in Safari 2. Much thanks to this article on the YUI Blog for another solution.
A lot of ajax-y web pages involve hiding and showing content. My current project has a tons of content related to a video in dynamic reveals that the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> I&#8217;ve had to update the code to get it to work reliably in Safari 2. Much thanks to <a href="http://yuiblog.com/blog/2007/06/07/style/">this article</a> on the YUI Blog for another solution.</p>
<p>A lot of ajax-y web pages involve hiding and showing content. My current project has a tons of content related to a video in dynamic reveals that the user can open and close. Of course, accessibility is important, so for users without JavaScript enabled, all of this hidden content should be visible.</p>
<p>Typically, JavaScript would explicitly hide this content as soon as it is available. Something like:</p>
<div class="geshi no javascript">
<div class="head">// using prototype.js</div>
<ol>
<li class="li1">
<div class="de1">document.<span class="me1">observe</span><span class="br0">&#40;</span><span class="st0">&quot;dom:loaded&quot;</span>, <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// initially hide the reveal container</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; $<span class="br0">&#40;</span><span class="st0">&#39;revealContent&#39;</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>I chose prototype.js because of its notorious file size, and I know a lot of you still don’t compress and gzip their JavaScript! If you’re loading a lot of JavaScript, HTML, and CSS, chances are pretty likely that the <code>#revealContent</code> element will display before that that function has a chance to run.</p>
<p>So how do we hide that element before it has a chance to load? The technique I’m using adds a CSS rule to a new stylesheet as soon as possible, i.e. when the JavaScript is interpreted.</p>
<div class="geshi no css">
<div class="head">/* Add a rule in CSS that lets you override the generated rule */</div>
<ol>
<li class="li1">
<div class="de1"><span class="re0">#revealContent</span><span class="re1">.show</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">display</span><span class="sy0">:</span> <span class="kw2">block</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<pre>// JavaScript

var CSSRules = function() {

	var headElement = document.getElementsByTagName("head")[0],
		styleElement = document.createElement(&#8221;style&#8221;);
	styleElement.type = &#8220;text/css&#8221;;
	headElement.appendChild(styleElement);

	// memoize the browser-dependent add function
	var add = function() {
		// IE doesn&#8217;t allow you to append text nodes to
 elements
		if (styleElement.styleSheet) {
			return function(selector, rule) {
				if (styleElement.styleSheet.cssText == &#8221;) {
					styleElement.styleSheet.cssText = &#8221;;
				}
				styleElement.styleSheet.cssText += selector + &#8221; { &#8221; + rule + &#8221; }&#8221;;
			}
		} else {
			return function(selector, rule) {
				styleElement.appendChild(document.createTextNode(selector + &#8221; { &#8221; + rule + &#8221; }&#8221;));
			}
		}
	}();

	return {
		add : add
	}
}();

CSSRules.add(&#8217;#revealContent&#8217;, &#8216;display: none !important&#8217;);

// later
$(&#8217;revealContent&#8217;).addClass(&#8217;show&#8217;);
&lt;/pre&gt;
&lt;p&gt;I haven’t tested this exhaustively, but it works as intended in Internet Explorer 6 and 7, Firefox 2 and 3, Safari 2 and 3, and Opera 9. I originally expected some strange behavior from adding elements to the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; before it finished loading, but fortunately this isn&#8217;t the case.&lt;/p&gt;
&lt;p&gt;You could also include a stylesheet or a &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tag in a &lt;code&gt;&amp;lt;noscript&amp;gt;&lt;/code&gt; tag, but I prefer to keep all of this behavior in JavaScript.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; The old code:&lt;/p&gt;
&lt;pre lang=&#8221;javascript&#8221;&gt;
// JavaScript
var CSSRules = function() {

    // create a stylesheet
    var stylesheet = document.createElement(&#8221;style&#8221;);
    stylesheet.setAttribute(&#8221;type&#8221;, &#8220;text/css&#8221;);
    document.getElementsByTagName(&#8221;head&#8221;)[0].appendChild(stylesheet);
    stylesheet = document.styleSheets[document.styleSheets.length - 1];

    return {
        add: function(selector, rule) {
            // the Microsoft method
            if (stylesheet.addRule) {
                stylesheet.addRule(selector, rule, 0);

            // the standard method
            } else if (stylesheet.insertRule) {
                // safari 2 apparently ignores this
                stylesheet.insertRule(selector.concat(&#8217;{&#8217; + rule + &#8216;}&#8217;), 0);
            }
        }
    };
}();

CSSRules.add(&#8217;#revealContent&#8217;, &#8216;display: none !important&#8217;);

// later
$(&#8217;revealContent&#8217;).addClass(&#8217;show&#8217;);
&lt;/pre&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://rule52.com/2008/06/css-rule-page-load/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Developing Silverlight 1.0 RIAs: XAML</title>
		<link>http://rule52.com/2008/06/developing-silverlight-xaml/</link>
		<comments>http://rule52.com/2008/06/developing-silverlight-xaml/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 22:59:48 +0000</pubDate>
		<dc:creator>Lenny</dc:creator>
		
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://rule52.com/?p=10</guid>
		<description><![CDATA[(Read the intro here.)
First, XAML is an awkward language to work with. From a business perspective, this makes sense because Microsoft can push their Expression suite on developers and designers. But this is unacceptable for a client-side developer who wants to write markup by hand and is a Mac user.
XML is a terrible vehicle for [...]]]></description>
			<content:encoded><![CDATA[<p>(Read the intro <a href="/2008/06/developing-silverlight-intr/">here</a>.)</p>
<p>First, XAML is an awkward language to work with. From a business perspective, this makes sense because Microsoft can push their Expression suite on developers and designers. But this is unacceptable for a client-side developer who wants to write markup by hand <strong>and</strong> is a Mac user.</p>
<p>XML is a terrible vehicle for describing anything more complicated than nested boxes. It is especially terrible with vectors. This is XAML generated by Blend to describe a tiny arrow pointing left:</p>
<div class="geshi no xml">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;Path</span> <span class="re0">Width</span>=<span class="st0">&quot;5&quot;</span> <span class="re0">Height</span>=<span class="st0">&quot;11.061&quot;</span> <span class="re0">Fill</span>=<span class="st0">&quot;#FFFFFFFF&quot;</span> <span class="re0">Stretch</span>=<span class="st0">&quot;Fill&quot;</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="re0">Data</span>=<span class="st0">&quot;M441.46101,604.74493 L445.48904,608.88653 441.52674,613.46898 </span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">442.43734,614.14997 446.91423,608.85206 442.3852,604.04503 z&quot;</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="re0">Opacity</span>=<span class="st0">&quot;1&quot;</span> <span class="re0">x:Name</span>=<span class="st0">&quot;back_arrow&quot;</span> Canvas.<span class="re0">Left</span>=<span class="st0">&quot;6&quot;</span> Canvas.<span class="re0">Top</span>=<span class="st0">&quot;8&quot;</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="re0">RenderTransformOrigin</span>=<span class="st0">&quot;0.5,0.5&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sc3"><span class="re1">&lt;Path</span>.RenderTransform<span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;TransformGroup<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;RotateTransform</span> <span class="re0">Angle</span>=<span class="st0">&quot;-180&quot;</span><span class="re2">/&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;/TransformGroup<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sc3"><span class="re1">&lt;/Path</span>.RenderTransform<span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/Path<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>Opening up 2000 line XAML files full of markup like this is headache inducing.</p>
<p>Second, XAML in SL 1.0 is the Anti-DRY language. It lacks concepts like cascading styles and class names, so if you want all your <code>TextBlock</code> elements to be red, you have to specify <code>Foreground="#FFFF0000"</code> for each one.</p>
<p>It gets worse for repeating elements programmatically. Say you have XAML for a button with a <code>TextBlock</code> label, and you want to create four of them to make a menu:</p>
<div class="geshi no xml">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;Canvas</span> <span class="re0">Width</span>=<span class="st0">&quot;100&quot;</span> <span class="re0">Height</span>=<span class="st0">&quot;20&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sc3"><span class="re1">&lt;Rectangle</span> <span class="re0">Fill</span>=<span class="st0">&quot;#FF000000&quot;</span> <span class="re0">RadiusX</span>=<span class="st0">&quot;5&quot;</span> <span class="re0">RadiusY</span>=<span class="st0">&quot;5&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">Width</span>=<span class="st0">&quot;100&quot;</span> <span class="re0">Height</span>=<span class="st0">&quot;20&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;TextBlock</span> <span class="re0">x:Name</span>=<span class="st0">&quot;buttonLabel&quot;</span> Canvas.<span class="re0">Top</span>=<span class="st0">&quot;3&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;Canvas.<span class="re0">Left</span>=<span class="st0">&quot;5&quot;</span> <span class="re0">Foreground</span>=<span class="st0">&quot;#FFFFFFFF&quot;</span><span class="re2">&gt;</span></span>Label<span class="sc3"><span class="re1">&lt;/TextBlock<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sc3"><span class="re1">&lt;/Rectangle<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/Canvas<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>Of course, you&#8217;ll want programmatic access to that <code>TextBlock</code> so that you can change the label text. But because there is only one namespace for <code>x:Name</code>s (<code>x:Name="buttonLabel"</code> can only be used once), adding more than one to the XAML hierarchy will throw an exception. Also, there are no utilities like <code>.getElementsByTagName</code>. Solutions like <code>button.children.getItem[0].children.getItem[0]</code> are brittle and don&#8217;t scale.</p>
<p>There is a solution to this, but it was nearly impossible to find in the Silverlight 1.0 documentation. The <a href="http://msdn.microsoft.com/en-us/library/bb980072(VS.95).aspx"><code>createFromXaml</code></a> method takes a second boolean argument that can create a new, separate namespace for <code>x:Name</code>s. This allows you to create multiple XAML objects from the same source and add them to the XAML hierarchy without <code>x:Name</code> collisions.</p>
<p>A caveat to this technique is that <code>findName</code> won&#8217;t find elements created in a new namespace, so you have to save a representation of each new element in JavaScript.</p>
<p>My methodology for using XAML tries to tackle these two big challenges. It breaks down to these steps:</p>
<ul>
<li>Use images instead of XAML vectors when the file size overhead allows it.</li>
<li>If Blend is used, only create small, isolated pieces of XAML so each is easier to digest and manipulate.</li>
<li>Zip up every XAML file and download it all at the beginning. XAML files are usually light enough that it shouldn&#8217;t noticeably delay the start of your application.</li>
<li>Create XAML elements dynamically from the files in the zip file. Use <code>createFromXaml(text, true)</code> for repeated elements and always save a representation of the elements in JavaScript to manipulate later.</li>
</ul>
<p>I built a number of utility functions to make this methodology very easy to use. Here is an example:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> ComplicatedXamlCreator;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">new</span> XamlDownloader<span class="br0">&#40;</span><span class="st0">&#39;cmn/xaml/packaged/xaml.zip&#39;</span>, <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; onComplete : <span class="kw2">function</span><span class="br0">&#40;</span>zip<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; ComplicatedXamlCreator = zip.<span class="me1">save</span><span class="br0">&#40;</span><span class="st0">&#39;complicatedXaml.xaml&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; zip.<span class="me1">destroy</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// later, create a xaml object and add it to the XAML hierarchy</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> xaml = ComplicatedXamlCreator<span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">root.<span class="me1">children</span>.<span class="me1">add</span><span class="br0">&#40;</span>xaml<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// or create multiple xaml objects and manipulate independently</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> i = <span class="nu0">0</span>; i <span class="sy0">&lt;</span> <span class="nu0">10</span>; i++<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> xaml = ComplicatedXamlCreator<span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// finds a different TextBlock element each iteration</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> label = xaml.<span class="me1">findName</span><span class="br0">&#40;</span><span class="st0">&#39;label&#39;</span><span class="br0">&#41;</span>; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; label.<span class="me1">text</span> = <span class="st0">&#39;Button Label &#39;</span> + i;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; root.<span class="me1">children</span>.<span class="me1">add</span><span class="br0">&#40;</span>xaml<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>My <code>XamlDownloader</code> utility is a simple wrapper for the <code>Downloader</code> class that passes back an instance of a <code>ZipHandler</code> class. The <code>save</code> method grabs a XAML file out of the zip file and returns a XAML creation function that can be run as many times as necessary.</p>
<p>Because the <code>XamlDownloader</code> utility creates a closure around a <code>Downloader</code> instance, you have to destroy that instance or you&#8217;ll create a massive memory leak. The WWE project would bring down a fresh instance of Firefox 2 in half an hour easily.</p>
<ul>
<li><a href="/2008/08/developing-silverlight-intr">Intro</a></li>
<li>Part 1: Xaml</li>
<li><a href="/2008/06/developing-silverlight-js-optimization/">Part 2: JavaScript Optimization</a></li>
<li>Part 3: JavaScript Architecture</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://rule52.com/2008/06/developing-silverlight-xaml/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Developing Silverlight 1.0 RIAs</title>
		<link>http://rule52.com/2008/06/developing-silverlight-intr/</link>
		<comments>http://rule52.com/2008/06/developing-silverlight-intr/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 22:59:27 +0000</pubDate>
		<dc:creator>Lenny</dc:creator>
		
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://rule52.com/?p=9</guid>
		<description><![CDATA[This will be the archive for my advancements in Silverlight 1.0 development, now made pretty much obsolete by the 2.0 Beta 2 plugin released last week.
I&#8217;ve worked on four SL 1.0 projects:

myHSN, which I can no longer find online &#8212; September, 2007.
Reading Pre-Columbian Artifacts and Waldseemüller Maps for the Library of Congress, developed concurrently &#8212; [...]]]></description>
			<content:encoded><![CDATA[<p>This will be the archive for my advancements in Silverlight 1.0 development, now made pretty much obsolete by the 2.0 Beta 2 plugin released last week.</p>
<p>I&#8217;ve worked on four SL 1.0 projects:</p>
<ul>
<li>myHSN, which I can no longer find online &#8212; September, 2007.</li>
<li><a href="http://myloc.gov/Exhibitions/EarlyAmericas/PreContactWorld/LanguageandContext/Interactives/readingartifacts/index.html">Reading Pre-Columbian Artifacts</a> and <a href="http://myloc.gov/Exhibitions/EarlyAmericas/Interactives/Maps/index.html">Waldseemüller Maps</a> for the Library of Congress, developed concurrently &#8212; November-December, 2007.</li>
<li><a href="http://www.wwe.com/shows/ecw/silverlight/ecw">WWE&#8217;s Silveright Video Player</a>, up but with features pulled &#8212; December, 2007-January, 2008.</li>
<li>Creating the US <a href="http://myloc.gov/Exhibitions/creatingtheus/interactives/declaration/index.html">Declaration</a>, <a href="http://myloc.gov/Exhibitions/creatingtheus/interactives/constitution/index.html">Constitution</a>, and <a href="http://myloc.gov/Exhibitions/creatingtheus/interactives/bill_of_rights/index.html">Bill of Rights</a> for the LOC &#8212; April, 2008.</li>
</ul>
<p>All of these projects were developed under extremely tight deadlines and generally had ad-hoc development processes. HSN was a mess because the plugin was released <em>after</em> development had started. The first LOC projects had a lot of UX, creative, and project management complications. WWE actually followed our architecture and development processes very well but it was an overly ambitious project for a technology this immature. </p>
<p>For the last LOC project, I was finally able to piece together techniques developed over the last six months. It was still a very tight deadline and inevitably a bit ad-hoc, but I am still proud of it.</p>
<h3>Challenges in Silverlight 1.0 Development</h3>
<p>Silverlight 1.0 development is difficult by its reliance on two technologies:</p>
<ol>
<li>XAML</li>
<li>JavaScript</li>
</ol>
<p>Of course, that&#8217;s all there is to Silverlight 1.0! On top of that, it&#8217;s a buggy plugin, but there&#8217;s not much we can do about that. Instead, I&#8217;ll focus on ways to address the challenges presented by SL 1.0&#8217;s core technologies.</p>
<ul>
<li><a href="/2008/06/developing-silverlight-xaml">Part 1: Xaml</a></li>
<li><a href="/2008/06/developing-silverlight-js-optimization/">Part 2: JavaScript Optimization</a></li>
<li>Part 3: JavaScript Architecture</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://rule52.com/2008/06/developing-silverlight-intr/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Alex Russell at Google I/O</title>
		<link>http://rule52.com/2008/06/alex-russell-at-google-io/</link>
		<comments>http://rule52.com/2008/06/alex-russell-at-google-io/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 20:24:13 +0000</pubDate>
		<dc:creator>Lenny</dc:creator>
		
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://rule52.com/?p=8</guid>
		<description><![CDATA[
I&#8217;m not sure how much I would have gotten out of Google&#8217;s I/O conference at the end of May, because so many of the sessions seemed to center around GWT and Gears, which I have little interest in. But fortunately they posted videos of the talks on Youtube, and I really like this one byAlex [...]]]></description>
			<content:encoded><![CDATA[<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/nG66hIhUdEU&#038;hl=en&#038;color1=0x3a3a3a&#038;color2=0x999999"></param><embed src="http://www.youtube.com/v/nG66hIhUdEU&#038;hl=en&#038;color1=0x3a3a3a&#038;color2=0x999999" type="application/x-shockwave-flash" width="425" height="344"></embed></object></p>
<p>I&#8217;m not sure how much I would have gotten out of Google&#8217;s I/O conference at the end of May, because so many of the sessions seemed to center around GWT and Gears, which I have little interest in. But fortunately they posted videos of the talks on Youtube, and I really like this one by<a href="http://alex.dojotoolkit.org/">Alex Russell</a> of the Dojo JS library. It&#8217;s not a talk about practical solutions to the larger problems facing the web, but it gives a thoughtful, articulate, big-picture summary of what brought us here, where we&#8217;re going, and who to yell at to change something.</p>
]]></content:encoded>
			<wfw:commentRss>http://rule52.com/2008/06/alex-russell-at-google-io/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How YUI&#8217;s event listeners changed the way I write JavaScript</title>
		<link>http://rule52.com/2008/06/yui-event-listeners/</link>
		<comments>http://rule52.com/2008/06/yui-event-listeners/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 04:09:34 +0000</pubDate>
		<dc:creator>Lenny</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://rule52.com/?p=7</guid>
		<description><![CDATA[I really like Prototype.js. Its inclusion in Ruby on Rails got me writing JavaScript again. We at Schematic don’t recommend Prototype for client projects however, and I’m glad because the library or framework you use can greatly change the style in which you write code. And sometimes that can make you a better programmer.
Working with YUI helped trigger [...]]]></description>
			<content:encoded><![CDATA[<p>I really like <a href="http://prototypejs.org/">Prototype.js</a>. Its inclusion in Ruby on Rails got me writing JavaScript again. We at <a href="http://schematic.com/">Schematic</a> don’t recommend Prototype for client projects however, and I’m glad because the library or framework you use can greatly change the style in which you write code. And sometimes that can make you a better programmer.</p>
<p>Working with <a href="http://developer.yahoo.com/yui/">YUI</a> helped trigger an epiphany for me this past week. Credit for laying the foundation for this moment goes to the <a href="http://yuiblog.com/blog/2008/05/05/crockford-inheritance/">sample chapter</a> on Inheritance of Douglas Crawford’s new book, <a href="http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742/">JavaScript: The Good Parts</a>. I did not understand the practical usage for the techniques in the chapter at first because I learned Object Oriented design in classical OO languages like Actionscript 2, PHP5, and Ruby (which Prototype imitates as best as possible).</p>
<p>I’ve tried to maintain this style in JavaScript, especially in large projects like Silverlight 1.0 RIAs. I usually structured classes like this:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> MyClass = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// MyClass constructor</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">MyClass.<span class="me1">prototype</span> =<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; method : <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// do stuff with the &quot;this&quot; object</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> myInstance = <span class="kw2">new</span> MyClass<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>I eventually extracted the new <code>Class.create</code> function from Prototype.js 1.6.0, because it gives you an elegant syntax for inheritance, mixins, and access to overridden methods without much overhead. It basically does the same as the above with some extra magic for creating the inheritance chain.</p>
<p>There’s nothing inherently wrong with this approach, and I think that anyone who tells you otherwise is a bit of a zealot. But it’s reliance on the <code>this</code> object creates problems, especially with event listeners. YUI made this very apparent:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> GUIElement = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> button = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">&#39;button&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; YAHOO.<span class="me1">util</span>.<span class="me1">Event</span>.<span class="me1">addListener</span><span class="br0">&#40;</span>button, <span class="st0">&#39;click&#39;</span>, <span class="kw1">this</span>.<span class="me1">onButtonClick</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">GUIElement.<span class="me1">prototype</span> = <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; onButtonClick : <span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// this === button</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> element = <span class="kw2">new</span> GUIElement<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>The scope of the event handler is the button, which I believe follows the standard. However, I usually want <code>this</code> to refer to the instance of the class so I can access other methods and properties. The <code>bind</code> method in Prototype makes this easy by creating an anonymous function that executes the function in the specified scope:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">button.<span class="me1">observe</span><span class="br0">&#40;</span><span class="st0">&#39;click&#39;</span>, <span class="kw1">this</span>.<span class="me1">onButtonClick</span>.<span class="me1">bind</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>There are a few problems with this approach.</p>
<ol>
<li><code>bind</code> uses <code>Function.apply</code> which adds overhead. It’s usually negligible but with JavaScript optimization is always a top priority.</li>
<li>We’re not using Prototype! In this case, the client is a large e-commerce site and we’re only coding a small section of it. Prototype doesn’t necessarily place nice with other libraries since it defines a lot of global variables and augments the prototypes of build-in objects (like adding <code>bind</code> to <code>Function</code>).</li>
</ol>
<p>You can <a href="http://developer.yahoo.com/yui/event/#customobject">specify the scope</a> of the event handler with YUI, but it has the same effects as the first point above.</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="co1">// a &quot;truthy&quot; last argument makes the second-to-last argument the function scope</span></div>
</li>
<li class="li1">
<div class="de1">YAHOO.<span class="me1">util</span>.<span class="me1">Event</span>.<span class="me1">addListener</span><span class="br0">&#40;</span>button, <span class="st0">&#39;click&#39;</span>, <span class="kw1">this</span>.<span class="me1">onButtonClick</span>, <span class="kw1">this</span>, <span class="kw2">true</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>This felt very awkward to me, like I was doing something I wasn’t supposed to do. It’s perfectly valid but the awkwardness got a few gears turning in my head. I don’t think I can adequately explain my train of thought without turning this into an unreadable ramble, but here is how I’ve started writing components (using the <a href="http://yuiblog.com/blog/2007/06/12/module-pattern/">module pattern</a>):</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> GUIElement = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// define constants, like shortcuts to the YUI library</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> Event = YAHOO.<span class="me1">util</span>.<span class="me1">Event</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// this &quot;constructor&quot; creates an object and passes it to functions</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// instead of calling methods on the object itself</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span> create<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> object = <span class="br0">&#123;</span><span class="br0">&#125;</span>; <span class="co1">// the instance</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; object.<span class="me1">button</span> = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">&#39;button&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; addEvents<span class="br0">&#40;</span>object<span class="br0">&#41;</span>; <span class="co1">// pass it to the function</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> object;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// we&#39;ll call the object &quot;that&quot; so it kind of looks like &quot;this&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span> addEvents<span class="br0">&#40;</span>that<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// pass &quot;that&quot; to the event handler, but don&#39;t change scope</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Event.<span class="me1">addListener</span><span class="br0">&#40;</span>that.<span class="me1">button</span>, <span class="st0">&#39;click&#39;</span>, onButtonClick, that<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span> onButtonClick<span class="br0">&#40;</span>event, that<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// do stuff with that</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// as a bonus, &quot;this&quot; still refers to the button</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; create: create <span class="co1">// make create a public function</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// the module pattern means no more &quot;new&quot; keyword</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> element = GUIElement.<span class="me1">create</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>And all of a sudden, Crockford’s techniques make sense! My paradigm shift is really just two ideas:</p>
<ul>
<li>Encapsulate code into factories, not classes.</li>
<li>Functions act upon objects instead of being methods of the object that act upon itself (<code>addEvents(that)</code> instead of <code>this.addEvents()</code>).</li>
</ul>
<p>I’ve yet to fully explore the ramifications of these techniques, but I wanted to try writing this down to see if it still makes sense. I won&#8217;t throw <code>Class.create</code> out of my toolbox just yet, but if I can understand an elegant way to do inheritance&#8211;or more likely, composition&#8211;I do think this will be the way to go 90% of the time. More on this topic to come.</p>
<p>Addendum: Curiously, <a href="http://www.lukew.com">Luke W</a> posted an article while I wrote this about the exact <a href="http://www.lukew.com/ff/entry.asp?687">component</a> I was refactoring when this epiphany hit me.</p>
]]></content:encoded>
			<wfw:commentRss>http://rule52.com/2008/06/yui-event-listeners/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Blog goals</title>
		<link>http://rule52.com/2008/05/blog-goals/</link>
		<comments>http://rule52.com/2008/05/blog-goals/#comments</comments>
		<pubDate>Thu, 29 May 2008 06:03:12 +0000</pubDate>
		<dc:creator>Lenny</dc:creator>
		
		<category><![CDATA[Site]]></category>

		<guid isPermaLink="false">http://www.rule52.com/?p=3</guid>
		<description><![CDATA[This is the first of what I&#8217;m sure will be many navel-gazing posts before I actually get into this blogging thing. I&#8217;m not happy with the design I threw together this past weekend. For a future laugh, here&#8217;s what I&#8217;ve come up with so far:

The reason I&#8217;m unhappy with this so far is that it [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first of what I&#8217;m sure will be many navel-gazing posts before I actually get into this blogging thing. I&#8217;m not happy with the design I threw together this past weekend. For a future laugh, here&#8217;s what I&#8217;ve come up with so far:</p>
<p><a href="http://www.rule52.com/wp-content/uploads/2008/05/1_cityscape.png"><img class="alignnone size-thumbnail wp-image-4" title="1_cityscape" src="http://www.rule52.com/wp-content/uploads/2008/05/1_cityscape-150x150.png" alt="The first try at a 4-column blog design" width="150" height="150" /></a><a href="http://www.rule52.com/wp-content/uploads/2008/05/2_underground.png"><img class="alignnone size-thumbnail wp-image-5" title="2_underground" src="http://www.rule52.com/wp-content/uploads/2008/05/2_underground-150x150.png" alt="The current design as of this writing" width="150" height="150" /></a></p>
<p>The reason I&#8217;m unhappy with this so far is that it doesn&#8217;t achieve the goals I have for a blog. Of course, I can&#8217;t fully achieve my goals unless I know what they are in the first place. What are my goals?</p>
<ul>
<li>To have central homepage that aggregates my three outlets on the web: twitter, tumblr, and this blog. The streams should be kept separate because they have different audiences. The blog is technical, tumblr is social, and twitter is personal.</li>
<li>To have URL I can use to identify myself professionally.</li>
<li>To have good blog for technical writing. It must be easy to skim and allow code samples.</li>
</ul>
<p>I could probably manufacture more if I needed to, but I think this is a good starting point.</p>
<p>Don&#8217;t worry, nonexistant readers! I have a few topics in mind for real posts in the near future.</p>
]]></content:encoded>
			<wfw:commentRss>http://rule52.com/2008/05/blog-goals/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://rule52.com/2008/05/hello-world/</link>
		<comments>http://rule52.com/2008/05/hello-world/#comments</comments>
		<pubDate>Tue, 27 May 2008 03:54:34 +0000</pubDate>
		<dc:creator>Lenny</dc:creator>
		
		<category><![CDATA[Site]]></category>

		<guid isPermaLink="false">http://www.rule52.com/?p=1</guid>
		<description><![CDATA[First! 
&#8230; wait.
A blog design in progress
Having tried to start a blog for, I don&#8217;t know, the last 8 years or so, I know that if I try to get the design perfect I will never post it for the world to see. Hell, if I were to wait until it qualified as just &#8220;good&#8221; you [...]]]></description>
			<content:encoded><![CDATA[<p>First! </p>
<p>&#8230; wait.</p>
<h3>A blog design in progress</h3>
<p>Having tried to start a blog for, I don&#8217;t know, the last 8 years or so, I know that if I try to get the design perfect I will never post it for the world to see. Hell, if I were to wait until it qualified as just &#8220;good&#8221; you would never read this. So my plan is to put something up that isn&#8217;t ugly and tweak it from here. I&#8217;ll write a post or two about my development process, or lack thereof, as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://rule52.com/2008/05/hello-world/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

