<?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>Kamibu &#187; style</title>
	<atom:link href="http://blog.kamibu.com/tag/style/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.kamibu.com</link>
	<description></description>
	<lastBuildDate>Sat, 14 Nov 2009 10:51:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>jQuery Style Rules</title>
		<link>http://blog.kamibu.com/2008/06/12/jquery-style-rules/</link>
		<comments>http://blog.kamibu.com/2008/06/12/jquery-style-rules/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 21:57:13 +0000</pubDate>
		<dc:creator>kostis90gr</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[rules]]></category>
		<category><![CDATA[style]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/?p=38</guid>
		<description><![CDATA[In Excalibur Phoenix a really powerful JavaScript library is used, jQuery. It allows developers to create JavaScript code fast, using only a few lines, and also offers a great variety of features that make coding life easier. jQuery code, though, doesn&#8217;t have many similarities with regular JavaScript DOM code. This post contains a few suggestions [...]]]></description>
			<content:encoded><![CDATA[<p>In Excalibur Phoenix a really powerful JavaScript library is used, <a href="http://jquery.com">jQuery</a>. It allows developers to create JavaScript code fast, using only a few lines, and also offers a great variety of features that make coding life easier. jQuery code, though, doesn&#8217;t have many similarities with regular JavaScript DOM code. This post contains a few suggestions about how Kamibu developers should write jQuery code. Before you start reading this post, please take a quick look on <a href="http://www.dionyziz.com/Style">this<br />
article</a>, since those style rules should be also applied whenever possible.</p>
<p>1) jQuery allows developers to execute more than one jQuery class methods using a technique called method chaining. Method Chaining should be used to the maximum by Kamibu developers.</p>
<p><em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.newcomment&quot;</span> <span style="color: #66cc66;">&#41;</span>.clone<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;
$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.newcomment&quot;</span> <span style="color: #66cc66;">&#41;</span>.css<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;opacity&quot;</span>, <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>.removeClass<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;newcomment&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.newcomment&quot;</span> <span style="color: #66cc66;">&#41;</span>.clone<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>.css<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;opacity&quot;</span>, <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>.removeClass<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;newcomment&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>The first example is not as fast as the second, since two DOM searches are performed.</p>
<p>2) Sometimes, however, chaining makes the code complex and difficult to read, especially when find() is used a lot of times. Therefore, when end() is used, the developer should change a line exactly after it.</p>
<p><em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments&quot;</span> <span style="color: #66cc66;">&#41;</span>.find<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;span.time&quot;</span> <span style="color: #66cc66;">&#41;</span>.text<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;πριν λίγο&quot;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/end"><span style="color: #000066;">end</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.find<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.text&quot;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/empty"><span style="color: #000066;">empty</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.append<span style="color: #66cc66;">&#40;</span> document.createTextNode<span style="color: #66cc66;">&#40;</span> texter <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/end"><span style="color: #000066;">end</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments&quot;</span> <span style="color: #66cc66;">&#41;</span>.find<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;span.time&quot;</span> <span style="color: #66cc66;">&#41;</span>.text<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;πριν λίγο&quot;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/end"><span style="color: #000066;">end</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
.find<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.text&quot;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/empty"><span style="color: #000066;">empty</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.append<span style="color: #66cc66;">&#40;</span> document.createTextNode<span style="color: #66cc66;">&#40;</span> texter <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/end"><span style="color: #000066;">end</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>3) This new JavaScript library provides a lot of ways to select a DOM node, apart from getElementById and getElementsByTagName. You should always use jQuery selectors, before trying to find another way to select the node you want.</p>
<p><em>Do not use this:</em><br />
<pre class="php"><span style="color: #000000; font-weight: bold;">var</span> node = $<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments&quot;</span> <span style="color: #66cc66;">&#41;</span>.get<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>;
node = node.childNodes<span style="color: #66cc66;">&#91;</span> node.childNodes.length<span style="color: #cc66cc;">-1</span> <span style="color: #66cc66;">&#93;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php"><span style="color: #000000; font-weight: bold;">var</span> node = $<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments:last-child&quot;</span> <span style="color: #66cc66;">&#41;</span>.get<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p><em>And do not use this:</em><br />
<pre class="php"><span style="color: #000000; font-weight: bold;">var</span> node = $<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments&quot;</span> <span style="color: #66cc66;">&#41;</span>.eq<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php"><span style="color: #000000; font-weight: bold;">var</span> node = $<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments:first&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>4) Even though selectors could become really long, do not break them into parts, unless you have a good reason to do so.</p>
<p><em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;#dilution div.comments div.text&quot;</span> <span style="color: #66cc66;">&#41;</span>.find<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div div:last-child&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;#dilution div.comments div.text div div:last-child&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>5) When you want to create a node and append it to an element do not use jQuery&#8217;s append method using a string as an argument. Firstly, create the element you want using DOM JavaScript, and then append it to the node you want. This solution should be preffered since it is more object-oriented. Use jQuery&#8217;s append( &#8220;string&#8221; ) only when you would use innerHTML in normal DOM</p>
<p><em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.bottom&quot;</span> <span style="color: #66cc66;">&#41;</span>.append<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;&lt;a href='http://www.zino.gr'&gt;Click Me&lt;/a&gt;&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php"><span style="color: #000000; font-weight: bold;">var</span> a = document.createElement<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'a'</span> <span style="color: #66cc66;">&#41;</span>;
a.href = <span style="color: #ff0000;">&quot;http://www.zino.gr&quot;</span>;
a.appendChild<span style="color: #66cc66;">&#40;</span> document.createTextNode<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'Click Me'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.bottom&quot;</span> <span style="color: #66cc66;">&#41;</span>.append<span style="color: #66cc66;">&#40;</span> a <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>6) Always try to use jQuery&#8217;s characteristics as much as you can, before you try to find another &#8220;manual&#8221; way. A characteristic example is jQuery&#8217;s <a href="http://docs.jquery.com/Attributes/toggleClass">toggleClass</a> method.</p>
<p>7) Always prefer jQuery&#8217;s events except for the following situations.<br />
        7.1 When a DOM element that will be appended is going to be bound to an event.<br />
        7.2 When an argument from backend is going to be used.<br />
                   <em>You should use this:</em><br />
                   <pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
				<span style="color: #0000ff;">$foo</span> = <span style="color: #cc66cc;">5</span>;
			?&gt;&lt;a onclick=<span style="color: #ff0000;">&quot;function() { alert( &lt;?php
			echo $foo;
			?&gt; );return false;&quot;</span> &gt;Test&lt;/a&gt;</pre><br />
         7.3 When an event that was not bounded by jQuery is going to be replaced.<br />
                   <em>Do not use this:</em><br />
                   <pre class="php"><span style="color: #000000; font-weight: bold;">var</span> a = document.createElement<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'a'</span> <span style="color: #66cc66;">&#41;</span>;
		a.onclick = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
			alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'lolek'</span> <span style="color: #66cc66;">&#41;</span>; 
		<span style="color: #66cc66;">&#125;</span>;
		$<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>.click<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'bolek'</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
                   <em>Use this instead:</em><br />
                   <pre class="php"><span style="color: #000000; font-weight: bold;">var</span> a = document.createElement<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'a'</span> <span style="color: #66cc66;">&#41;</span>;
		a.onclick = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
			alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'lolek'</span> <span style="color: #66cc66;">&#41;</span>; 
		<span style="color: #66cc66;">&#125;</span>;
		a.onclick = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'bolek'</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>;</pre></p>
<p>                  <strong>Notice:</strong> Keep in mind that jQuery supports multiple events. The first example, will just add another function to be executed when a is clicked. So two alerts will pop up (lolek,bolek). Moreover, the unbind method cannot be used since the first event was not bounded using jQuery.</p>
<p>8 ) Use fadeIn, fadeOut, fadeTo, show, hide methods, instead of the more general animate method, whenever possible.</p>
<p><em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'chiki'</span> <span style="color: #66cc66;">&#41;</span>.animate<span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> opacity : <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#125;</span>, <span style="color: #cc66cc;">400</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'chiki'</span> <span style="color: #66cc66;">&#41;</span>.fadeOut<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">400</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>9) Prefer jQuery&#8217;s methods instead of the body&#8217;s onload event.</p>
<p><em>Do not use this:</em><br />
<pre class="php">&lt;body onload=<span style="color: #ff0000;">&quot;alert( 'done' );&quot;</span>&gt;&lt;/body&gt;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.ready<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'done'</span> <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>Moreover,<br />
<em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'done'</span> <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.ready<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'done'</span> <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p><strong>Notice:</strong> Use &#8220;jQuery(function($) {&#8221; format only when absolutely necessary</p>
<p>10) Always prefer $( &#8216;something&#8217; ) instead of jQuery( &#8216;something&#8217; ), unless a compatibility issue emerges.</p>
<p>11) Kamibu Specific:<br />
        11.1 Use jQuery&#8217;s effects instead of the objects of animation.js<br />
               <em>Do not use this:</em><br />
               <pre class="php">Animations.Create<span style="color: #66cc66;">&#40;</span> document.getElementById<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'fire'</span> <span style="color: #66cc66;">&#41;</span> , <span style="color: #ff0000;">'opacity'</span> , <span style="color: #cc66cc;">2000</span> , <span style="color: #cc66cc;">1</span> , <span style="color: #cc66cc;">0.3</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
                <em>Use this instead:</em><br />
                <pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'#fire'</span> <span style="color: #66cc66;">&#41;</span>.css<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'opacity'</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>.fadeTo<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">2000</span>, <span style="color: #cc66cc;">0.3</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
        11.2 Prefer Coala instead of jQuery.ajax</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2008/06/12/jquery-style-rules/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

