<?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>a pint of javascript (and general web stuff) &#187; patterns</title>
	<atom:link href="http://blog.gonchuki.com/category/patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.gonchuki.com</link>
	<description></description>
	<lastBuildDate>Tue, 06 Jul 2010 21:45:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CSS Sprites2 &#8211; It&#039;s MooTools Time</title>
		<link>http://blog.gonchuki.com/archives/css-sprites2-its-mootools-time/</link>
		<comments>http://blog.gonchuki.com/archives/css-sprites2-its-mootools-time/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 17:29:20 +0000</pubDate>
		<dc:creator>gonchuki</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[usability]]></category>
		<category><![CDATA[sprites]]></category>

		<guid isPermaLink="false">http://blog.gonchuki.com/?p=34</guid>
		<description><![CDATA[A few months ago you may have read Dave Shea&#039;s entry on A List Apart &#034;CSS Sprites2 &#8211; It&#039;s JavaScript Time&#034; where he implemented a variant of his own CSS Sprites article by adding jQuery based animations. In this same article Dave encouraged us [developers] to port it to other libraries, so after a few [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago you may have read <a href="http://www.mezzoblue.com/">Dave Shea</a>&#039;s entry on A List Apart &#034;<a href="http://www.alistapart.com/articles/sprites2"><acronym title="Cascading Style Sheets">CSS</acronym> Sprites2 &#8211; It&#039;s JavaScript Time</a>&#034; where he implemented a variant of his own <a href="http://www.alistapart.com/articles/sprites"><acronym title="Cascading Style Sheets">CSS</acronym> Sprites</a> article by adding <a href="http://jquery.com">jQuery</a> based animations. In this same article Dave encouraged us [developers] to port it to other libraries, so after a few months of inactivity I&#039;m back with the <a href="http://mootools.net">MooTools</a> port of this slick effect.<br />
<span id="more-34"></span></p>
<link rel="stylesheet" type="text/css" href="http://sandbox.gonchuki.com/sprites2/sprites2.css" media="all" />
<h3 class="section">Enter the MooTools</h3>
<p>While the original version used jQuery (1.2.6 at that time) and lots of people like it, we (mootoolers) have a different approach to code specially emphasizing on OOP techniques and readability. Part of the issue might not be jQuery per-se and be Dave&#039;s code that was a little obfuscated, but it anyways has a lot of room for improvement.<br />
This article will just describe differences between the MooTools implementation and the Dave&#039;s jQuery one, so unless stated otherwise, everything from the ALA article applies here.</p>
<h3 class="section">enter the Sprites2 class</h3>
<p>Part of the requirements I imposed myself when porting to MooTools was that it had to be easier to use than the jQuery version, and that it should give a certain degree of flexibility to extend the possible transition effects.<br />
End result is a class intuitively named Sprites2, where you gracefully pass named parameters:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">new</span> Sprites2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>mode<span style="color: #339933;">:</span> <span style="color: #3366CC;">'fade'</span><span style="color: #339933;">,</span> item_selector<span style="color: #339933;">:</span> <span style="color: #3366CC;">'ul.nav a'</span><span style="color: #339933;">,</span> duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">250</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

Now that&#039;s classy! [stupid pun, I know]<br />
Notice that these parameters above are the class defaults, so you could have just as easily used <var>new Sprites2();</var> and the end result would be the same.</p>
<h3 class="section">changes to original code and idea</h3>
<p>Apart from moving the code to a class, a change to note is the removal of the :active state from the <acronym title="Cascading Style Sheets">CSS</acronym> and the <var>setActive</var> parameter in the <acronym title="JavaScript">JS</acronym> code. This was mostly a personal preference as the :active pseudo-class created a few issues when holding the mouse on the element and releasing outside of it. Other notable changes in the <acronym title="Cascading Style Sheets">CSS</acronym> are the simplification of the implementation/adaptation process by using a simpler approach on the styles (for example, using <var>height:100%;</var> where applicable instead of <a href="http://www.alistapart.com/d/sprites2/examples/example-script.css">redefining height:48px; 5 times in the <acronym title="Cascading Style Sheets">CSS</acronym></a>).</p>
<p>MooTools also has a free &#034;upgrade&#034; built-in: animations have an internal state so that if you roll out from the element and enter it again, the transition will continue to run smoothly. In contrast, jQuery version (may be by code design, maybe because of a library limitation) flickers madly when you roll over and out of the element repeatedly with an interval less than the effect duration.</p>
<p>Also to note, and as mentioned, the Sprites2 class can be easily extended outside of the sprites2.js file with just a few lines of code to create for example a horizontal slide effect:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Sprites2.<span style="color: #660066;">implement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  effects<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    slide_x<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>fx_element<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      fx_element.<span style="color: #660066;">store</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'width'</span><span style="color: #339933;">,</span> fx_element.<span style="color: #660066;">getSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">x</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setStyles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'width'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">show_fn</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>fx_element<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> fx_element.<span style="color: #660066;">tween</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'width'</span><span style="color: #339933;">,</span> fx_element.<span style="color: #660066;">retrieve</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'width'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hide_fn</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>fx_element<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> fx_element.<span style="color: #660066;">tween</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'width'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>and you can start using it right away with <var>new Sprites2({mode: &#039;slide_x&#039;});</var>. This obviously works any way you want, as you can manually add or remove the effects directly in the source file.</p>
<h3 class="section">download, full demo and requirements</h3>
<p>The example here is running on a standard <a href="http://ajax.googleapis.com/ajax/libs/mootools/1.2.1/mootools-yui-compressed.js">google hosted version of mootools 1.2.1</a>. For those of you that use custom builds, just be sure to add FX.Tween and FX.Morph to your standard array of includes (morph is only used by the &#034;animate&#034; effect, so if you don&#039;t use that one you can exclude it.)</p>
<p><a href="http://sandbox.gonchuki.com/sprites2/sprites2_moo.html">View full online demo</a></p>
<p><a href="http://sandbox.gonchuki.com/sprites2/sprites2.zip" rel="download" class="download">Download Sprites2</a> for MooTools [zip].</p>
<p><strong>new!</strong> &#8211; by popular demand (not really), I have just set-up a git repo so you can fork the latest source: <a href="http://github.com/gonchuki/sprites2-moo">http://github.com/gonchuki/sprites2-moo</a></p>
<p><strong>License:</strong> <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a><br />
<small>disclaimer: original code did not contain any license whatsoever (like all ALA code), so I assumed a CC share-alike license applies perfectly in this case.</small><br />]]></content:encoded>
			<wfw:commentRss>http://blog.gonchuki.com/archives/css-sprites2-its-mootools-time/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>the web is event-driven, and so is your code</title>
		<link>http://blog.gonchuki.com/archives/the-web-is-event-driven-and-so-is-your-code/</link>
		<comments>http://blog.gonchuki.com/archives/the-web-is-event-driven-and-so-is-your-code/#comments</comments>
		<pubDate>Fri, 03 Aug 2007 05:08:52 +0000</pubDate>
		<dc:creator>gonchuki</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://blog.gonchuki.com/archives/the-web-is-event-driven-and-so-is-your-code/</guid>
		<description><![CDATA[You click on something and then something else happens, you scroll the window and something moves around.
Welcome to the event-driven web, a concept that still some developers fail to assimilate.

the event-driven web
Developers need to understand that events are (or should be) everywhere, whether on DOM elements, over the HTTP protocol on in plain old javascript [...]]]></description>
			<content:encoded><![CDATA[<p>You click on something and then something else happens, you scroll the window and something moves around.<br />
Welcome to the event-driven web, a concept that still some developers fail to assimilate.<br />
<span id="more-14"></span></p>
<h3 class="section">the event-driven web</h3>
<p>Developers need to understand that events are (or should be) everywhere, whether on <acronym title="Document Object Model">DOM</acronym> elements, <a href="http://alex.dojotoolkit.org/?p=545">over the <acronym title="HyperText Transfer Protocol">HTTP</acronym> protocol</a> on in plain old javascript objects.<br />
Now, the former two methods are well agreed and documented, yet the later is still an undiscovered world for many developers. </p>
<h3 class="section">event driven&#8230; objects?</h3>
<p>Think about it, you attach events to <acronym title="Document Object Model">DOM</acronym> elements, you respond to events pushed by the server yet you are still using a rigid callback based architecture in your objects and pseudo-Classes. <strong>That&#039;s plain wrong</strong>.<br />
let&#039;s take object foo as:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  bar<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>baz<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/* do some stuff here */</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>what the average developer does when they need to do something <em>after</em> <var>bar</var> is finished is to add another parameter into the call of <var>bar</var>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  bar<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>baz<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/* do some stuff here */</span>
    callback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you then need another callback to do something in the middle of <var>bar</var>, soon enough before you notice you will be growing a bloated piece of code. You will notice this approach is not extensible any more when you see you need *yet another* parameter passed into your object to add the new functionality.<br />
To keep things clear, what your object/widget/Class should do is to &#034;<em>raise it&#039;s hand</em>&#034; when it needs our attention, and allow us to externally do whatever we desire on the outside, we should not pass-in callbacks to the object, we should run our callbacks once the object told us it&#039;s proper time to do so.</p>
<h3 class="section">mootools to the rescue</h3>
<p>So yes, achieving this in plain javascript is not possible as events are only part of the <acronym title="Document Object Model">DOM</acronym> elements. However there&#039;s one nifty (yet barely known) feature in <a href="http://www.mootools.net">mootools</a> that lets us extend any object with whatever set of functions we desire. For the <a href="http://www.ruby-lang.org">Ruby</a> folks, that&#039;s <a href="http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/">mixins</a> for javascript.</p>
<p>If you are a mootools lover, you may already know that <a href="http://docs.mootools.net/Class/Class.js">Classes</a> natively implement the Events interface. What you might not know is that *any* object can be mixed-in with <a href="http://docs.mootools.net/Element/Element-Event.js">Events</a> to be that event-raiser we need.<br />
So when needing to set-up callbacks in our code we could better use:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  bar<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>baz<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/* do some stuff here */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fireEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onComplete'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
$extend<span style="color: #009900;">&#40;</span>foo<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">new</span> Events<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The result of this is that our object <var>foo</var> can now raise events, ones that we can attach to at *any* point of our code by the simple means of:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">foo.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onComplete'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hello from foo's onComplete&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>and any time you need a new callback you can just set up another event and respond to it accordingly. Setting up an event of course, is as simple as adding a new <var>fireEvent</var> call where necessary and no more fiddling around with you perfectly built object.</p>
<h3 class="section">conclusion</h3>
<p>Events are everywhere on web development, and it&#039;s our job to use them where applicable and <strong>know</strong> how to do it. Events for regular objects help you keep your code extensible and semantically consistent with the rest of your <acronym title="Document Object Model">DOM</acronym>-based code, and that is a Good Thing.</p>
<p><strong>PS:</strong> for those out there not using mootools or not needing it&#039;s whole range of functionalities, there&#039;s also the tiny <a href="http://livepipe.net/projects/object_event/">Object.Event</a> library that basically allows you to reach the same results, with a <a href="http://www.prototypejs.org/">prototype</a>-like syntax.</p>
<p><strong>2008-07-13 Update:</strong> Having re-read this article many times, I must warn you that I do not fully agree any more to many of the points exposed at that time, so take the lecture with a grain of salt and simply imagine that the article was correctly written and explained.<br />
Still, there&#039;s a lot of potential in using custom events inside your javascript code and I encourage you to investigate more into the topic.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.gonchuki.com/archives/the-web-is-event-driven-and-so-is-your-code/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
