<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" ><channel><title>JavaScriptly &#187; Solution</title> <atom:link href="http://javascriptly.com/category/solution/feed/" rel="self" type="application/rss+xml" /><link>http://javascriptly.com</link> <description>JavaScript Techniques, Tricks and News</description> <lastBuildDate>Thu, 07 Jan 2010 13:47:39 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.1</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Prototype vs Class Inheritance in YUI 3</title><link>http://javascriptly.com/2010/01/prototype-vs-class-inheritance-in-yui-3/</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[As I mentioned in the post YUI 3.0 – Changes From the Root, YUI 3 is a radical evolvement of the framework. One of the most interesting features it provides with is about much better support for OOP. YUI 3 supports two inheritance patterns via object prototype and “class”. Let’s examine the differences and advantages of [...]]]></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>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2010/01/prototype-vs-class-inheritance-in-yui-3/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Best IE6 “PNG Alpha-Transparency” Fixes</title><link>http://javascriptly.com/2009/12/best-ie6-png-fixes/</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[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 [...]]]></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>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2009/12/best-ie6-png-fixes/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>corMVC: An jQuery-based MVC Framework</title><link>http://javascriptly.com/2009/12/coremvc-an-jquery-based-mvc-framework/</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[Solution]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[MVC]]></category><guid isPermaLink="false">http://javascriptly.com/?p=58</guid> <description><![CDATA[I know quite a few JavaScript MVC frameworks out there but corMVC is what makes me exited at most for a few reasons. corMVC stands for &#8220;client-only-required&#8221; Model-View-Controller 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 [...]]]></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>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2009/12/coremvc-an-jquery-based-mvc-framework/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Simple Web Chat with Meteor Comet Server</title><link>http://javascriptly.com/2008/09/comet-chat-app-meteor-server/</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[ Comet is not really brand-new (Ajax) term today; however, with most of people how it works remains somewhat mysterious. I have a little hand-on experience with Comet when creating a hobby game project with DWR Reverse Ajax sometime ago. It (DWR) was simple to start and really worked but required a Java web server (Tomcat, Jetty&#8230;) [...]]]></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>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2008/09/comet-chat-app-meteor-server/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Quick &amp; Useful jQuery Plugins</title><link>http://javascriptly.com/2008/09/quick-useful-jquery-plugins/</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[Here are some quick and useful jQuery plugins from Jason Frame&#8217;s &#8220;jQuery Grab Bag&#8220;. Auto-Grow TextArea The technique is borrowed from Facebook that uses an off-screen &#60;div&#62; 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 [...]]]></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>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2008/09/quick-useful-jquery-plugins/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Unobtrusive Draggable Tabbed Navigation</title><link>http://javascriptly.com/2008/09/unobtrusive-draggable-tabbed-navigation/</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[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 draggable tabbed navigation using Chain.js and its great extension: Interaction.js. Riziq, creator of Chain.js &#38; Interaction.js, already showed an example on how to utilize the libs to [...]]]></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>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2008/09/unobtrusive-draggable-tabbed-navigation/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>LightningDOM to Balance DOM vs innerHTML</title><link>http://javascriptly.com/2008/09/lightningdom-to-balance-dom-vs-innerhtml/</link> <comments>http://javascriptly.com/2008/09/lightningdom-to-balance-dom-vs-innerhtml/#comments</comments> <pubDate>Fri, 12 Sep 2008 11:19:04 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[Solution]]></category> <category><![CDATA[DOM API]]></category> <category><![CDATA[innerHTML]]></category> <category><![CDATA[LighteningDOM]]></category><guid isPermaLink="false">http://javascriptly.com/?p=34</guid> <description><![CDATA[Using DOM API versus innerHTML to create/change content on webpage has been in discussion for years. The pain (slow DOM manipulation speed) left by Internet Explorer causes a big concern over DOM API for serious use in practice because the browser is still holding big slice of market share today (about 80%). We can guess that [...]]]></description> <content:encoded><![CDATA[<p style="text-align: justify;">Using DOM API versus innerHTML to create/change content on webpage has been in discussion for years. The pain (slow DOM manipulation speed) left by Internet Explorer causes a big concern over DOM API for serious use in practice because the browser is still holding big slice of market share today (about 80%).</p><p style="text-align: justify;">We can guess that the browser war (re)started by Firefox and stirred up by Google Chrome lately will result in better IE versions, yet that bright day won&#8217;t come any time soon.</p><p style="text-align: justify;">Recently, I had reviews on <a href="http://beebole.com/pure/">PURE</a> and <a href="http://github.com/raid-ox/chain.js/wikis">Chain.js</a> as client-side template engines. Though both libraries take pure, unobtrusive JavaScript approach for implementation, PURE seems more eager about speed when using innerHTML to create elements while Chain.js depends on DOM API for more flexible and direct manipulation.</p><p style="text-align: justify;">I cannot say what approach is better. PURE may have some advantage on page with huge number of DOM elements to be created/changed but I think most of projects will be happy with Chain.js for its power. It grows a question: any chance that we can achieve both flexibility and speed in these libs? Probably, <a href="http://blog.cornerstonenw.com/2008/09/10/donating-lightningdom/">LightningDOM donated by Peter Rust</a> will be a right answer.</p><p style="text-align: justify;">In the post, Peter places LightningDOM as &#8220;<em>a layer between the raw HTML and your templating/data-mapping code, without the slowness of the DOM.</em>&#8221; So how it works? Just see the example.</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> strMarkup <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'destination'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> dom <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> LightningDOM<span style="color: #009900;">&#40;</span>strMarkup<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dom.<span style="color: #660066;">firstChild</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I Love speed!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'destination'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> dom.<span style="color: #660066;">outerHTML</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;">Not too difficult to understand, right? You give all HTML markups to LightningDOM, do anything you want with familiar getters/setters like you can do with DOM API then assign the result back to real DOM&#8217;s innerHTML.</p><p style="text-align: justify;">Peter tells more about internal implementation to get both speed and lightness in the library.</p><p><span id="more-34"></span></p><blockquote><p>The LightningDOM parses the given HTML using Regexes. I have two different parse algorithms (but both using the same regex). IE is much faster at parsing the whole markup by doing a single regex replace to convert the markup into JSON and then to eval the JSON. FF (and I&#8217;m guessing Chrome &amp; Opera) is much faster at looping through the markup with a global regex.</p><p>All the tags are parsed into a two-dimensional array (well, technically an array of arrays), which represents all the information in the tags. Here&#8217;s a pretty look at the internal data structure representing a chunk of HMTL (note that we use a simpler object model inspired by <a href="http://effbot.org/zone/element-index.htm">ElementTree</a> to representing the inner-text as a &#8220;tail&#8221; of the previous tag, rather than a nested &#8220;Text Node&#8221;).</p></blockquote><p><em>Array of Arrays (_aMarkup):</em></p><p><a href="http://csnw.files.wordpress.com/2008/09/lightningdom.png"><img class="alignnone size-full wp-image-5" title="LightningDOM data architecture" src="http://csnw.files.wordpress.com/2008/09/lightningdom.png?w=500&amp;h=148" alt="" width="500" height="148" /></a></p><p style="text-align: justify;">Of course, the decision to utilize LightningDOM for PURE or Chain.js depends on leaders of projects but it does not prevent you from taking advantage of the lib if you see true value from it. Personally, I think it&#8217;s a good solution though I&#8217;ll have more tests/benchmarks to evaluate its stability across browsers.</p><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/javascript-in-google-chrome/" rel="bookmark">JavaScript in Google Chrome Not So That Fast</a></li><li><a href="http://javascriptly.com/2008/09/chainjs-02-updated-examples/" rel="bookmark">Chain.js v0.2 – Updated Examples</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2008/09/lightningdom-to-balance-dom-vs-innerhtml/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Chain.js v0.2 – Updated Examples</title><link>http://javascriptly.com/2008/09/chainjs-02-updated-examples/</link> <comments>http://javascriptly.com/2008/09/chainjs-02-updated-examples/#comments</comments> <pubDate>Wed, 10 Sep 2008 04:42:37 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[Solution]]></category> <category><![CDATA[Chain.js]]></category><guid isPermaLink="false">http://javascriptly.com/?p=32</guid> <description><![CDATA[In previous post, I had a chance to introduce Chain.js with 3 examples and found a few issues due to both bugs in version 0.1 and my unawareness of implemented features. Now version 0.2 of Chain.js has just come out, I&#8217;d like to revise the examples to show better implementation we can do with the [...]]]></description> <content:encoded><![CDATA[<p style="text-align: justify;">In <a href="http://javascriptly.com/2008/08/a-better-javascript-template-engine/">previous post</a>, I had a chance to introduce <a href="http://github.com/raid-ox/chain.js/wikis" target="_blank">Chain.js</a> with 3 examples and found a few issues due to both bugs in version 0.1 and my unawareness of implemented features. Now version 0.2 of Chain.js has just come out, I&#8217;d like to revise the examples to show better implementation we can do with the library in practice.</p><p style="text-align: justify;">Basically, we don&#8217;t need to call <code>chain()</code> function every time we add or replace items instead just execute it once on DOM ready event, and we can utilize <code>anchor</code> property to define where items to be rendered within table tag.</p><p><strong><a href="http://javascriptly.com/examples/chain/example1-2.html">Example 1</a>:</strong> All codes to get it works are here.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> render<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'json.php'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'lang='</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#langList'</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: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>books<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#table_book'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">items</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'replace'</span><span style="color: #339933;">,</span> books<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>
&nbsp;
$<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;">'#table_book'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		anchor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'tbody'</span> <span style="color: #009966; font-style: italic;">/* defines, where items will be rendered*/</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;">'#langList'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span>render<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 	<span style="color: #006600; font-style: italic;">// handle change event of drop-down list</span>
	setTimeout<span style="color: #009900;">&#40;</span>render<span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 		<span style="color: #006600; font-style: italic;">// call render function on load</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: center;"><a href="http://javascriptly.com/examples/chain/example1-2.html"><img title="chain-example1" src="http://javascriptly.com/wp-content/uploads/2008/08/chain-example1.gif" border="0" alt="" width="500" height="146" /></a></p><p style="text-align: justify;"><strong><a href="http://javascriptly.com/examples/chain/example2-2.html">Example 2</a>:</strong> Almost the same codes as example 1 plus add, remove and sort functions.</p><p><span id="more-32"></span></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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> render<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'json.php'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'lang='</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#langList'</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: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>books<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#table_book'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">items</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'replace'</span><span style="color: #339933;">,</span> books<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>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> addData<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> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#input_title'</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>
		publisher<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#input_publisher'</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>
		price<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#input_price'</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: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#table_book'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">items</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'add'</span><span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> sortBy<span style="color: #009900;">&#40;</span>col<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#table_book'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">items</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sort'</span><span style="color: #339933;">,</span> col<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>toggle<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: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$<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;">'#table_book'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		anchor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'tbody'</span><span style="color: #339933;">,</span> <span style="color: #009966; font-style: italic;">/* defines, where items will be rendered*/</span>
		bind<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: #009966; font-style: italic;">/* bind event */</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dblclick</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: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'remove'</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;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#langList'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span>render<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 	<span style="color: #006600; font-style: italic;">// handle change event of drop-down list</span>
	setTimeout<span style="color: #009900;">&#40;</span>render<span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 		<span style="color: #006600; font-style: italic;">// call render function on load</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: center;"><a href="http://javascriptly.com/examples/chain/example2-2.html"><img title="chain-example2" src="http://javascriptly.com/wp-content/uploads/2008/08/chain-example2.gif" border="0" alt="" width="500" height="169" /></a></p><p style="text-align: justify;"><strong><a href="http://javascriptly.com/examples/chain/example3-2.html">Example 3</a>:</strong> Custom data binding looks much easier now.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> render<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'json.php'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'lang='</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#langList'</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: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>books<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#table_book'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">items</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'replace'</span><span style="color: #339933;">,</span> books<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>
&nbsp;
$<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: #009966; font-style: italic;">/* Custom data binding */</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#table_book'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		anchor <span style="color: #339933;">:</span> <span style="color: #3366CC;">'tbody'</span><span style="color: #339933;">,</span> <span style="color: #009966; font-style: italic;">/* defines, where items will be rendered*/</span>
		<span style="color: #3366CC;">'.price'</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
			style<span style="color: #339933;">:</span> <span style="color: #3366CC;">'text-align: right'</span><span style="color: #339933;">,</span>
			content<span style="color: #339933;">:</span> <span style="color: #3366CC;">'${price.toFixed(2)}'</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #3366CC;">'.order .order-link'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
			style<span style="color: #339933;">:</span> <span style="color: #3366CC;">'color: red;'</span><span style="color: #339933;">,</span>
			href<span style="color: #339933;">:</span> <span style="color: #3366CC;">'{orderLink}'</span><span style="color: #339933;">,</span>
			target<span style="color: #339933;">:</span> <span style="color: #3366CC;">'_blank'</span><span style="color: #339933;">,</span>
			content<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Order Now!'</span>
		<span style="color: #009900;">&#125;</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;">'#langList'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span>render<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 	<span style="color: #006600; font-style: italic;">// handle change event of drop-down list</span>
	setTimeout<span style="color: #009900;">&#40;</span>render<span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 		<span style="color: #006600; font-style: italic;">// call render function on load</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: center;"><a href="http://javascriptly.com/examples/chain/example3-2.html"><img title="chain-example3" src="http://javascriptly.com/wp-content/uploads/2008/08/chain-example3.gif" border="0" alt="" width="500" height="155" /></a></p><p style="text-align: justify;">Thanks Rizqi Ahmad for kind support to complete these examples.</p><p style="text-align: justify;"><strong></strong><strong><a href="http://javascriptly.com/examples/chain/chain-2.zip">Download updated example codes</a></strong></p><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/2008/09/lightningdom-to-balance-dom-vs-innerhtml/" rel="bookmark">LightningDOM to Balance DOM vs innerHTML</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2008/09/chainjs-02-updated-examples/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>JavaScript to Detect Google Chrome</title><link>http://javascriptly.com/2008/09/javascript-to-detect-google-chrome/</link> <comments>http://javascriptly.com/2008/09/javascript-to-detect-google-chrome/#comments</comments> <pubDate>Fri, 05 Sep 2008 07:04:22 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[Solution]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[Google Chrome]]></category><guid isPermaLink="false">http://javascriptly.com/?p=26</guid> <description><![CDATA[The negative side of having a new (and promisingly become popular) browser, no matter how good it can be, is you&#8217;ll have to test your web apps with it among many others. Probably, the first step is to detect the browser from JavaScript code by parsing browser&#8217;s user agent, and here is what of Google Chrome.Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13I [...]]]></description> <content:encoded><![CDATA[<p style="text-align: justify;">The negative side of having a new (and promisingly become popular) browser, no matter how good it can be, is you&#8217;ll have to test your web apps with it among many others.</p><p style="text-align: justify;">Probably, the first step is to detect the browser from JavaScript code by parsing browser&#8217;s user agent, and here is what of Google Chrome.</p><div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)
AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13</pre></div></div><p style="text-align: justify;">I guess old JavaScript codes can mistakenly tell it Safari like when running the code snippet below using JQuery&#8217;s browser utility.</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">browser</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> val<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	 $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;div&gt;&quot;</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; : &lt;span&gt;&quot;</span> <span style="color: #339933;">+</span> val <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/span&gt;&lt;/div&gt;&quot;</span><span style="color: #009900;">&#41;</span>
		.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</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><img class="size-full wp-image-27" title="jquery-browser-detection" src="http://javascriptly.com/wp-content/uploads/2008/09/jquery-browser-detection.gif" alt="" width="279" height="206" /></p><p style="text-align: justify;">It may be no problem until you find something wrong when your apps running on Chrome only. So, here is the code line to detect Chrome generally:</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> is_chrome <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/chrome/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span> navigator.<span style="color: #660066;">userAgent</span>.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</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;">We&#8217;ll have to change the JQuery browser utility to support Chrome detection 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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> userAgent <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">userAgent</span>.<span style="color: #660066;">toLowerCase</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;">// Figure out what browser is being used</span>
jQuery.<span style="color: #660066;">browser</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	version<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>userAgent.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span> <span style="color: #009966; font-style: italic;">/.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
	chrome<span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/chrome/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span> userAgent <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	safari<span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/webkit/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span> userAgent <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!/</span>chrome<span style="color: #339933;">/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span> userAgent <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	opera<span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/opera/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span> userAgent <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	msie<span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/msie/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span> userAgent <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!/</span>opera<span style="color: #339933;">/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span> userAgent <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	mozilla<span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/mozilla/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span> userAgent <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!/</span><span style="color: #009900;">&#40;</span>compatible<span style="color: #339933;">|</span>webkit<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span> userAgent <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p style="text-align: justify;">Now, correct result shows on test screen:</p><p><img class="alignnone size-full wp-image-28" title="jquery-browser-detection2" src="http://javascriptly.com/wp-content/uploads/2008/09/jquery-browser-detection2.gif" alt="" width="280" height="234" /></p><p style="text-align: justify;">Just one notice: Current version of JQuery (1.2.6) is treating Chrome as if it was Safari and basically no serious problem has been found yet. Modifying browser detection can cause the lib render pages/elements incorrectly for it has no knowledge of Chrome&#8217;s rendering engine. To keep compatibility, you can change line 7 back to:</p><div class="wp_syntax"><table><tr><td class="line_numbers"><pre>7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">	safari<span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/webkit/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span> userAgent <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></pre></td></tr></table></div><p><span id="more-26"></span></p><p>But in this case the browser will be detected as both Chrome and Safari &#8212; not nice solution, I accept.</p><p style="text-align: justify;">Anyway, it&#8217;s very likely that JQuery and other JavaScript frameworks will include Chrome to browser list for detection in next versions.</p><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/09/lightningdom-to-balance-dom-vs-innerhtml/" rel="bookmark">LightningDOM to Balance DOM vs innerHTML</a></li><li><a href="http://javascriptly.com/2008/08/turn-on-javascript-jit-in-firefox-31/" rel="bookmark">Turn on JavaScript JIT in Firefox 3.1</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2008/09/javascript-to-detect-google-chrome/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>10 Alternative JavaScript Frameworks</title><link>http://javascriptly.com/2008/09/10-alternative-javascript-frameworks/</link> <comments>http://javascriptly.com/2008/09/10-alternative-javascript-frameworks/#comments</comments> <pubDate>Fri, 05 Sep 2008 04:28:38 +0000</pubDate> <dc:creator>Jimmy Vu</dc:creator> <category><![CDATA[Solution]]></category> <category><![CDATA[framework]]></category><guid isPermaLink="false">http://javascriptly.com/?p=24</guid> <description><![CDATA[In modern web development, some JavaScript frameworks have made their names like Prototype, Mootools, YUI, JQuery. But if you find them not wholly suitable to your next project there are some other good frameworks out there. Jacob Gube has given out a nice introduction of &#8220;10 alternative and capable JavaScript frameworks/libraries to explore&#8221; going over the [...]]]></description> <content:encoded><![CDATA[<p style="text-align: justify;">In modern web development, some JavaScript frameworks have made their names like Prototype, Mootools, YUI, JQuery. But if you find them not wholly suitable to your next project there are some other good frameworks out there.</p><p style="text-align: justify;">Jacob Gube has given out a nice introduction of &#8220;<strong><a href="http://sixrevisions.com/javascript/promising_javascript_frameworks/" target="_blank">10 alternative and capable JavaScript frameworks/libraries to explore</a></strong>&#8221; going over the good of them. Here they are:</p><ol type="1"><li><a href="http://www.sproutcore.com/">SproutCore</a></li><li><a title="Adobe Labs - Spry framework for Ajax" href="http://labs.adobe.com/technologies/spry/">Spry</a></li><li><a href="http://javascriptmvc.com/">JavaScriptMVC</a></li><li><a href="http://qooxdoo.org/">qooxdoo</a></li><li><a href="http://www.midorijs.com/">midori</a></li><li><a title="Archetype JavaScript Framework - Archetype JavaScript framework presentation" href="http://archetypejs.org/">Archetype      JavaScript Framework</a></li><li><a title="June JavaScript Framework" href="http://june-js.com/">June      Framework</a></li><li><a href="http://www.uize.com/">UIZE</a></li><li><a href="http://simplejs.bleebot.com/">SimpleJS</a></li><li><a href="http://js.fleegix.org/">Fleegix.js</a></li></ol><p style="text-align: justify;">Some of them provide comprehensive solutions that allow you to build a (complicated) desktop-like application with ready-made components (<strong>SproutCore</strong>, <strong>Spry</strong>, <strong>qooxdoo</strong>,<strong> UIZE</strong>), other focus on DOM selector and effects with minimal weight and, specifically, framework <strong>JavaScriptMVC</strong> presents the <a href="http://en.wikipedia.org/wiki/Model-view-controller">Model-View-Controller</a> (MVC) architectural pattern to JavaScript.</p><p><span id="more-24"></span></p><div id="attachment_25" class="wp-caption aligncenter" style="width: 510px"><img class="size-full wp-image-25" title="js-framework-find-your-way" src="http://javascriptly.com/wp-content/uploads/2008/09/js-framework-find-your-way.png" alt="Find your own JavaScript framework (archetypejs.org)" width="500" height="387" /><p class="wp-caption-text">Find your own JavaScript framework (archetypejs.org)</p></div><p style="text-align: justify;">Of course, no framework is perfect and it costs some time to get acquaintance with new one. However, you may find one is better than others for specific requirements so it&#8217;s good to have alternatives.</p><p style="text-align: justify;">I&#8217;ll have more reviews on each framework with hand-on experiences and examples from time to time (hopefully).</p><div id="crp_related"><h2>Related Posts:</h2><ul><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/2009/12/coremvc-an-jquery-based-mvc-framework/" rel="bookmark">corMVC: An jQuery-based MVC Framework</a></li><li><a href="http://javascriptly.com/2010/01/prototype-vs-class-inheritance-in-yui-3/" rel="bookmark">Prototype vs Class Inheritance in YUI 3</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://javascriptly.com/2008/09/10-alternative-javascript-frameworks/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- This site's performance optimized by W3 Total Cache. Dramatically improve the speed and reliability of your blog!

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 10/44 queries in 1.683 seconds using disk

Served from: quangvhg.virtual.vps-host.net @ 2010-09-10 03:10:00 -->