<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>JavaScriptly</title> <link>http://javascriptly.com</link> <description>JavaScript Techniques, Tricks and News</description> <lastBuildDate>Tue, 06 Sep 2011 04:57:52 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.2.1</generator> <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/javascriptly" /><feedburner:info uri="javascriptly" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><image><link>http://javascriptly.com</link><url>http://javascriptly.com/wp-content/themes/Padangan/images/javascriptly-logo.gif</url><title>JavaScriptly</title></image><item><title>Prototype vs Class Inheritance in YUI 3</title><link>http://feedproxy.google.com/~r/javascriptly/~3/dS9Dd1h1oB8/</link> <comments>http://javascriptly.com/2010/01/prototype-vs-class-inheritance-in-yui-3/#comments</comments> <pubDate>Thu, 07 Jan 2010 11:27:02 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[Development News]]></category> <category><![CDATA[Solution]]></category> <category><![CDATA[OOP]]></category> <category><![CDATA[YUI 3.x]]></category><guid isPermaLink="false">http://javascriptly.com/?p=142</guid> <description><![CDATA[<p style="text-align: justify;">As I mentioned in the post <a title="Permanent Link to YUI 3.0 – Changes From the Root" href="http://javascriptly.com/2008/08/yui-30-changes-from-the-root/">YUI 3.0 – Changes From the Root</a>, <a href="http://developer.yahoo.com/yui/3/" target="_blank">YUI 3</a> is a radical evolvement of the framework. One of the&#8230;</p>]]></description> <content:encoded><![CDATA[<p style="text-align: justify;">As I mentioned in the post <a title="Permanent Link to YUI 3.0 – Changes From the Root" href="http://javascriptly.com/2008/08/yui-30-changes-from-the-root/">YUI 3.0 – Changes From the Root</a>, <a href="http://developer.yahoo.com/yui/3/" target="_blank">YUI 3</a> is a radical evolvement of the framework. One of the most interesting features it provides with is about much better support for OOP.</p><p style="text-align: justify;">YUI 3 supports two inheritance patterns via object prototype and “class”. Let’s examine the differences and advantages of each pattern.</p><p><a href="http://javascriptly.com/wp-content/uploads/2010/01/yui3-logo.png" rel="lightbox"><img src="http://javascriptly.com/wp-content/uploads/2010/01/yui3-logo.png" alt="" title="yui3-logo" width="464" height="255" class="alignleft size-full wp-image-143" /></a></p><h2>Prototypal Inheritance in YUI 3</h2><p style="text-align: justify;"><a href="http://javascript.crockford.com/prototypal.html" target="_blank">Suggested by Douglas Crockford</a>, prototypal inheritance pattern is to deal with objects, i.e. objects inherit from objects. YUI 3 supports prototypal inheritance from its core; it means you can utilize the pattern by including the seed file only:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> 
    src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div><p style="text-align: justify;">Now, for example, we have an object named <code>animal</code>:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> animal <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Animal&quot;</span><span style="color: #339933;">,</span>
    level<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;top&quot;</span><span style="color: #339933;">,</span>
    say<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; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;I am an &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; at &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">level</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; level.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>animal.<span style="color: #660066;">say</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// I am an Animal at top level.</span></pre></div></div><p style="text-align: justify;">And you want to create an <code>elephant</code> object that is an inheritance of <code>animal</code>. It’s so easy to do:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> elephant <span style="color: #339933;">=</span> Y.<span style="color: #660066;">Object</span><span style="color: #009900;">&#40;</span>animal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p style="text-align: justify;">Of course, you’ll need to modify the newly-born creature to make it a true elephant by overriding its parent properties and adding some new ones:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// override an animal’s property</span>
elephant.<span style="color: #660066;">level</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;second&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// add elephant’s own properties</span>
elephant.<span style="color: #660066;">trunk</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
elephant.<span style="color: #660066;">tusks</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span></pre></div></div><p style="text-align: justify;">However if you want the elephant to say more about its own belongings, you’ll have to redefine <code>say</code> method completely.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">elephant.<span style="color: #660066;">say</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; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;I am an &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; at &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">level</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; level.&quot;</span>
        <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; I have &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">trunk</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; trunk and &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tusks</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; tusks.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>elephant.<span style="color: #660066;">say</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// I am an Animal at second level. I have 1 trunk and 2 tusks.</span></pre></div></div><p>I think this pattern is quite easy to understand but before going forward to the next one, here are what we learn from it:</p><ol><li style="text-align: justify;">We can      create a new object that inherits all the properties and methods from an      existing object.</li><li style="text-align: justify;">We can      customize the new object by overwriting some/all of the members or adding      new ones.</li><li style="text-align: justify;"><p style="text-align: justify;">The dark part is an overwritten property has no way to reference its parent’s property.</p><p style="text-align: justify;"><strong>Note:</strong> In this pattern, the child’s properties do not really overwrite its parent’s ones but they just take higher precedence. So if we delete a child’s method, the parent’s one will be back magically. For example:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">delete</span> elephant.<span style="color: #660066;">say</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>elephant.<span style="color: #660066;">say</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// I am an Animal at second level.</span></pre></div></div></li></ol><h2>“Class” Inheritance in YUI 3</h2><p style="text-align: justify;">Yes, I know there are no true classes in JavaScript, however we can make <strong>constructor functions act like classes</strong> for OOP (you can read more from the book <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FObject-Oriented-JavaScript-high-quality-applications-libraries%2Fdp%2F1847194141%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1262840077%26sr%3D8-1&amp;tag=feed0e-20&amp;linkCode=ur2&amp;camp=178" target="_blank">Object-Oriented JavaScript</a> by Stoyan Stefanov).</p><p style="text-align: justify;"><strong>Note:</strong> From now on, for easier to understand the pattern, I’ll call all constructor functions (that are capitalized in examples) classes; don’t be confused.</p><p style="text-align: justify;">First, we’ll mimic above example by creating  <code>Animal</code> and <code>Elephant</code> classes as follows:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// parent class</span>
<span style="color: #003366; font-weight: bold;">function</span> Animal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Animal&quot;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">// parent’s properties</span>
Animal.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">getName</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; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Animal.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">setName</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// child class</span>
<span style="color: #003366; font-weight: bold;">function</span> Elephant<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div><p style="text-align: justify;">YUI 3 supports class inheritance via <a href="http://developer.yahoo.com/yui/3/api/YUI%7Eoop.html">oop</a> module and because this is used widely by almost all other modules, you’ll rarely have to include it explicitly. However, for testing, just call <code>Y.extend()</code> method within <code>oop</code>’s scope (that let <code>Elephant</code> inherit <code>Animal</code> as you can guess):</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">YUI<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #003366; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'oop'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>Y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    Y.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Elephant<span style="color: #339933;">,</span> Animal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// test code here</span>
    <span style="color: #006600; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Next we’ll do some tests:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// create an object of Elephant</span>
<span style="color: #003366; font-weight: bold;">var</span> jumbo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Elephant<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>jumbo.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// undefined</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>jumbo.<span style="color: #660066;">getName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// function</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// set name for the Elephant object			</span>
jumbo.<span style="color: #660066;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Jumbo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>jumbo.<span style="color: #660066;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Jumbo</span></pre></div></div><p style="text-align: justify;">You can see from the example, <code>jumbo</code> object which is an instance of <code>Elephant</code> class only inherit members of the prototype, not “private” members from parent’s class and it is considered a good practice to leave all instance-specific properties as private properties and to add all the public functionality to the prototype.</p><p style="text-align: justify;">Interestingly we can add some new properties with default values to child class via arguments of <code>Y.extend()</code>, for example, to add trunk and tusks to <code>Elephant</code> class:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// create an object of Elephant “class”</span>
<span style="color: #003366; font-weight: bold;">var</span> jumbo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Elephant<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>jumbo.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// undefined</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>jumbo.<span style="color: #660066;">getName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// function</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// set name for the elephant				</span>
jumbo.<span style="color: #660066;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Jumbo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>jumbo.<span style="color: #660066;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Jumbo</span></pre></div></div><p style="text-align: justify;">Last but not least, class inheritance has very powerful feature: <code>superclass</code> access. To see how it works, we’ll add a method to <code>Animal</code> class to let it say something.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// parent class</span>
<span style="color: #003366; font-weight: bold;">function</span> Animal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// parent’s method</span>
Animal.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">say</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; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;I am an animal.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div><p style="text-align: justify;">Then, we’ll overwrite say method in <code>Elephant</code> class, yet still be able to keep what <code>Animal</code> can say about.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Y.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Elephant<span style="color: #339933;">,</span> Animal<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>trunk<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> tusks<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Elephant.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">say</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; font-weight: bold;">return</span> Elephant.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">say</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> 
		<span style="color: #3366CC;">&quot; I have &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">trunk</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; trunk and &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tusks</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; tusks.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// test</span>
jumbo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Elephant<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>jumbo.<span style="color: #660066;">say</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// I am an animal. I have 1 trunk and 2 tusks.</span></pre></div></div><p style="text-align: justify;">In summary, “class” inheritance pattern in YUI 3 is more powerful than prototypal counterpart though harder to get it full, here are some important points:</p><ol><li style="text-align: justify;">YUI 3      allows us to implement inheritance at (pseudo) class level in JavaScript.</li><li style="text-align: justify;">Inherited      classes cannot directly access to parent’s own members, children only      inherit prototype members by default.<p style="text-align: justify;"><strong>Note:</strong> Well, it is possible to inherit parent’s own properties by initializing the parent constructor from the child (but it is not always a good OOP practice). See example:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// parent class</span>
<span style="color: #003366; font-weight: bold;">function</span> Animal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Animal&quot;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">// child class</span>
<span style="color: #003366; font-weight: bold;">function</span> Elephant<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// initialize the parent using the child as &quot;this&quot;</span>
    Elephant.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">constructor</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Y.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Elephant<span style="color: #339933;">,</span> Animal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// test</span>
jumbo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Elephant<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>jumbo.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Animal</span></pre></div></div></li><li style="text-align: justify;">You      can gain access to the parent’s overridden methods via superclass.</li></ol><h2>Conclusion</h2><p style="text-align: justify;">Code reuse is considered as the most confusing part of JavaScript for it lacks robust OOP features that many modern programming languages provide by default. YUI 3 with the two inheritance patterns we’ve gone through is trying to fill the gap.</p><p style="text-align: justify;">Prototypal inheritance is easier to understand but limit in object inheritance only. To get full OO support in YUI 3, it is advisable to study “class” inheritance pattern provided by the framework.</p><div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://javascriptly.com/2009/12/coremvc-an-jquery-based-mvc-framework/" rel="bookmark">corMVC: An jQuery-based MVC Framework</a></li><li><a href="http://javascriptly.com/2008/08/is-future-of-javascript-in-harmony/" rel="bookmark">Is Future of JavaScript in Harmony?</a></li><li><a href="http://javascriptly.com/2010/01/why-do-we-need-google-closure/" rel="bookmark">Why Do We Need Google Closure?</a></li></ul></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/javascriptly?a=dS9Dd1h1oB8:srjvb5H0K8M:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dS9Dd1h1oB8:srjvb5H0K8M:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=dS9Dd1h1oB8:srjvb5H0K8M:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dS9Dd1h1oB8:srjvb5H0K8M:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=dS9Dd1h1oB8:srjvb5H0K8M:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dS9Dd1h1oB8:srjvb5H0K8M:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=dS9Dd1h1oB8:srjvb5H0K8M:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dS9Dd1h1oB8:srjvb5H0K8M:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=dS9Dd1h1oB8:srjvb5H0K8M:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dS9Dd1h1oB8:srjvb5H0K8M:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dS9Dd1h1oB8:srjvb5H0K8M:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=l6gmwiTKsz0" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2010/01/prototype-vs-class-inheritance-in-yui-3/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://javascriptly.com/2010/01/prototype-vs-class-inheritance-in-yui-3/</feedburner:origLink></item> <item><title>Why Do We Need Google Closure?</title><link>http://feedproxy.google.com/~r/javascriptly/~3/oK5lxx-P18Y/</link> <comments>http://javascriptly.com/2010/01/why-do-we-need-google-closure/#comments</comments> <pubDate>Tue, 05 Jan 2010 03:52:26 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[Development News]]></category> <category><![CDATA[Compressor]]></category> <category><![CDATA[Google]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[Lib]]></category><guid isPermaLink="false">http://javascriptly.com/?p=136</guid> <description><![CDATA[<h2>Why Google Closure?</h2><p><img src="http://javascriptly.com/wp-content/uploads/2010/01/google-closure-logo.png" alt="" title="google-closure-logo" width="128" height="128" class="alignleft size-full wp-image-139" /></p><p style="text-align: justify;">When a new JavaScript library arrives, a question is often raised reasonably: “<em>Why does the world need another library?</em>” while we’ve already had too many good ones like jQuery, Prototype, Mootools, YUI… you name&#8230;</p>]]></description> <content:encoded><![CDATA[<h2>Why Google Closure?</h2><p><img src="http://javascriptly.com/wp-content/uploads/2010/01/google-closure-logo.png" alt="" title="google-closure-logo" width="128" height="128" class="alignleft size-full wp-image-139" /></p><p style="text-align: justify;">When a new JavaScript library arrives, a question is often raised reasonably: “<em>Why does the world need another library?</em>” while we’ve already had too many good ones like jQuery, Prototype, Mootools, YUI… you name it.</p><p style="text-align: justify;">To find the right answer, just have a look at the package offered by Google under the name of <a href="http://code.google.com/closure/" target="_blank">Closure project</a>. We’ll find there three sub-projects:</p><ol><li style="text-align: justify;"><strong><a href="http://code.google.com/closure/compiler/" target="_blank">Closure Compiler</a></strong>: a JavaScript optimizer.      This is not a “<em>yet</em> <em>another JavaScript compressor</em>”, it      can do more: Besides compressing your codes it can analyze your      code and remove “dead” parts, correct wrong syntax, check variable      references/types, warn about common pitfalls. The really good news is it      comes with a Firebug extension, called Closure Inspector, that allows us      to debug compressed code by linking obfuscated lines to original ones.</li><li style="text-align: justify;"><strong><a href="http://code.google.com/closure/library/" target="_blank">Closure Library</a></strong>: a JavaScript library (as we expect) intended for use with  Closure Compiler. Basically it can do all the common tasks like other libs such as DOM manipulation, server communication, animation/effect and encloses a large set of reusable UI widgets and controls.</li><li style="text-align: justify;"><strong><a href="http://code.google.com/closure/templates/" target="_blank">Closure Templates</a></strong>: is not just a client-side JavaScript template engine like Pure but can be used in server-side with Java.</li></ol><p style="text-align: justify;"><strong>Closure Library</strong> is a big library with more than 400 JavaScript files well organized into Java-like directory structure. We’ll find there something like packages, classes/sub-classes with Java-doc syntax description (in fact, you know, there are no true packages/classes in JavaScript.)</p><p><span id="more-136"></span></p><p style="text-align: justify;">Google don’t boast the library in term of better speed or DOM manipulation techniques, the advantages of Closure lie in application deployment. Powered by <strong>Closure Compiler</strong>, application using <strong>Closure Library</strong> can be highly object-oriented in development while can be compact, fast and safe in production. An example is worth a thousand words, so let’s see <a href="http://code.google.com/closure/library/docs/gettingstarted.html">official “<em>Hello World</em>” application</a> in development and deployment.</p><h3>1. JavaScript</h3><p style="text-align: left;">+ Get and put the lib into a directory called <code>closure-library-read-only</code></p><p style="text-align: left;">+ Create a JavaScript file (<code>hello.js</code>):</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;">goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'goog.dom'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> sayHi<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> newHeader <span style="color: #339933;">=</span> goog.<span style="color: #660066;">dom</span>.<span style="color: #660066;">createDom</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'h1'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'style'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'background-color:#EEE'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Hello world!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    goog.<span style="color: #660066;">dom</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</span><span style="color: #339933;">,</span> newHeader<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p style="text-align: justify;">The most notable line is the first line that <strong>dynamically</strong> includes <code>goog.dom</code> for DOM manipulation (which is defined in <code>dom/dom.js</code> file). All other “packages” can be included in the same way.</p><h3>2. HTML</h3><p style="text-align: left;">Now, create a HTML to display the result (<code>hello.html</code>):</p><div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;closure-library-read-only/closure/goog/base.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;hello.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body</span> <span style="color: #000066;">onload</span>=<span style="color: #ff0000;">&quot;sayHi()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p style="text-align: justify;">We’ll need to add <code>base.js</code> which is a bootstrap file to load other JavaScript files required and of course the <code>hello.js</code> file we’ve just created.</p><h3>3. Deployment</h3><p style="text-align: justify;">The above example works as long as you don’t mind to deploy 400+ files and let the application load all necessary JavaScript files (12 files weighted 356KB as captured image from Firebug below).</p><p style="text-align: left;"><a rel="lightbox" href="http://javascriptly.com/wp-content/uploads/2010/01/google-closure-hello-world-js-files.png"><img class="size-medium wp-image-137 aligncenter" title="google-closure-hello-world-js-files" src="http://javascriptly.com/wp-content/uploads/2010/01/google-closure-hello-world-js-files-499x139.png" alt="Google Closure JS Files Loaded" width="499" height="139" rel="lightbox" /></a></p><p style="text-align: justify;">However, thinking of an application with hundreds of JavaScript files required, usability would be a nightmare for this approach and that’s why Google provides a tool named <strong><a href="http://code.google.com/closure/library/docs/calcdeps.html">Dependency Calculation Script</a></strong> to relief the pain. The tool, as its name implies, can calculate dependencies for application and create a single JavaScript file. Using the tool in conjunction with <strong>Closure Compiler</strong> to remove redundant code you will end up with perfect result: all-in-one, small, fast and safe JavaScript file.</p><p style="text-align: justify;">Here is how to create the final script:</p><div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">closure-library-read-only<span style="color: #000000; font-weight: bold;">/</span>closure<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>calcdeps.py <span style="color: #660033;">-i</span> hello.js \
  <span style="color: #660033;">-p</span> closure-library-read-only <span style="color: #660033;">-o</span> compiled \
  <span style="color: #660033;">-c</span> compiler.jar \
  <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;--compilation_level=ADVANCED_OPTIMIZATIONS&quot;</span> \
  <span style="color: #000000; font-weight: bold;">&gt;</span> hello-compiled.js</pre></div></div><p style="text-align: justify;"><strong>Note:</strong> Because the advanced optimization functionality of <strong>Closure Compiler</strong> is to include only code portions that are called in file, before running above compilation command you’ll need to modify script <code>hello.js</code> slightly to execute <code>onload</code> function as follows:</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'goog.dom'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> sayHi<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> newHeader <span style="color: #339933;">=</span> goog.<span style="color: #660066;">dom</span>.<span style="color: #660066;">createDom</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'h1'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'style'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'background-color:#EEE'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Hello world!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    goog.<span style="color: #660066;">dom</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</span><span style="color: #339933;">,</span> newHeader<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
window.<span style="color: #000066;">onload</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>
    sayHi<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: #339933;">;</span></pre></td></tr></table></div><p>And don’t forget to remove <code>onload</code> call from HTML:</p><div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;hello-compiled.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p style="text-align: justify;">Now, look at the final result: a 3KB JavaScript file loaded in 64ms vs 356KB in 414ms respectively of un-optimized version that interprets to more than one hundred times better in size and seven times better in loading speed. (FYI, gzipping output, we’ll have less than 1KB file load only).</p><p><a href="http://javascriptly.com/wp-content/uploads/2010/01/google-closure-hello-world-js-files-optimized.png" rel="lightbox"><img src="http://javascriptly.com/wp-content/uploads/2010/01/google-closure-hello-world-js-files-optimized-500x56.png" alt="Google Closure JS File Load Optimized" title="google-closure-hello-world-js-files-optimized" width="500" height="56" class="aligncenter size-medium wp-image-138" /></a></p><h2>Conclusion</h2><p style="text-align: justify;">I neither see <strong>Closure Library</strong> as yet-another-JS-lib nor see <strong>Closure Compiler</strong> as yet-another-JS-compressor. They together provide a complete solution to create and deploy sophisticated web apps with confidence.</p><p style="text-align: justify;">It would not be a surprise that Google developers are using similar tools internally to create great web apps like GMail, Wave so it makes sense that we can utilize Closure to build a web application with the same level of efficiency, completion and beauty.</p><ul><li><strong><a href="http://javascriptly.com/examples/google-closure-example.zip">Download Example Code</a></strong></li></ul><div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://javascriptly.com/2008/09/javascript-in-google-chrome/" rel="bookmark">JavaScript in Google Chrome Not So That Fast</a></li><li><a href="http://javascriptly.com/2008/08/yui-30-changes-from-the-root/" rel="bookmark">YUI 3.0 – Changes From the Root</a></li><li><a href="http://javascriptly.com/2008/09/lightningdom-to-balance-dom-vs-innerhtml/" rel="bookmark">LightningDOM to Balance DOM vs innerHTML</a></li></ul></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/javascriptly?a=oK5lxx-P18Y:g3rtnjIsyEo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=oK5lxx-P18Y:g3rtnjIsyEo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=oK5lxx-P18Y:g3rtnjIsyEo:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=oK5lxx-P18Y:g3rtnjIsyEo:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=oK5lxx-P18Y:g3rtnjIsyEo:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=oK5lxx-P18Y:g3rtnjIsyEo:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=oK5lxx-P18Y:g3rtnjIsyEo:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=oK5lxx-P18Y:g3rtnjIsyEo:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=oK5lxx-P18Y:g3rtnjIsyEo:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=oK5lxx-P18Y:g3rtnjIsyEo:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=oK5lxx-P18Y:g3rtnjIsyEo:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=l6gmwiTKsz0" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2010/01/why-do-we-need-google-closure/feed/</wfw:commentRss> <slash:comments>3</slash:comments> <feedburner:origLink>http://javascriptly.com/2010/01/why-do-we-need-google-closure/</feedburner:origLink></item> <item><title>Best IE6 “PNG Alpha-Transparency” Fixes</title><link>http://feedproxy.google.com/~r/javascriptly/~3/yhlKB6ckCm8/</link> <comments>http://javascriptly.com/2009/12/best-ie6-png-fixes/#comments</comments> <pubDate>Mon, 28 Dec 2009 06:43:19 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[Solution]]></category> <category><![CDATA[alpha]]></category> <category><![CDATA[fix]]></category> <category><![CDATA[hack]]></category> <category><![CDATA[IE6]]></category> <category><![CDATA[PNG]]></category> <category><![CDATA[transparent]]></category><guid isPermaLink="false">http://javascriptly.com/?p=63</guid> <description><![CDATA[<p style="text-align: justify;">You are likely living in civilized society with Firefox, Safari, Chrome or Internet Explorer 7/8 installed in your computers. Yet, some are still in the dark with the older Microsoft browsers named Internet Explorer 6 (IE6) or, worse,&#8230;</p>]]></description> <content:encoded><![CDATA[<p style="text-align: justify;">You are likely living in civilized society with Firefox, Safari, Chrome or Internet Explorer 7/8 installed in your computers. Yet, some are still in the dark with the older Microsoft browsers named Internet Explorer 6 (IE6) or, worse, older versions. One of the most painful facts about IE6 is that it does not natively support PNG alpha-channel transparency.</p><p style="text-align: justify;">Microsoft probably thought that GIF transparency &#8212; where each pixel is either fully transparent or a solid color, not anything between &#8212; should be enough for the world. But it&#8217;s not! Just compare three &#8220;<strong>JavaScriptly</strong>&#8221; logos in transparent GIF, transparent PNG and semi-transparent PNG images on a web page, you&#8217;ll recognize the differences.</p><ul type="disc"><li><strong><a href="http://javascriptly.com/examples/ie6-png-fix/non-fixed-demo/">View      Non-Fixed PNG Demo</a></strong></li></ul><div id="attachment_64" class="wp-caption aligncenter" style="width: 377px"><a href="http://javascriptly.com/wp-content/uploads/2009/12/gif-png-compare.png" rel="lightbox"><img class="size-medium wp-image-64" title="gif-png-compare" src="http://javascriptly.com/wp-content/uploads/2009/12/gif-png-compare.png" alt="Transparent PNG vs Transparent GIF" width="367" height="439" /></a><p class="wp-caption-text">Transparent PNG vs Transparent GIF</p></div><p style="text-align: justify;">(1) The font edge of GIF logo has side-effect which can be only fixed by choosing &#8220;color matte&#8221; exactly like background color when exporting the image (but we can do nothing optimal if the background has multi-colors like a picture.)</p><p style="text-align: justify;">(2) Only PNG can be semi-transparent like the last logo, GIF is out of luck.</p><p><span id="more-63"></span></p><div id="attachment_65" class="wp-caption aligncenter" style="width: 474px"><a href="http://isie6dead.com/"><img class="size-medium wp-image-65" title="ie6-not-dead-yet" src="http://javascriptly.com/wp-content/uploads/2009/12/ie6-not-dead-yet.png" alt="Is IE6 dead yet?" width="464" height="285" /></a><p class="wp-caption-text">Is IE6 dead yet?</p></div><p style="text-align: justify;">So, transparent PNG is obviously a better choice except for the mentioned problem: IE6, which is still being used by at least one tenth of net citizens at the time I&#8217;m writing this blog post, does not like it. Well, if you care about that 10% (poor) audiences, here are some of the best hacks to fix the issue.</p><h2>TwinHelix</h2><p style="text-align: justify;"><strong><a href="http://www.twinhelix.com/css/iepngfix/" target="_blank">TwinHelix&#8217;s fix</a></strong> is one of the most popular used solutions for IE6 PNG alpha-transparency issue. This uses CSS &#8220;behaviors&#8221; to execute fixing scripts which basically apply Microsoft&#8217;s proprietary filter called the <em>AlphaImageLoader</em> to the PNG images on the web page.</p><ul type="disc"><li><a title="TwinHelix's Fix Demo" href="http://javascriptly.com/examples/ie6-png-fix/twinhelix-demo/"><strong>View      TwinHelix Fix Demo</strong></a></li><li><a href="http://javascriptly.com/examples/ie6-png-fix/twinhelix-demo.zip"><strong>Download      TwinHelix Demo Code</strong></a></li></ul><h3>Installation</h3><ol type="1"><li>Download      the fix <a href="http://www.twinhelix.com/css/iepngfix/iepngfix.zip">here</a>.</li><li>Extract,      copy and paste <code>iepngfix.htc</code> and <code>blank.gif</code> into your website directory.</li><li>Add      the following CSS:<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">&lt;style type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #00AA00;">&gt;</span>
    img<span style="color: #00AA00;">,</span> div<span style="color: #00AA00;">,</span> a<span style="color: #00AA00;">,</span> input <span style="color: #00AA00;">&#123;</span> behavior<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">iepngfix.htc</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#125;</span>
&lt;/style<span style="color: #00AA00;">&gt;</span></pre></div></div><p style="text-align: justify;">You&#8217;ll need to include tags or elements you want to apply the fix only. For example, you can apply fix to images only:</p><div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">img <span style="color: #00AA00;">&#123;</span> behavior<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">iepngfix.htc</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div><p>or to the logo element only:</p><div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#logo</span> <span style="color: #00AA00;">&#123;</span> behavior<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">iepngfix.htc</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div><p><strong>Notice:</strong></p><p style="text-align: justify;">+ Path to .HTC (and the blank GIF file) is relative to the HTML document location, not relative to CSS file.</p><p style="text-align: justify;">+ Path to blank GIF can be modified if the file <em>blank.gif</em> is at a sub-directory by opening .HTC file in a text editor and change the <em>blankImg</em> variable like:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">IEPNGFix.<span style="color: #660066;">blankImg</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'images/blank.gif'</span><span style="color: #339933;">;</span></pre></div></div></li><li style="text-align: justify;">A      JavaScript add-on is required to support CSS1 <code>background-repeat</code> and <code>background-position</code>.      Just include it if needed:<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;iepngfix_tilebg.js&gt;&lt;/script&gt;</span></pre></div></div></li></ol><h2>SuperSleight</h2><p style="text-align: justify;">SuperSleight fix comes along with a <strong><a href="http://24ways.org/2007/supersleight-transparent-png-in-ie6" target="_blank">great article</a></strong> about the issue and resolution. This fix is also to apply the <em>AlphaImageLoader </em>filter to images but it use JavaScript directly, not via CSS behaviors like TwinHelix&#8217;s, so that it&#8217;s much easier to install.</p><ul type="disc"><li><a title="SuperSleight Fix Demo" href="http://javascriptly.com/examples/ie6-png-fix/supersleight-demo/"><strong>View      SuperSleight Fix Demo</strong></a></li><li><a href="http://javascriptly.com/examples/ie6-png-fix/supersleight-demo.zip"><strong>Download      SuperSleight Demo Code</strong></a></li></ul><h3>Installation</h3><ol type="1"><li>Download      SuperSleight <a href="http://24ways.org/code/supersleight-transparent-png-in-ie6/supersleight.zip">here</a>.</li><li>Extract,      copy and paste <code>supersleight.js</code> and <code>x.gif</code> files into appropriated directories.</li><li style="text-align: justify;">Link      the script file into your document inside      conditional comments so that it is delivered to IE6 (or older)      only.<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">if</span> lte IE <span style="color: #CC0000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;supersleight.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;!</span><span style="color: #009900;">&#91;</span>endif<span style="color: #009900;">&#93;</span><span style="color: #339933;">--&gt;</span></pre></div></div></li><li style="text-align: justify;">You      can change path to blank GIF file (<em>x.gif</em>)      if it&#8217;s not at the same directory with the JS file by modifying variable <em>shim</em> at near top of the scripts      like:<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> shim <span style="color: #339933;">=</span> <span style="color: #3366CC;">'images/x.gif'</span><span style="color: #339933;">;</span></pre></div></div></li><li style="text-align: justify;">Instead      applying the fix to all PNGs, you can limit the script to just one part of      the page by passing an ID of an element to:<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">supersleight.<span style="color: #660066;">limitTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p style="text-align: justify;">before initialization function at bottom of the file:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">supersleight.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><li>jQuery      plugin of SuperSleight can be downloaded from <strong><a href="http://allinthehead.com/retro/338/supersleight-jquery-plugin">this page</a></strong>.</li></li></ol><h2>DD_belatedPNG</h2><p style="text-align: justify;">The major problem of TwinHelix&#8217;s, SuperSleight fixes and <a href="http://www.campbellsdigitalsoup.co.uk/about/png-fix/">various</a> <a href="http://labs.unitinteractive.com/unitpngfix.php">derived</a> <a href="http://www.komodomedia.com/blog/2007/11/css-png-image-fix-for-ie/">solutions</a> is that <em>AlphaImageLoader </em>filter appears not very efficient and it can slowdown a page load significantly if there are many PNG images to be fixed.</p><p style="text-align: justify;">In 2008, a <strong><a href="http://www.dillerdesign.com/experiment/DD_belatedPNG/" target="_blank">new technique</a></strong> to solve the issue was discovered by Drew Diller when he found that alpha-transparent PNG images show up correctly in VML (Vector Markup Language) fill elements.</p><ul type="disc"><li><a title="DD_belatedPNG Fix Demo" href="http://javascriptly.com/examples/ie6-png-fix/dd_belatedpng-demo/"><strong>View      DD_belatedPNG Fix Demo</strong></a></li><li><a href="http://javascriptly.com/examples/ie6-png-fix/dd_belatedpng-demo.zip"><strong>Download      DD_belatedPNG Demo Code</strong></a></li></ul><h3>Installation</h3><ol type="1"><li>Download the latest version of DD_belatedPNG <a href="http://www.dillerdesign.com/experiment/DD_belatedPNG/#download">here</a>.</li><li>Extract,      copy and paste <code>DD_belatedPNG_x.x.x.js</code> file into website directory</li><li>Refer      to the script file in your document and add another script node to execute      the fix inside conditional comments to let it be loaded in IE6 only:<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;!--[if IE 6]&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;DD_belatedPNG.js&quot;&gt;&lt;/script&gt;
    <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>  
        DD_belatedPNG.<span style="color: #660066;">fix</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.logo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
    <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&lt;![endif]--&gt;</pre></div></div></li><li>String      argument for <code>DD_belatedPNG.fix</code> can be any valid CSS selector like <code>logo</code> class in above code snippet or a group of CSS selectors like:<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">DD_belatedPNG.<span style="color: #660066;">fix</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.class1, .class2, img'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div></li></ol><h2>Summary</h2><p style="text-align: justify;">We&#8217;ve gone though why alpha-transparent PNG should be a better choice for web developers/designers and three solutions to force Internet Explorer 6 to render it correctly.</p><p style="text-align: justify;">All solutions are based on one of two hacks: <em>AlphaImageLoader </em>filter and VML. SuperSleight looks better than TwinHelix&#8217;s but I think <strong>DD_belatedPNG</strong> appears to be the most elegant way out.</p><div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://javascriptly.com/2009/12/something-for-christmas-snowfall-in-picture/" rel="bookmark">Something for Christmas: Snowfall in Picture</a></li><li><a href="http://javascriptly.com/2010/01/why-do-we-need-google-closure/" rel="bookmark">Why Do We Need Google Closure?</a></li><li><a href="http://javascriptly.com/2008/09/comet-chat-app-meteor-server/" rel="bookmark">Simple Web Chat with Meteor Comet Server</a></li></ul></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/javascriptly?a=yhlKB6ckCm8:wzq8CMus2hw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=yhlKB6ckCm8:wzq8CMus2hw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=yhlKB6ckCm8:wzq8CMus2hw:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=yhlKB6ckCm8:wzq8CMus2hw:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=yhlKB6ckCm8:wzq8CMus2hw:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=yhlKB6ckCm8:wzq8CMus2hw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=yhlKB6ckCm8:wzq8CMus2hw:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=yhlKB6ckCm8:wzq8CMus2hw:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=yhlKB6ckCm8:wzq8CMus2hw:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=yhlKB6ckCm8:wzq8CMus2hw:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=yhlKB6ckCm8:wzq8CMus2hw:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=l6gmwiTKsz0" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2009/12/best-ie6-png-fixes/feed/</wfw:commentRss> <slash:comments>4</slash:comments> <feedburner:origLink>http://javascriptly.com/2009/12/best-ie6-png-fixes/</feedburner:origLink></item> <item><title>Something for Christmas: Snowfall in Picture</title><link>http://feedproxy.google.com/~r/javascriptly/~3/jbrO3feXAj0/</link> <comments>http://javascriptly.com/2009/12/something-for-christmas-snowfall-in-picture/#comments</comments> <pubDate>Wed, 23 Dec 2009 07:05:26 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[Misc]]></category> <category><![CDATA[effect]]></category> <category><![CDATA[snowfall]]></category><guid isPermaLink="false">http://javascriptly.com/?p=62</guid> <description><![CDATA[<p></p><p style="text-align: justify;">Now it is time for Christmas and if you want to add some snows to a picture like the below example, a cool jQuery plugin &#8212; created by <a href="http://www.somethinghitme.com/2009/12/20/snowfall-plugin-updated/" target="_blank">Jason Brown</a> &#8212; can come to help.&#8230;</p>]]></description> <content:encoded><![CDATA[<p></p><p style="text-align: justify;">Now it is time for Christmas and if you want to add some snows to a picture like the below example, a cool jQuery plugin &#8212; created by <a href="http://www.somethinghitme.com/2009/12/20/snowfall-plugin-updated/" target="_blank">Jason Brown</a> &#8212; can come to help.</p><p><a href="http://javascriptly.com/wp-content/uploads/2009/12/snow-fall-in-picture.png" rel="lightbox"><img src="http://javascriptly.com/wp-content/uploads/2009/12/snow-fall-in-picture-500x378.png" alt="" title="snow-fall-in-picture" width="500" height="378" class="aligncenter size-medium wp-image-141" /></a></p><ul><li><strong><a href="http://javascriptly.com/examples/jquery-snowfall/">View Live Demo</a></strong></li><li><strong><a href="http://javascriptly.com/examples/jquery-snowfall/snowfall.jquery.zip">Download Plugin</a></strong></li></ul><p><span id="more-62"></span></p><p style="text-align: justify;">Just include the plugin along with the latest version of jQuery (1.3.2 currently) and initiate it as the following code</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#snow_window'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">snowfall</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		flakeCount <span style="color: #339933;">:</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">,</span>
		flakeColor <span style="color: #339933;">:</span> <span style="color: #3366CC;">'#ffffff'</span><span style="color: #339933;">,</span>
		flakeIndex<span style="color: #339933;">:</span> <span style="color: #CC0000;">999999</span><span style="color: #339933;">,</span>
		minSize <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
		maxSize <span style="color: #339933;">:</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span>
		minSpeed <span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>
		maxSpeed <span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div><p style="text-align: justify;">where &#8220;<em>snow_window</em>&#8221; is the ID of target DIV in which snowflakes will fly. You can define number of snowflakes, their color, min/max sizes (just do not set too big sizes or they would look unnatural) and falling speed as parameters (<em>flakeCount</em>, <em>flakeColor</em> etc) in the initializing code.</p><p style="text-align: justify;">Of course, you can set the snowfall effect to the whole web page with</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">snowfall</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p style="text-align: justify;"><strong>Tip:</strong> It won&#8217;t work properly with fixed positioning and you must explicitly set the target DIV with &#8220;<em>position: relative</em>&#8221; or you&#8217;ll see snow falling on top corner of the page instead.</p><p style="text-align: justify;">I hope you enjoy this Christmas and can surprise your friends with the snowfall effect.</p><div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://javascriptly.com/2009/12/best-ie6-png-fixes/" rel="bookmark">Best IE6 “PNG Alpha-Transparency” Fixes</a></li><li><a href="http://javascriptly.com/2008/09/quick-useful-jquery-plugins/" rel="bookmark">Quick & Useful jQuery Plugins</a></li><li><a href="http://javascriptly.com/2009/12/firequery-an-introduction/" rel="bookmark">FireQuery - An Introduction</a></li></ul></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/javascriptly?a=jbrO3feXAj0:0kAgQ3pYCPE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=jbrO3feXAj0:0kAgQ3pYCPE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=jbrO3feXAj0:0kAgQ3pYCPE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=jbrO3feXAj0:0kAgQ3pYCPE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=jbrO3feXAj0:0kAgQ3pYCPE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=jbrO3feXAj0:0kAgQ3pYCPE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=jbrO3feXAj0:0kAgQ3pYCPE:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=jbrO3feXAj0:0kAgQ3pYCPE:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=jbrO3feXAj0:0kAgQ3pYCPE:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=jbrO3feXAj0:0kAgQ3pYCPE:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=jbrO3feXAj0:0kAgQ3pYCPE:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=l6gmwiTKsz0" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2009/12/something-for-christmas-snowfall-in-picture/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://javascriptly.com/2009/12/something-for-christmas-snowfall-in-picture/</feedburner:origLink></item> <item><title>corMVC: An jQuery-based MVC Framework</title><link>http://feedproxy.google.com/~r/javascriptly/~3/dThB108DQ54/</link> <comments>http://javascriptly.com/2009/12/coremvc-an-jquery-based-mvc-framework/#comments</comments> <pubDate>Tue, 22 Dec 2009 10:16:28 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[Solution]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[MVC]]></category><guid isPermaLink="false">http://javascriptly.com/?p=58</guid> <description><![CDATA[<p style="text-align: justify;">I know quite a few JavaScript MVC frameworks out there but <strong><a href="http://www.bennadel.com/resources/projects/cormvc/demo/index.htm#/" target="_blank">corMVC</a></strong> is what makes me exited at most for a few reasons.</p><p style="text-align: justify;"><strong>corMVC</strong> stands for &#8220;<strong>c</strong>lient-<strong>o</strong>nly-<strong>r</strong>equired&#8221; <strong>M</strong>odel-<strong>V</strong>iew-<strong>C</strong>ontroller and that means it does not depend on&#8230;</p>]]></description> <content:encoded><![CDATA[<p style="text-align: justify;">I know quite a few JavaScript MVC frameworks out there but <strong><a href="http://www.bennadel.com/resources/projects/cormvc/demo/index.htm#/" target="_blank">corMVC</a></strong> is what makes me exited at most for a few reasons.</p><p style="text-align: justify;"><strong>corMVC</strong> stands for &#8220;<strong>c</strong>lient-<strong>o</strong>nly-<strong>r</strong>equired&#8221; <strong>M</strong>odel-<strong>V</strong>iew-<strong>C</strong>ontroller and that means it does not depend on specific server-side technology. In case you want to demo something, it would be perfect if everything can be done on client side. Of course, you can save changes or load data from server (via Model) as the general illustration below.</p><div id="attachment_59" class="wp-caption alignnone" style="width: 510px"><a href="http://javascriptly.com/wp-content/uploads/2009/12/cormvc-overview.png" rel="lightbox"><img src="http://javascriptly.com/wp-content/uploads/2009/12/cormvc-overview-500x238.png" alt="corMVC Overview" title="cormvc-overview" width="500" height="238" class="size-medium wp-image-59" /></a><p class="wp-caption-text">corMVC Overview</p></div><p style="text-align: justify;">Not like other JavaScript MVC solutions, <strong>corMVC</strong> is very simple and has very small footprint. It also does not require you to build the application using scaffolding or any other command-line utilities.</p><p><span id="more-58"></span></p><p style="text-align: justify;">The whole framework is actually a 19KB JavaScript file (not minified yet) named <em>Application.js<strong>,</strong></em> you&#8217;ll have to define models, views and controllers by yourself following the guidelines. It&#8217;s not hard especially for those who are familiar with modern MVC frameworks like Rails, Django or CakePHP.</p><p style="text-align: justify;">Running application built on <strong>corMVC</strong>, you&#8217;ll have almost the same impression of running a server-side MVC application for the URIs (after the hash, however) are changed based on actions. The following diagram is about how the framework detects URL changes and handles events accordingly.</p><p><a href="http://javascriptly.com/wp-content/uploads/2009/12/mvc-workflow.png" rel="lightbox"><img src="http://javascriptly.com/wp-content/uploads/2009/12/mvc-workflow-500x462.png" alt="" title="mvc-workflow" width="500" height="462" class="alignnone size-medium wp-image-60" /></a></p><p style="text-align: justify;">To get an overview of how the framework works and the interaction of model-view-controller we are going to examine <a href="http://www.bennadel.com/resources/presentations/jquery2/demo/#/" target="_blank">the demo application</a> called &#8220;<strong>Contacts</strong>&#8221; provided by <a href="http://www.bennadel.com/" target="_blank">Ben Nadel</a> &#8212; the author of <strong>corMVC</strong>. The app, as its name tells, is a contact manager that has all basic functionalities like Search, View, Add and Delete contact info on a single web page.</p><div id="attachment_61" class="wp-caption alignnone" style="width: 510px"><a href="http://www.bennadel.com/resources/presentations/jquery2/demo/#/contacts" target="_blank"><img src="http://javascriptly.com/wp-content/uploads/2009/12/contacts-app-ui-500x290.png" alt="MVC Contacts application" title="contacts-app-ui" width="500" height="290" class="size-medium wp-image-61" /></a><p class="wp-caption-text">MVC Contacts application</p></div><p style="text-align: justify;">Let&#8217;s look at definition of a &#8220;contact&#8221; model in &#8220;<em>contact.js</em>&#8221; file.</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Add model to the application.</span>
window.<span style="color: #660066;">application</span>.<span style="color: #660066;">addModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> $<span style="color: #339933;">,</span> application <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// I am the contact class.</span>
	<span style="color: #003366; font-weight: bold;">function</span> Contact<span style="color: #009900;">&#40;</span> id<span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">,</span> phone<span style="color: #339933;">,</span> email <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>id <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: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">phone</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>phone <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">email</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>email <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// I validate the contact instance.</span>
	Contact.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">validate</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: #006600; font-style: italic;">// For now, we are just goint to validate TRUE on the client and let</span>
		<span style="color: #006600; font-style: italic;">// the full validation happen on the model peristence.</span>
		<span style="color: #000066; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">// ----------------------------------------------------------------------- //</span>
	<span style="color: #006600; font-style: italic;">// ----------------------------------------------------------------------- //</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Return a new model class.</span>
	<span style="color: #000066; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span> Contact <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> jQuery<span style="color: #339933;">,</span> window.<span style="color: #660066;">application</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p style="text-align: justify;">The code define a contact class with a few properties like ID, Name, Phone# and E-mail address and return the class as a model that will be used by Controllers/Views.</p><p style="text-align: justify;">The controller is more interesting and it&#8217;s not too difficult to understand though.</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Add a controller to the application.</span>
window.<span style="color: #660066;">application</span>.<span style="color: #660066;">addController</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> $<span style="color: #339933;">,</span> application <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// I am the controller class.</span>
	<span style="color: #003366; font-weight: bold;">function</span> Controller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Route URL events to the controller's event handlers.</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">route</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">index</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">route</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;/contacts/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">index</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">route</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;/contacts/add/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addContact</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">route</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;/contacts/edit/:id&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">editContact</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">route</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;/contacts/delete/:id&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">deleteContact</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Set default properties.</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentView</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contactListView</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contactFormView</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Extend the core application controller (required).</span>
	Controller.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> application.<span style="color: #660066;">Controller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">// I initialize the controller. I get called once the application starts</span>
	<span style="color: #006600; font-style: italic;">// running. At that point, the DOM is available and all the other model</span>
	<span style="color: #006600; font-style: italic;">// and view classes will have been added to the system.</span>
	Controller.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">init</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; font-weight: bold;">this</span>.<span style="color: #660066;">contactListView</span> <span style="color: #339933;">=</span> application.<span style="color: #660066;">getView</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;ContactList&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contactFormView</span> <span style="color: #339933;">=</span> application.<span style="color: #660066;">getView</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;ContactForm&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">// I am the add event for this controller.</span>
	Controller.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">addContact</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Show the form view.</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">showView</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contactFormView</span><span style="color: #339933;">,</span> event <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">// I am the edit event for this controller.</span>
	Controller.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">editContact</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event<span style="color: #339933;">,</span> id <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Show the form view.</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">showView</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contactFormView</span><span style="color: #339933;">,</span> event <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">// I am the delete event for this controller.</span>
	Controller.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">deleteContact</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event<span style="color: #339933;">,</span> id <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Delete the contact.</span>
		application.<span style="color: #660066;">getModel</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;ContactService&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">deleteContact</span><span style="color: #009900;">&#40;</span>
			id<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>
				application.<span style="color: #660066;">relocateTo</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;contacts&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">// I am the default event for this controller.</span>
	Controller.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">index</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Show the list view.</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">showView</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contactListView</span><span style="color: #339933;">,</span> event <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">// I show the given view; but first, I hide any existing view.</span>
	Controller.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">showView</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> view<span style="color: #339933;">,</span> event <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Check to see if there is a current view. If so, then hide it.</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentView</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentView</span>.<span style="color: #660066;">hideView</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentView</span>.<span style="color: #660066;">hideView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Show the given view.</span>
		view.<span style="color: #660066;">showView</span><span style="color: #009900;">&#40;</span> event.<span style="color: #660066;">parameters</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Store the given view as the current view.</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentView</span> <span style="color: #339933;">=</span> view<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">// ----------------------------------------------------------------------- //</span>
	<span style="color: #006600; font-style: italic;">// ----------------------------------------------------------------------- //</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Return a new contoller instance.</span>
	<span style="color: #000066; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> Controller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> jQuery<span style="color: #339933;">,</span> window.<span style="color: #660066;">application</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p style="text-align: justify;">Basically, the controller defines routes for events associated with handlers like <em>addContact</em>, <em>editContact</em>, <em>deleteContact</em>. In most of cases, the handlers are to show appropriated views or to send events to model for updates.</p><p style="text-align: justify;">There are a number of views required by the app such as &#8220;<em>Contact List</em>&#8220;, &#8220;<em>Contact Form</em>&#8221; and a simple view for progress notification. Here are excerpt codes from &#8220;<em>Contact List</em>&#8221; view that mostly to interact with user and send events to controller if necessary.</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #006600; font-style: italic;">// Add view to the application.</span>
window.<span style="color: #660066;">application</span>.<span style="color: #660066;">addView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> $<span style="color: #339933;">,</span> application <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// I am the contact list view class.</span>
	<span style="color: #003366; font-weight: bold;">function</span> ContactList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">layout</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">searchForm</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">searchCriteria</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addLink</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contactList</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contactTemplate</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">// I initialize the view. I get called once the application starts</span>
	<span style="color: #006600; font-style: italic;">// running. At that point, the DOM is available and all the other model</span>
	<span style="color: #006600; font-style: italic;">// and view classes will have been added to the system.</span>
	ContactList.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">init</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: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Initialize properties.</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">layout</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;#contact-list-layout&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">searchForm</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;#contact-list-header form&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">searchCriteria</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">searchForm</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;input&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addLink</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;#contact-list-header a&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contactList</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;#contact-list&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contactTemplate</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;#contact-list-item-template&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Bind the search form submit.</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">searchForm</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span>
			<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// Hand of to the search form handler.</span>
				self.<span style="color: #660066;">searchFormHandler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Cancel the default event.</span>
				<span style="color: #000066; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Bind the keypress event on the search criteria.</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">searchCriteria</span>.<span style="color: #660066;">keyup</span><span style="color: #009900;">&#40;</span>
			<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// Filter the contact list.</span>
				self.<span style="color: #660066;">filterList</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Bind the keypress event to the search criteria so we can track </span>
		<span style="color: #006600; font-style: italic;">// the use of the special keys presses.</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">searchCriteria</span>.<span style="color: #660066;">keypress</span><span style="color: #009900;">&#40;</span>
			<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// Store the SHIFT and ALT key status of the current click.</span>
				self.<span style="color: #660066;">searchCriteria</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;shift&quot;</span><span style="color: #339933;">,</span> event.<span style="color: #660066;">shiftKey</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				self.<span style="color: #660066;">searchCriteria</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;alt&quot;</span><span style="color: #339933;">,</span> event.<span style="color: #660066;">altKey</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Bind the list-level clicking (to avoid setting to many </span>
		<span style="color: #006600; font-style: italic;">// event handlers).</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contactList</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span>
			<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> target <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span> event.<span style="color: #660066;">target</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Check to see if the target is the &quot;more&quot; link.</span>
				<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>target.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;a.more&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">// Toggle the list item details.</span>
					self.<span style="color: #660066;">toggleDetails</span><span style="color: #009900;">&#40;</span> target.<span style="color: #660066;">parents</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;li&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">// Blur the current link.</span>
					target.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">// Prevent default event.</span>
					<span style="color: #000066; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Check to see if the target is a the &quot;delete&quot; link.</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>target.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;a.delete&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">// Blur the current link.</span>
					target.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">// Confirm that the user wants to delete the contact.</span>
					<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">confirm</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;Delete this contact?&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
						<span style="color: #006600; font-style: italic;">// This is a *hack* that we have to do since jQuery click() </span>
						<span style="color: #006600; font-style: italic;">// event won't trigger the default action of the link.</span>
						application.<span style="color: #660066;">setLocation</span><span style="color: #009900;">&#40;</span> target.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;href&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #009900;">&#125;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">// Return false to cancel default event.</span>
					<span style="color: #000066; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
&nbsp;
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	....
&nbsp;
	<span style="color: #006600; font-style: italic;">// Return a new view class.</span>
	<span style="color: #000066; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> ContactList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> jQuery<span style="color: #339933;">,</span> window.<span style="color: #660066;">application</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p style="text-align: justify;">Up till now, you may have had the overall knowledge of the framework and how to create a pure JavaScript MVC application. Yet, to get full understanding of <strong>corMVC</strong>, please see this great <a href="http://www.bennadel.com/resources/presentations/jquery2/video/">video presentation</a> in which Ben Nadel explains why he came to the framework, how the JavaScript/jQuery were used, step-by-step application building etc. (this is a multiple-part video and a bit lengthy but I&#8217;m sure it&#8217;ll pay off.)</p><ul><li><strong><a href="http://www.bennadel.com/resources/presentations/jquery2/code.zip">Download Demo Application Code</a></strong></li></ul><div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://javascriptly.com/2008/09/10-alternative-javascript-frameworks/" rel="bookmark">10 Alternative JavaScript Frameworks</a></li><li><a href="http://javascriptly.com/2008/09/chainjs-02-updated-examples/" rel="bookmark">Chain.js v0.2 – Updated Examples</a></li><li><a href="http://javascriptly.com/2008/09/comet-chat-app-meteor-server/" rel="bookmark">Simple Web Chat with Meteor Comet Server</a></li></ul></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/javascriptly?a=dThB108DQ54:3pJ3No0yfDE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dThB108DQ54:3pJ3No0yfDE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=dThB108DQ54:3pJ3No0yfDE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dThB108DQ54:3pJ3No0yfDE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=dThB108DQ54:3pJ3No0yfDE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dThB108DQ54:3pJ3No0yfDE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=dThB108DQ54:3pJ3No0yfDE:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dThB108DQ54:3pJ3No0yfDE:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=dThB108DQ54:3pJ3No0yfDE:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dThB108DQ54:3pJ3No0yfDE:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=dThB108DQ54:3pJ3No0yfDE:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=l6gmwiTKsz0" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2009/12/coremvc-an-jquery-based-mvc-framework/feed/</wfw:commentRss> <slash:comments>5</slash:comments> <feedburner:origLink>http://javascriptly.com/2009/12/coremvc-an-jquery-based-mvc-framework/</feedburner:origLink></item> <item><title>FireQuery – An Introduction</title><link>http://feedproxy.google.com/~r/javascriptly/~3/Ed9efyhzayk/</link> <comments>http://javascriptly.com/2009/12/firequery-an-introduction/#comments</comments> <pubDate>Mon, 21 Dec 2009 06:41:37 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[Firebug]]></category> <category><![CDATA[FireQuery]]></category><guid isPermaLink="false">http://javascriptly.com/?p=51</guid> <description><![CDATA[<h2 style="text-align: justify;">Installation</h2><p style="text-align: justify;">FireQuery is an extension of Firebug created by <a href="http://www.binaryage.com/" target="_blank">BinaryAge</a> to help developers to keep track with jQuery expressions, data and collections as expressed on tool <a href="http://firequery.binaryage.com/" target="_blank">website</a>:</p><blockquote><ul><li> Query expressions</li></ul></blockquote><p>&#8230;</p>]]></description> <content:encoded><![CDATA[<h2 style="text-align: justify;">Installation</h2><p style="text-align: justify;">FireQuery is an extension of Firebug created by <a href="http://www.binaryage.com/" target="_blank">BinaryAge</a> to help developers to keep track with jQuery expressions, data and collections as expressed on tool <a href="http://firequery.binaryage.com/" target="_blank">website</a>:</p><blockquote><ul><li> Query expressions are intelligently presented in Firebug Console and DOM inspector</li><li> attached jQuery data are first class citizens</li><li> elements in jQuery collections are highlighted on hover</li><li> jQuerify: enables you to inject jQuery into any web page</li></ul></blockquote><p style="text-align: justify;">You can install the tool from <a href="https://addons.mozilla.org/en-US/firefox/addon/12632" target="_blank">official Mozilla add-on page</a> (it requires Firebug 1.3+ already existed.) One note: You may have to find <a href="https://addons.mozilla.org/en-US/firefox/addons/versions/12632" target="_blank">an older version</a> (v0.3) to make it work with current official Firebug release (v1.4) as my experience on Windows.</p><h2 style="text-align: justify;">In Action</h2><p style="text-align: justify;">After installing the add-on and restarting Firefox, just go to <a href="http://firequery.binaryage.com/test/index.html" target="_blank">the test page</a> to see how FireQuery tracks embedded jQuery data on FireBug&#8217;s &#8220;<strong>HTML</strong>&#8221; tab. The image below illustrates the data embedded in accordance with the jQuery codes to inject them to the page.</p><p><span id="more-51"></span><br /><div id="attachment_53" class="wp-caption alignnone" style="width: 510px"><a href="http://javascriptly.com/wp-content/uploads/2009/12/firequery-embedded-data.png" rel="lightbox"><img class="size-full wp-image-53" title="firequery-embedded-data" src="http://javascriptly.com/wp-content/uploads/2009/12/firequery-embedded-data.png" alt="FireQuery shows embedded data by jQuery" width="500" height="305" /></a><p class="wp-caption-text">FireQuery shows embedded data by jQuery</p></div></p><p style="text-align: justify;">Now, we&#8217;ll dig into cooler features. One of them is the ability to &#8220;jQuerify&#8221; any web page without jQuery support like <a href="http://www.google.com" target="_blank">Google.com</a> for example. This can be done simply by clicking &#8220;<strong>jQuerify</strong>&#8221; button on FireBug&#8217;s &#8220;<strong>Console</strong>&#8221; tab.</p><div id="attachment_54" class="wp-caption alignnone" style="width: 509px"><a href="http://javascriptly.com/wp-content/uploads/2009/12/firequery-jquerify-google-dot-com.png" rel="lightbox"><img class="size-medium wp-image-54" title="firequery-jquerify-google-dot-com" src="http://javascriptly.com/wp-content/uploads/2009/12/firequery-jquerify-google-dot-com-499x319.png" alt="jQuerify Google.com with FireQuery" width="499" height="319" /></a><p class="wp-caption-text">jQuerify Google.com with FireQuery</p></div><p style="text-align: justify;">Wow, now you can execute any jQuery codes in the Google home page. Normally, there is nothing happen when you click on Google logo but you just want it to say hello instead. Paste the following code snippet to Firebug command window and run it.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#logo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</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, I'm Google!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><div id="attachment_55" class="wp-caption alignnone" style="width: 509px"><a href="http://javascriptly.com/wp-content/uploads/2009/12/firequery-running-jquery-snippet.png" rel="lightbox"><img class="size-medium wp-image-55" title="firequery-running-jquery-snippet" src="http://javascriptly.com/wp-content/uploads/2009/12/firequery-running-jquery-snippet-499x130.png" alt="Running jQuery snippet on Google.com" width="499" height="130" /></a><p class="wp-caption-text">Running jQuery snippet on Google.com</p></div><p style="text-align: justify;">Now, click on Google&#8217;s logo and you&#8217;ll see the alert dialog appears to say hello to you (no surprise) and the FireBug&#8217;s console also shows the jQuery command executed.</p><div id="attachment_57" class="wp-caption alignnone" style="width: 510px"><a href="http://javascriptly.com/wp-content/uploads/2009/12/google-dot-com-saying-hello.png" rel="lightbox"><img class="size-medium wp-image-57" title="google-dot-com-saying-hello" src="http://javascriptly.com/wp-content/uploads/2009/12/google-dot-com-saying-hello-500x292.png" alt="Google.com to say hello" width="500" height="292" /></a><p class="wp-caption-text">Google.com to say hello</p></div><h2>Conclusion</h2><p>FireQuery is a great tool for those who commit to jQuery as main JavaScript library or is going to write a jQuery plug-in. Also, It&#8217;s especially useful for developers, theme designers to see the jQuery&#8217;s data and effects in action without loading the library and writing codes in the templates.</p><div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://javascriptly.com/2010/01/why-do-we-need-google-closure/" rel="bookmark">Why Do We Need Google Closure?</a></li><li><a href="http://javascriptly.com/2008/09/javascript-in-google-chrome/" rel="bookmark">JavaScript in Google Chrome Not So That Fast</a></li><li><a href="http://javascriptly.com/2008/09/javascript-to-detect-google-chrome/" rel="bookmark">JavaScript to Detect Google Chrome</a></li></ul></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/javascriptly?a=Ed9efyhzayk:Dmh2ni7R1-E:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=Ed9efyhzayk:Dmh2ni7R1-E:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=Ed9efyhzayk:Dmh2ni7R1-E:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=Ed9efyhzayk:Dmh2ni7R1-E:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=Ed9efyhzayk:Dmh2ni7R1-E:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=Ed9efyhzayk:Dmh2ni7R1-E:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=Ed9efyhzayk:Dmh2ni7R1-E:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=Ed9efyhzayk:Dmh2ni7R1-E:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=Ed9efyhzayk:Dmh2ni7R1-E:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=Ed9efyhzayk:Dmh2ni7R1-E:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=Ed9efyhzayk:Dmh2ni7R1-E:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=l6gmwiTKsz0" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2009/12/firequery-an-introduction/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://javascriptly.com/2009/12/firequery-an-introduction/</feedburner:origLink></item> <item><title>jQuery Code Content Assistance for Eclipse WTP</title><link>http://feedproxy.google.com/~r/javascriptly/~3/ueb1HbEYJIc/</link> <comments>http://javascriptly.com/2008/10/jquery-eclipse-wtp/#comments</comments> <pubDate>Tue, 14 Oct 2008 03:16:23 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[Eclipse]]></category> <category><![CDATA[WTP]]></category><guid isPermaLink="false">http://javascriptly.com/?p=48</guid> <description><![CDATA[<p style="text-align: justify;">JavaScript code content assistance built in Eclipse WTP does very good job that I found even better than the same functionality in some commercial solutions, however, jQuery&#8217;s syntax is not supported (no surprise). That&#8217;s why jQueryWTP tool comes&#8230;</p>]]></description> <content:encoded><![CDATA[<p style="text-align: justify;">JavaScript code content assistance built in Eclipse WTP does very good job that I found even better than the same functionality in some commercial solutions, however, jQuery&#8217;s syntax is not supported (no surprise). That&#8217;s why jQueryWTP tool comes to help adding jQuery support to Eclipse WTP (and Eclipse PDT, MyEclipse which are based on WTP too.)</p><p style="text-align: justify;">This is not an Eclipse plugin instead a tool to patch the existing plugin and inject jQuery&#8217;s syntax support into it. First <a href="http://sourceforge.net/project/showfiles.php?group_id=202840&amp;package_id=241633&amp;release_id=631407">download the tool from SourceForce</a>; it&#8217;s a Java executable JAR so you can double-click to it or run it from command line:</p><div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">java <span style="color: #660033;">-jar</span> jqueryWTP0.2forJQuery1.2.6.jar</pre></div></div><p style="text-align: justify;">Now browse to plugin file</p><p style="text-align: justify;"><code>org.eclipse.wst.javascript.ui_xxxxxxx.jar</code></p><p style="text-align: justify;">and set output directory to generate the patch. Please backup the original file and set output directory different from source one.</p><p>&nbsp;</p><p style="text-align: justify;"><img class="alignnone size-full wp-image-49" title="jquery-eclipse-wtp-ui" src="http://javascriptly.com/wp-content/uploads/2008/10/jquery-eclipse-wtp-ui.gif" alt="jquery-eclipse-wtp-ui.gif" width="492" height="138" /></p><p>&nbsp;</p><p style="text-align: justify;">Select &#8220;<strong>Generate</strong>&#8221; button to get the patched file then copy over original file. Start Eclipse and open a HTML or script file to see jQuery&#8217;s functions listed on code assistance like image below.</p><p><span id="more-48"></span></p><p>&nbsp;</p><p style="text-align: justify;"><img class="alignnone size-full wp-image-50" title="jquery-eclipse-wtp-code-assistance" src="http://javascriptly.com/wp-content/uploads/2008/10/jquery-eclipse-wtp-code-assistance.gif" alt="wtp-code-assistance.gif" width="500" height="429" /></p><p>&nbsp;</p><p style="text-align: justify;">Find more info about jQueryWTP and donate for project <a href="http://www.langtags.com/jquerywtp/index.html" target="_blank">here</a>.</p><div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://javascriptly.com/2010/01/why-do-we-need-google-closure/" rel="bookmark">Why Do We Need Google Closure?</a></li><li><a href="http://javascriptly.com/2009/12/best-ie6-png-fixes/" rel="bookmark">Best IE6 “PNG Alpha-Transparency” Fixes</a></li><li><a href="http://javascriptly.com/2009/12/firequery-an-introduction/" rel="bookmark">FireQuery - An Introduction</a></li></ul></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/javascriptly?a=ueb1HbEYJIc:NO0YRJ2ZHEQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=ueb1HbEYJIc:NO0YRJ2ZHEQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=ueb1HbEYJIc:NO0YRJ2ZHEQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=ueb1HbEYJIc:NO0YRJ2ZHEQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=ueb1HbEYJIc:NO0YRJ2ZHEQ:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=ueb1HbEYJIc:NO0YRJ2ZHEQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=ueb1HbEYJIc:NO0YRJ2ZHEQ:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=ueb1HbEYJIc:NO0YRJ2ZHEQ:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=ueb1HbEYJIc:NO0YRJ2ZHEQ:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=ueb1HbEYJIc:NO0YRJ2ZHEQ:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=ueb1HbEYJIc:NO0YRJ2ZHEQ:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=l6gmwiTKsz0" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2008/10/jquery-eclipse-wtp/feed/</wfw:commentRss> <slash:comments>13</slash:comments> <feedburner:origLink>http://javascriptly.com/2008/10/jquery-eclipse-wtp/</feedburner:origLink></item> <item><title>Simple Web Chat with Meteor Comet Server</title><link>http://feedproxy.google.com/~r/javascriptly/~3/_1RmeRM3dMM/</link> <comments>http://javascriptly.com/2008/09/comet-chat-app-meteor-server/#comments</comments> <pubDate>Fri, 19 Sep 2008 11:29:15 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[Solution]]></category> <category><![CDATA[Ajax]]></category> <category><![CDATA[Comet]]></category> <category><![CDATA[Meteor]]></category><guid isPermaLink="false">http://javascriptly.com/?p=43</guid> <description><![CDATA[<p><a href="http://meteorserver.org/"><img src="http://javascriptly.com/wp-content/uploads/2008/09/meteor-logo.png" alt="" title="meteor-logo" width="342" height="207" class="alignnone size-full wp-image-45" /></a></p><p style="text-align: justify;"><a href="http://alex.dojotoolkit.org/?p=545">Comet</a> is not really brand-new (Ajax) term today; however, with most of people how it works remains somewhat mysterious.</p><p style="text-align: justify;">I have a little hand-on experience with Comet when creating a hobby game project with <a&#8230;</p>]]></description> <content:encoded><![CDATA[<p><a href="http://meteorserver.org/"><img src="http://javascriptly.com/wp-content/uploads/2008/09/meteor-logo.png" alt="" title="meteor-logo" width="342" height="207" class="alignnone size-full wp-image-45" /></a></p><p style="text-align: justify;"><a href="http://alex.dojotoolkit.org/?p=545">Comet</a> is not really brand-new (Ajax) term today; however, with most of people how it works remains somewhat mysterious.</p><p style="text-align: justify;">I have a little hand-on experience with Comet when creating a hobby game project with <a href="http://directwebremoting.org/dwr/reverse-ajax">DWR Reverse Ajax</a> sometime ago. It (DWR) was simple to start and really worked but required a Java web server (Tomcat, Jetty&#8230;) that I found rather expensive (in term of resources) for such small application. I want a small, dedicated and reliable server for Comet apps while don&#8217;t like to be deeply sunk in technical terms like &#8220;Bayeux&#8221; or &#8220;Continuation&#8221;.</p><p style="text-align: justify;">After reviewing the <a href="http://cometdaily.com/maturity.html">Comet Maturity Guide</a> from Comet Daily I decided to give <a href="http://meteorserver.org/">Meteor Comet server</a> a try for several reasons, especially because it&#8217;s built on Perl that can be easily deployed in any server with Perl installed (i.e. almost all Linux servers, no?) and it should be lightweight (up to my experience with Perl). Here is <a href="http://cometdaily.com/2008/03/14/comet-gazing-maturity/">other info about Meteor server</a>:</p><blockquote><ol type="1"><li>Server daemon will run on any      platform for which Perl is available.</li><li style="text-align: justify;">In live use typically 1,000      clients per node receiving 2 msgs (~400 bytes) per sec each. Tested up to      5,000 clients per node receiving 1 msg/sec each.</li><li style="text-align: justify;">Nodes exist independently,      supports broadcasting for message distribution. Cluster of three Meteor      nodes runs <a href="http://cometdaily.com/2007/11/07/developing-markets-live-for-the-ft/">FT      Alphaville</a>.</li><li>Transports are completely      configurable within simple constraints.</li><li>Server supports client      ‘catch-up&#8217;, allowing clients to regulate quality of service themselves.</li><li>Stable, in production use.</li><li>GPL v2. Free.</li></ol></blockquote><h2>Install &amp; Setup Meteor Server</h2><p style="text-align: justify;">You can find very good how-to guide for installing Meteor server from <a href="http://meteorserver.org/installation/">Meteorserver.org</a>; actually it&#8217;s quite simple to follow. Just download, extract to default location (/usr/local/meteor) and configure daemon service as guided. One notice is on Fedora/Cent OS the start function in init scripts must be changed from:</p><p><span id="more-43"></span></p><div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> –u root .<span style="color: #000000; font-weight: bold;">/</span>meteord <span style="color: #000000; font-weight: bold;">&gt;/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>meteord <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&amp;</span></pre></div></div><p>to:</p><div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>meteord <span style="color: #000000; font-weight: bold;">&gt;/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>meteord <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&amp;</span></pre></div></div><p style="text-align: justify;">After installed and run, the server will listen on 2 ports: 4670 and 4671 as default &#8211; the first port is &#8220;Subscriber port&#8221; and the second is &#8220;Controller port&#8221;. However, you typically want to serve your website on port 80 to avoid cross-site scripting restrictions, therefore, Meteor must also serve on the same port. Yes, the installation guide points out three possible ways to do that:</p><blockquote><p style="text-align: justify;">1. Set SubscriberPort to 80 and SubscriberIP to a specific      IP, say X.X.X.1, in the Meteor config file. This will bind Meteor to a      particular IP address on port 80 and leave Apache to bind to port 80 on a      different IP. To make this work you will need to run Meteor as root, and      configure Apache (or whatever web-server you&#8217;re using) to bind to port 80      on a <em>different IP</em>, say X.X.X.2.</p><p style="text-align: justify;">2. Configure a firewall to redirect traffic on      port 80 of one specific IP, to the Meteor Subscriber port (typically      4670). Then both Meteor and Apache can run in their default      configurations, and Meteor can be run as a non-privileged user. So, if      you&#8217;re using iptables you need the following commands:</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">iptables <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-I</span> PREROUTING <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">--destination</span> X.X.X.1 <span style="color: #660033;">-j</span> REDIRECT <span style="color: #660033;">--to-port</span> <span style="color: #000000;">4670</span>
iptables <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">4670</span> <span style="color: #660033;">-j</span> ACCEPT
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>iptables save</pre></td></tr></table></div><p>or if you&#8217;ve got a hardware firewall, set up an equivilent set of rules on that.</p><p style="text-align: justify;">3. Run Meteor on a different physical server to      the one running Apache. Then each server need only have one IP, and Apache      can be run in its default configuration. Meteor must be set to use port 80      and be run as root.</p></blockquote><p style="text-align: justify;">Well, in brief, you&#8217;ll need either another dedicated (virtual) server or to obtain another IP. But if you don&#8217;t have another server and don&#8217;t want to buy extra IP just for testing, I can tell you <strong>the forth way</strong> to get Meteor server work in the same machine and the same IP. The answer is to configure &#8220;Reverse Proxy&#8221; to pass all requests from specific domain to Meteor. Fortunately, it&#8217;s very simple to do that with Apache&#8217;s mod proxy. Here are what I did.</p><p style="text-align: justify;"> 1. Make  sure Apache to load &#8220;mod proxy&#8221; by commenting out the following line in <code>httpd.conf</code> file.</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">LoadModule proxy_module modules<span style="color: #000000; font-weight: bold;">/</span>mod_proxy.so
LoadModule proxy_connect_module modules<span style="color: #000000; font-weight: bold;">/</span>mod_proxy_connect.so
LoadModule proxy_http_module modules<span style="color: #000000; font-weight: bold;">/</span>mod_proxy_http.so</pre></td></tr></table></div><p style="text-align: justify;"> 2. Add      &#8220;reverse proxy&#8221; for the sub-domain (like data.yourdomain.com) that points to Meteor</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="bash" style="font-family:monospace;">NameVirtualHost <span style="color: #000000; font-weight: bold;">*</span>:<span style="color: #000000;">80</span>
<span style="color: #666666; font-style: italic;"># other virtual hosts</span>
<span style="color: #666666; font-style: italic;">#...</span>
<span style="color: #000000; font-weight: bold;">&lt;</span>VirtualHost <span style="color: #000000; font-weight: bold;">*</span>:<span style="color: #000000;">80</span><span style="color: #000000; font-weight: bold;">&gt;</span>
    	ServerAdmin admin<span style="color: #000000; font-weight: bold;">@</span>localhost
    	ServerName data.yourdomain.com
&nbsp;
 	    	ProxyPass  <span style="color: #000000; font-weight: bold;">/</span>  http:<span style="color: #000000; font-weight: bold;">//</span>127.0.0.1:<span style="color: #000000;">4670</span><span style="color: #000000; font-weight: bold;">/</span>
    	    	ProxyPassReverse  <span style="color: #000000; font-weight: bold;">/</span>  http:<span style="color: #000000; font-weight: bold;">//</span>127.0.0.1:<span style="color: #000000;">4670</span><span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>VirtualHost<span style="color: #000000; font-weight: bold;">&gt;</span></pre></td></tr></table></div><p style="text-align: left;">Restart Apache and you&#8217;ll see the magic work!</p><h2>The Chat Application</h2><p style="text-align: justify;">I often start with a Comet solution by building an online chat so that I can test how fast and reliable the solution is across different browsers. Also, it&#8217;s interesting to see messages coming in real-time manner.</p><p style="text-align: left;">Of course, there is no rocket science here; it looks simple like the demo below.</p><div id="attachment_44" class="wp-caption alignnone" style="width: 510px"><a href="http://javascriptly.com/examples/comet/"><img src="http://javascriptly.com/wp-content/uploads/2008/09/comet-chat-meteor.gif" alt="Web Chat on Meteor Comet Server" title="comet-chat-meteor" width="500" height="402" class="size-full wp-image-44" /></a><p class="wp-caption-text">Web Chat on Meteor Comet Server</p></div><p style="text-align: justify;">Essentially, you&#8217;ll have to include Meteor&#8217;s scripts from sub-domain, <code>data.javascriptly.com</code> in this demo. This script file comes from Meteor server we configured as above and enclose all functions to pull messages in (near) real time.</p><div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://data.javascriptly.com/meteor.js&quot;</span> <span style="color: #000066;">charset</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p style="text-align: left;">Then, setup Comet connection on page load:</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Set this to something unique to this client</span>
Meteor.<span style="color: #660066;">hostid</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;1234567&quot;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Our Meteor server is on the data. subdomain</span>
Meteor.<span style="color: #660066;">host</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;data.&quot;</span> <span style="color: #339933;">+</span> location.<span style="color: #660066;">hostname</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Call the addMsg() function when data arrives</span>
Meteor.<span style="color: #660066;">registerEventCallback</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;process&quot;</span><span style="color: #339933;">,</span> addMsg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Join the chat channel and get last 10 events, then stream</span>
Meteor.<span style="color: #660066;">joinChannel</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;chat&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Meteor.<span style="color: #660066;">mode</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'stream'</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Start streaming!</span>
Meteor.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p style="text-align: justify;">Every time a message arrives, the <code>addMsg()</code> function will be executed to add it to chat console, simple enough!</p><p style="text-align: justify;">Now, when you click &#8220;Send&#8221; button the message you&#8217;ve typed in will be sent to Apache/PHP server (not the Meteor server) using an Ajax POST.</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> sendMsg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> msg <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#message'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>msg.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> user <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#user'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>user.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> user <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Guest&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	$.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'chat.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
		user<span style="color: #339933;">:</span> toCharRef<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
		message<span style="color: #339933;">:</span> toCharRef<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#message'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#message'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p style="text-align: justify;">On receipt Apache/PHP will parse the message and write it to Meteor&#8217;s Controller which is listening on port 4671 like this:</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">...</span>
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;chat&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$op</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Open a controller channel to Meteor server</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Connecting to Meteor<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$op</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fsockopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;127.0.0.1&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4671</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Meteor not responding<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">socket_set_blocking</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$op</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Connected<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$haswritten</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$buf</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ADDMESSAGE &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$ch</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &lt;strong&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$user</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$time</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/strong&gt; &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$message</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p style="text-align: justify;">Meteor server, then, delivers added message to all clients subscribing to &#8220;chat&#8221; channel. That&#8217;s all.</p><p style="text-align: justify;">The app works seamlessly on IE6/7/8, Firefox 3, Safari 3.1 while on Opera 9.2 and Google Chrome Meteor uses &#8220;simple forever IFRAME&#8221; technique for streaming which makes hourglass pointer display all the time. Hope Andrew Betts and team will find some workaround to avoid this issue in next version?</p><p style="text-align: justify;">Chat message supports Unicode (the feature many developers ignore) so you can chat in any language you like: German, French, Chinese, Japanese and English of course. Please feel free to leave your comments/questions here.</p><ul><li><strong><a href="http://javascriptly.com/examples/comet/" title="Demo">View solution online</a></strong></li><li><strong><a href="http://javascriptly.com/examples/comet.zip" title="Download">Download source code</a></strong></li></ul><div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://javascriptly.com/2009/12/coremvc-an-jquery-based-mvc-framework/" rel="bookmark">corMVC: An jQuery-based MVC Framework</a></li><li><a href="http://javascriptly.com/2008/09/zend-16-comes-dojo-firephp-integrated/" rel="bookmark">Zend Framework 1.6 Comes with Dojo and FirePHP Integrated</a></li><li><a href="http://javascriptly.com/2008/08/a-better-javascript-template-engine/" rel="bookmark">A Better JavaScript Template Engine</a></li></ul></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/javascriptly?a=_1RmeRM3dMM:iTLmvAJwg3Y:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=_1RmeRM3dMM:iTLmvAJwg3Y:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=_1RmeRM3dMM:iTLmvAJwg3Y:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=_1RmeRM3dMM:iTLmvAJwg3Y:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=_1RmeRM3dMM:iTLmvAJwg3Y:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=_1RmeRM3dMM:iTLmvAJwg3Y:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=_1RmeRM3dMM:iTLmvAJwg3Y:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=_1RmeRM3dMM:iTLmvAJwg3Y:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=_1RmeRM3dMM:iTLmvAJwg3Y:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=_1RmeRM3dMM:iTLmvAJwg3Y:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=_1RmeRM3dMM:iTLmvAJwg3Y:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=l6gmwiTKsz0" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2008/09/comet-chat-app-meteor-server/feed/</wfw:commentRss> <slash:comments>8</slash:comments> <feedburner:origLink>http://javascriptly.com/2008/09/comet-chat-app-meteor-server/</feedburner:origLink></item> <item><title>Quick &amp; Useful jQuery Plugins</title><link>http://feedproxy.google.com/~r/javascriptly/~3/GfmlFDkgN7c/</link> <comments>http://javascriptly.com/2008/09/quick-useful-jquery-plugins/#comments</comments> <pubDate>Tue, 16 Sep 2008 19:55:49 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[Solution]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[plugin]]></category><guid isPermaLink="false">http://javascriptly.com/?p=39</guid> <description><![CDATA[<p style="text-align: justify;">Here are some quick and useful jQuery plugins from Jason Frame&#8217;s &#8220;<a href="http://github.com/jaz303/jquery-grab-bag/tree/master" target="_blank">jQuery Grab Bag</a>&#8220;.</p><h2>Auto-Grow TextArea</h2><p style="text-align: justify;">The technique is borrowed from Facebook that uses an off-screen &#60;div&#62; to calculate the required dimensions of the&#8230;</p>]]></description> <content:encoded><![CDATA[<p style="text-align: justify;">Here are some quick and useful jQuery plugins from Jason Frame&#8217;s &#8220;<a href="http://github.com/jaz303/jquery-grab-bag/tree/master" target="_blank">jQuery Grab Bag</a>&#8220;.</p><h2>Auto-Grow TextArea</h2><p style="text-align: justify;">The technique is borrowed from Facebook that uses an off-screen &lt;div&gt; to calculate the required dimensions of the textarea to reveal all texts inside instead of to display vertical scrollbar. Run the following code line to enable auto-grow behavior to all textareas on page.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'textarea'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autogrow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p><strong>Online Demo</strong></p><p><a href="http://javascriptly.com/examples/jquery-grab-bag/autogrow-textarea.html"><img class="alignnone size-full wp-image-40" title="auto-grow-textarea" src="http://javascriptly.com/wp-content/uploads/2008/09/auto-grow-textarea.gif" alt="" width="342" height="221" /></a></p><h2>Input Hint</h2><p style="text-align: justify;">It&#8217;s not always necessary to attach label to every text field in web form, instead you can use hint to tell user what to type in the fields. This plugin will help keeping away from tedious codes to add hints to input boxes.</p><p style="text-align: left;">First, add <code>hint</code> attribute to input fields.</p><p><span id="more-39"></span></p><div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">hint</span>=<span style="color: #ff0000;">&quot;Your first name&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;firstname&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">hint</span>=<span style="color: #ff0000;">&quot;Your last name&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;lastname&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div><p style="text-align: left;">Then, simply get the functionality work on the fields.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'*[@hint]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">inputHint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p><strong>Online Demo</strong></p><p><a href="http://javascriptly.com/examples/jquery-grab-bag/input-hint.html"><img class="alignnone size-full wp-image-41" title="input-hint-example" src="http://javascriptly.com/wp-content/uploads/2008/09/input-hint-example.gif" alt="" width="424" height="54" /></a></p><h2>Text Resizing from Cookie</h2><p style="text-align: justify;">This is not a jQuery&#8217;s plugin but still depends on jQuery to allow user to select appropriate text size on reading. Of course, some modern browsers feature zoom functionality but not equally good. So, users still want to resize text on fly.</p><p style="text-align: left;">We&#8217;ll need to define some styles with different font sizes for body tag.</p><div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">body<span style="color: #6666ff;">.small</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
body<span style="color: #6666ff;">.medium</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
body<span style="color: #6666ff;">.large</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">14px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div><p style="text-align: left;">And some links for size selection accordantly.</p><div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">'sizer'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">'#'</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">'small'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>small<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">'#'</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">'medium'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>medium<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">'#'</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">'large'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>large<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p style="text-align: left;">Then call <code>cookieResize()</code> function on load. Done.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">cookieResize<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#sizer a'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'medium'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p><strong>Online Demo</strong></p><p><a href="http://javascriptly.com/examples/jquery-grab-bag/cookie-text-size.html"><img class="alignnone size-full wp-image-42" title="cookie-resize" src="http://javascriptly.com/wp-content/uploads/2008/09/cookie-resize.gif" alt="" width="282" height="211" /></a></p><ul><li> <a title="Download source codes" href="http://javascriptly.com/examples/jquery-grab-bag.zip"><strong>Download example codes</strong></a></li></ul><div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://javascriptly.com/2008/08/a-better-javascript-template-engine/" rel="bookmark">A Better JavaScript Template Engine</a></li><li><a href="http://javascriptly.com/2008/09/unobtrusive-draggable-tabbed-navigation/" rel="bookmark">Unobtrusive Draggable Tabbed Navigation</a></li><li><a href="http://javascriptly.com/2010/01/why-do-we-need-google-closure/" rel="bookmark">Why Do We Need Google Closure?</a></li></ul></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/javascriptly?a=GfmlFDkgN7c:G_4SG-HFON8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=GfmlFDkgN7c:G_4SG-HFON8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=GfmlFDkgN7c:G_4SG-HFON8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=GfmlFDkgN7c:G_4SG-HFON8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=GfmlFDkgN7c:G_4SG-HFON8:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=GfmlFDkgN7c:G_4SG-HFON8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=GfmlFDkgN7c:G_4SG-HFON8:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=GfmlFDkgN7c:G_4SG-HFON8:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=GfmlFDkgN7c:G_4SG-HFON8:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=GfmlFDkgN7c:G_4SG-HFON8:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=GfmlFDkgN7c:G_4SG-HFON8:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=l6gmwiTKsz0" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2008/09/quick-useful-jquery-plugins/feed/</wfw:commentRss> <slash:comments>1</slash:comments> <feedburner:origLink>http://javascriptly.com/2008/09/quick-useful-jquery-plugins/</feedburner:origLink></item> <item><title>Unobtrusive Draggable Tabbed Navigation</title><link>http://feedproxy.google.com/~r/javascriptly/~3/OtRactcbxI0/</link> <comments>http://javascriptly.com/2008/09/unobtrusive-draggable-tabbed-navigation/#comments</comments> <pubDate>Tue, 16 Sep 2008 03:52:02 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[Solution]]></category> <category><![CDATA[Chain.js]]></category> <category><![CDATA[Interaction.js]]></category> <category><![CDATA[tabbed navigation]]></category><guid isPermaLink="false">http://javascriptly.com/?p=35</guid> <description><![CDATA[<p style="text-align: justify;">There are many examples on creating tabbed navigation with (or without) help of JavaScript frameworks like Prototype, MooTools or JQuery. However, I find that it&#8217;s much easier to create <strong>draggable</strong> tabbed navigation using <a href="http://github.com/raid-ox/chain.js/wikis">Chain.js</a> and its great&#8230;</p>]]></description> <content:encoded><![CDATA[<p style="text-align: justify;">There are many examples on creating tabbed navigation with (or without) help of JavaScript frameworks like Prototype, MooTools or JQuery. However, I find that it&#8217;s much easier to create <strong>draggable</strong> tabbed navigation using <a href="http://github.com/raid-ox/chain.js/wikis">Chain.js</a> and its great extension: <a href="http://github.com/raid-ox/interaction.js/wikis/">Interaction.js</a>.</p><p style="text-align: justify;">Riziq, creator of Chain.js &amp; Interaction.js, already showed <a href="http://rizqi.namaku.de/2008/08/tab-interface-using-chainjs/">an example</a> on how to utilize the libs to build tab interface in a few JS code lines. However, for its own purpose, the example is not SEO-friendly &#8212; disabling JavaScript in your browser will result in empty content and search engines will see nothing on your page consequently. Now say, you want to create a tabbed navigation for your blog to show/hide &#8220;Latest Posts&#8221;, &#8220;Latest Comments&#8221; etc. and you want the links can be seen no matter if JavaScript is enabled or not in reader&#8217;s browser &#8212; something looks like this:</p><div id="attachment_36" class="wp-caption alignnone" style="width: 441px"><a href="http://javascriptly.com/examples/chain-tab/tab.html"><img src="http://javascriptly.com/wp-content/uploads/2008/09/tabbed-navigation-final.gif" alt="Tabbed navigation for my blog" title="tabbed-navigation-final" width="431" height="285" class="size-full wp-image-36" border="0" /></a><p class="wp-caption-text">Blog's tabbed navigation</p></div><h2>Step 1: HTML &amp; CSS</h2><p style="text-align: justify;">Just create a template in HTML and full links to latest posts, comments and most popular articles:</p><p><span id="more-35"></span></p><div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;wrapper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;tabs&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;tab&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;title&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Tab<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;content&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;latest_posts&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Latest Posts<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>…<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;content&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;latest_comments&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Latest Comments<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>…<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;content&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;most_pop&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Most Popular<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>…<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p style="text-align: left;">And here is the style sheet to get tabbed look:</p><div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#wrapper</span> <span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#wrapper</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">,</span> Helvetica<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0.8em</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">15px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">400px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#wrapper</span> ul <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#wrapper</span> li <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">6px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#wrapper</span> li a<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">9px</span> <span style="color: #933;">15px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#wrapper</span> div <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#wrapper</span> .<span style="color: #000000; font-weight: bold;">content</span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #933;">15px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#wrapper</span> <span style="color: #6666ff;">.content</span> p<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1.6em</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0.5em</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#wrapper</span> <span style="color: #6666ff;">.content</span> a<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#666</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.tab</span> a <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">&quot;off_tab.png&quot;</span><span style="color: #00AA00;">&#41;</span> <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">repeat-x</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#f2f2f2</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.tab</span><span style="color: #6666ff;">.selected</span> a<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">&quot;active_tab.png&quot;</span><span style="color: #00AA00;">&#41;</span> <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">repeat-x</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div><p style="text-align: justify;">At this point, the page looks fairly good and search engines (Yahoo, Google&#8230;) will be surely happy with it.</p><p><img src="http://javascriptly.com/wp-content/uploads/2008/09/tabbed-navigation-noscript.gif" alt="" title="tabbed-navigation-noscript" width="432" height="527" class="alignnone size-full wp-image-37" /></p><h2>Step 2 &#8211; Creating Tabs and Link Them to Contents</h2><p style="text-align: justify;">It&#8217;s very simple to create tabs with Chain.js. The following scripts will do the job (for those who are not familiar with Chain.js, please read my articles <a href="http://javascriptly.com/2008/08/a-better-javascript-template-engine/">here</a> and <a href="http://javascriptly.com/2008/09/chainjs-02-updated-examples/">here</a>).</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tabs'</span><span style="color: #009900;">&#41;</span>
	.<span style="color: #660066;">items</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>
		<span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'latest_posts'</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Latest Posts'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'latest_comments'</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Latest Comments'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'most_pop'</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Most Popular'</span><span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	.<span style="color: #660066;">selectable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>required<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009966; font-style: italic;">/* force the first tab selected */</span>
	.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p style="text-align: justify;">Now, we need to link tabs with related contents in div tags specified by <code>id</code> field. On tab clicked, all other contents should be hidden but linked one; this function is to accomplish that.</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;">function</span> showContent<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// hide all tabs</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// show selected tab</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>By customizing default <code>chain()</code> method, we can inject <code>onclick</code> event to each tab.</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>8
9
10
11
12
13
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		showContent<span style="color: #009900;">&#40;</span>self.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p style="text-align: left;">All codes are as follows:</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tabs'</span><span style="color: #009900;">&#41;</span>
		.<span style="color: #660066;">items</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>
			<span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'latest_posts'</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Latest Posts'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			<span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'latest_comments'</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Latest Comments'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			<span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'most_pop'</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Most Popular'</span><span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		.<span style="color: #660066;">selectable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>required<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009966; font-style: italic;">/* force the first tab selected */</span>
		.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span>	<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				showContent<span style="color: #009900;">&#40;</span>self.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	showContent<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'latest_posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> showContent<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// hide all tabs</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// show selected tab</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><h2>Step 3 &#8211; Enable Drag-n-Drop</h2><p style="text-align: justify;">Of course, in practice, there is almost no need to make tabs draggable for a blog&#8217;s tabbed navigation. But if required, it can be fulfilled by just adding 2 more functions, <code>draggable()</code> and  <code>sortable()</code>, before the <code>chain()</code> method.</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tabs'</span><span style="color: #009900;">&#41;</span>
	.<span style="color: #660066;">items</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>
		<span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'latest_posts'</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Latest Posts'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'latest_comments'</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Latest Comments'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'most_pop'</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Most Popular'</span><span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	.<span style="color: #660066;">selectable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>required<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009966; font-style: italic;">/* force the first tab selected */</span>
	.<span style="color: #660066;">draggable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>axis<span style="color: #339933;">:</span><span style="color: #3366CC;">'x'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>  <span style="color: #009966; font-style: italic;">/* enable drag-n-drop */</span>
	.<span style="color: #660066;">sortable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>align<span style="color: #339933;">:</span><span style="color: #3366CC;">'horizontal'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
	.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span>	<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			showContent<span style="color: #009900;">&#40;</span>self.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p style="text-align: justify;">So now we have a tabbed navigation with advanced functionality implemented in a few code lines which are completely separated from HTML. And like other examples here, you can see demo and download source codes from links below:</p><ul type="disc"><li><strong><a href="http://javascriptly.com/examples/chain-tab/tab.html">View Online Demo</a></strong></li><li><strong><a href="http://javascriptly.com/examples/chain-tab/chain-tab.zip">Download example codes</a></strong></li></ul><div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://javascriptly.com/2008/08/a-better-javascript-template-engine/" rel="bookmark">A Better JavaScript Template Engine</a></li><li><a href="http://javascriptly.com/2008/09/chainjs-02-updated-examples/" rel="bookmark">Chain.js v0.2 – Updated Examples</a></li><li><a href="http://javascriptly.com/2008/09/lightningdom-to-balance-dom-vs-innerhtml/" rel="bookmark">LightningDOM to Balance DOM vs innerHTML</a></li></ul></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/javascriptly?a=OtRactcbxI0:SqkPvv9W5Tc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=OtRactcbxI0:SqkPvv9W5Tc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=OtRactcbxI0:SqkPvv9W5Tc:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=OtRactcbxI0:SqkPvv9W5Tc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=OtRactcbxI0:SqkPvv9W5Tc:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=OtRactcbxI0:SqkPvv9W5Tc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=OtRactcbxI0:SqkPvv9W5Tc:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=OtRactcbxI0:SqkPvv9W5Tc:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/javascriptly?i=OtRactcbxI0:SqkPvv9W5Tc:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=OtRactcbxI0:SqkPvv9W5Tc:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/javascriptly?a=OtRactcbxI0:SqkPvv9W5Tc:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/javascriptly?d=l6gmwiTKsz0" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2008/09/unobtrusive-draggable-tabbed-navigation/feed/</wfw:commentRss> <slash:comments>3</slash:comments> <feedburner:origLink>http://javascriptly.com/2008/09/unobtrusive-draggable-tabbed-navigation/</feedburner:origLink></item> </channel> </rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: basic
Database Caching using disk: basic

Served from: javascriptly.com @ 2012-05-12 07:25:26 -->

