<?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>Truly Evil &#187; jQuery</title>
	<atom:link href="http://trulyevil.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://trulyevil.com</link>
	<description>An exploration into frontend development</description>
	<lastBuildDate>Tue, 13 Oct 2009 19:26:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Custom Events in jQuery</title>
		<link>http://trulyevil.com/2009/05/07/custom-events-in-jquery/</link>
		<comments>http://trulyevil.com/2009/05/07/custom-events-in-jquery/#comments</comments>
		<pubDate>Thu, 07 May 2009 21:43:44 +0000</pubDate>
		<dc:creator>jcfant</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[custom events]]></category>
		<category><![CDATA[events]]></category>

		<guid isPermaLink="false">http://trulyevil.com/?p=83</guid>
		<description><![CDATA[
    




Events seem daunting at first, but really jQuery makes them super simple. Events can be used to alter behavior or even have multiple events happen on a single state change.
We all know about events like click, onkeydown, onmousedown, onmouseover, etc. But you can fire custom events as well. First you need [...]]]></description>
			<content:encoded><![CDATA[<div style="margin: 0px 12px 12px; float: right;">
    <script type="text/javascript"><!--
google_ad_client = "pub-3022454604218516";
/* Small Square Image Add */
google_ad_slot = "0968228215";
google_ad_width = 200;
google_ad_height = 200;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<p>Events seem daunting at first, but really jQuery makes them super simple. Events can be used to alter behavior or even have multiple events happen on a single state change.</p>
<p>We all know about events like click, onkeydown, onmousedown, onmouseover, etc. But you can fire custom events as well. First you need to understand how to fire events. You can do this by using the $().trigger() event through jQuery.<br />
<span id="more-83"></span><br />
To do this, we will attach an event for the onchange to a select input. Then when the value changes we will fire a custom event called &#8220;update&#8221;. </p>
<p>First, the select box.</p>
<style type="text/css">
.javascript { padding: 0px; }
.javascript .internal { color: #2970A6; font-weight: bold; }
.javascript .comment { color: #006600; }
.javascript .quote { color: #2970A6; }
.javascript ol { background-color: #73787B; color: #FFFFFF; }
.javascript li { background-color: #DCDCDC; padding: 0px; margin: 0px;  }
.javascript .line { color: #000000; margin-left: 10px; }
.javascript span.black { color: #000; }
</style>

<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="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>select name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;test&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; --- &quot;</span><span style="color: #339933;">&gt;</span> <span style="color: #339933;">---</span> <span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; Test 1&quot;</span><span style="color: #339933;">&gt;</span> Test <span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; Test 2&quot;</span><span style="color: #339933;">&gt;</span> Test <span style="color: #cc66cc;">2</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; Test 3&quot;</span><span style="color: #339933;">&gt;</span> Test <span style="color: #cc66cc;">3</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; Test 4&quot;</span><span style="color: #339933;">&gt;</span> Test <span style="color: #cc66cc;">4</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; Test 5&quot;</span><span style="color: #339933;">&gt;</span> Test <span style="color: #cc66cc;">5</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; Test 6&quot;</span><span style="color: #339933;">&gt;</span> Test <span style="color: #cc66cc;">6</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; Test 7&quot;</span><span style="color: #339933;">&gt;</span> Test <span style="color: #cc66cc;">7</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; Test 8&quot;</span><span style="color: #339933;">&gt;</span> Test <span style="color: #cc66cc;">8</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; Test 9&quot;</span><span style="color: #339933;">&gt;</span> Test <span style="color: #cc66cc;">9</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; Test 10&quot;</span><span style="color: #339933;">&gt;</span> Test <span style="color: #cc66cc;">10</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>select<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'select.test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</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;">'select.test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">trigger</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'update'</span><span style="color: #339933;">,</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: #660066;">val</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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now we need to listen to this event, we can do this by using .bind() to listen to events triggered on a DOM element.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'select.test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'update'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
     console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Thats the basic way of firing and listening to custom events in jQuery. We can attach more listeners to the &#8220;update&#8221; event by attaching them in the same way.</p>
<p>We can also do this in a more OO way. Lets say we have multiple objects/plugins on the page that need to update when the custom event is fired. We can do this by adding the bind code into the object, then when the event is fired we can update our objects.</p>
<p></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;">var</span> object1 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</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> obj <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: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Object 1&quot;</span><span style="color: #339933;">;</span> 
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'select.test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'update'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> obj.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span>data<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: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">update</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Update from %o with %o&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> data<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: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> object2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</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> obj <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: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Object 2&quot;</span><span style="color: #339933;">;</span> 
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'select.test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'update'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> obj.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span>data<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: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">update</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Update from %o with %o&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> data<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: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The console will log the following:<br />
Update from Object name=Object 1 with &#8221; Test 1&#8243;<br />
Update from Object name=Object 2 with &#8221; Test 1&#8243;</p>
<p>Currently we have just been passing the value to our update event, BUT you can actually pass more than just the value. You can pass ANY of javascript built in data types, including functions, objects and arrays. If we update the trigger to pass the jQuery object, here is what it would look like:</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: #009900;">&#40;</span><span style="color: #3366CC;">'select.test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</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;">'select.test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">trigger</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'update'</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And now our console would log something like the following:<br />
Update from Object name=Object 1 with &lt;select class=&#8221;test&#8221; name=&#8221;test&#8221;&gt;<br />
Update from Object name=Object 2 with &lt;select class=&#8221;test&#8221; name=&#8221;test&#8221;&gt;</p>
<p>Want to see this in action? You can view the demo <a href="http://trulyevil.com/demos/custom-events/">here</a>.</p>
<p>*Update fixed link to the demo</p>
]]></content:encoded>
			<wfw:commentRss>http://trulyevil.com/2009/05/07/custom-events-in-jquery/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Expanding the jQuery Plugin Development Pattern</title>
		<link>http://trulyevil.com/2009/05/01/eexpanding-the-jquery-plugin-development-pattern/</link>
		<comments>http://trulyevil.com/2009/05/01/eexpanding-the-jquery-plugin-development-pattern/#comments</comments>
		<pubDate>Fri, 01 May 2009 20:08:45 +0000</pubDate>
		<dc:creator>jcfant</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://trulyevil.com/?p=10</guid>
		<description><![CDATA[
    

Recently I was tasked with writing a jQuery plugin for a carousel. I am sure at this point you are asking yourself, &#8220;Why would you write another one when there are so many out there?&#8221;. I agree. However, while there are alot of very good carousel plugins, the requirements meant that [...]]]></description>
			<content:encoded><![CDATA[<div style="margin: 0px 12px 12px; float: right;">
    
</div>
<p>Recently I was tasked with writing a jQuery plugin for a carousel. I am sure at this point you are asking yourself, &#8220;Why would you write another one when there are so many out there?&#8221;. I agree. However, while there are alot of very good carousel plugins, the requirements meant that I would end up having to edit the code anywyas. And, since this would be my first shot at writing a jQuery plugin from scratch and I figured it would be a good time to learn the process and to document some best practices/design patterns.<br />
<span id="more-10"></span></p>
<h2>Getting Started:</h2>
<p>Read through  <a href="http://docs.jquery.com/Plugins/Authoring">jQuery Authoring Guidelines</a>, this will get you 90% of the way to writing your first plugin.</p>
<h2>Patterns:</h2>
<p>If you haven&#8217;t seen it yet, you need to checkout a post on <a title="plugin development patttern" href="http://www.learningjquery.com/2007/10/a-plugin-development-pattern" target="_blank">LearningJquery.com by Mike Alsup</a>. He came up with a good base for a design pattern on writing jQuery plugins:</p>
<blockquote><p>There are a few <em>requirements</em> that I feel this pattern handles nicely:</p>
<ol>
<li>Claim only a single name in the jQuery namespace</li>
<li>Accept an options argument to control plugin behavior</li>
<li>Provide public access to default plugin settings</li>
<li>Provide public access to secondary functions (as applicable)</li>
<li>Keep private functions private</li>
<li>Support the Metadata Plugin</li>
</ol>
</blockquote>
<p>While those are a great start, lets add some new ideas to it.</p>
<ol>
<li>Claim only a single name in the jQuery namespace</li>
<li>Accept an options argument to control plugin behavior</li>
<li>Provide public access to default plugin settings</li>
<li>Provide public access to secondary functions (as applicable)</li>
<li>Keep private functions private</li>
<li>Support the Metadata Plugin</li>
<li><strong>Allow for extending the object via callbacks (either hooks or events) as applicable<br />
</strong></li>
<li><strong>scope all selectors with the &#8216;this&#8217; object</strong></li>
</ol>
<h2>Setting Up the Pattern</h2>
<p>For this example, I am going to write a tab plugin. I know, I know&#8230; again there are already so many out there. However, we need an example that will show off the 7-9 in the list, and really its not that hard to write.</p>
<p>Here is a full example that meets the first 6 rules in this design. I am not going to go into detail on each of the first 6, since they are well outlined by <a href="http://www.malsup.com/jquery/">Mike Alsup</a>.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><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>
&nbsp;
    $.<span style="color: #339933;">&lt;</span>span <span style="color: #003366; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;internal fn&quot;</span><span style="color: #339933;">&gt;</span>fn.<span style="color: #660066;">tabs</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>tabOptions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// support mutltiple elements</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;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">1</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;">each</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: #660066;">tabs</span><span style="color: #009900;">&#40;</span>tabOptions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>   
&nbsp;
        <span style="color: #006600; font-style: italic;">// SETUP private variabls;</span>
        <span style="color: #003366; font-weight: bold;">var</span> pOne <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span> 
        <span style="color: #003366; font-weight: bold;">var</span> pTwo <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span> 
        <span style="color: #006600; font-style: italic;">//...</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// SETUP private functions;</span>
        <span style="color: #003366; font-weight: bold;">var</span> findTabs <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;">// do something ... </span>
                            <span style="color: #009900;">&#125;</span>   
        <span style="color: #003366; font-weight: bold;">var</span> hooks <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;">// do something ... </span>
                            <span style="color: #009900;">&#125;</span>   
        <span style="color: #006600; font-style: italic;">// ... </span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// setup options</span>
        <span style="color: #003366; font-weight: bold;">var</span> defaultOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> 
                                    debug <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
                                <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> defaultOptions<span style="color: #339933;">,</span> tabOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">intialize</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;">// support MetaData plugin</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">meta</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> options<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">data</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: #009900;">&#125;</span>   
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changeTab</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;">// change Tab </span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getOptions</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> options<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">intiazlie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>   
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>#7 Extend the plugin via callbacks</h2>
<p>Sounds complicated, but this will allow your plugin to remain flexible and not have to be redesigned each time something new and exciting comes up that you need to accomplish. Taking the code above, lets look at first the changeTab() routine.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changeTab</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;">// change Tab </span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In order to make a call back we need to allow some options to be passed in. We set up the options 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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> 
                    tab0  <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                                onstart <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: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// do something here</span>
                                <span style="color: #000066;">onload</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// do something here</span>
                                onfinished <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: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// do something here</span>
                            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    tab1  <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #006600; font-style: italic;">// lets chain some methods together</span>
                                onstart <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: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// do something here</span>
                                <span style="color: #000066;">onload</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</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;">&#125;</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: #009900;">&#125;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// 2 methods each will be execute</span>
                                onfinished <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</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;">&#125;</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: #009900;">&#125;</span> <span style="color: #009900;">&#93;</span> <span style="color: #006600; font-style: italic;">// again 2 methods</span>
                            <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now, when each of the two tabs loads, we can check and see if there are any callbacks that need to be called in order to extend the functionality. The new changeTab code would look something 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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changeTab</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// find tab clicked</span>
    <span style="color: #003366; font-weight: bold;">var</span> tabIndex <span style="color: #339933;">=</span> findTabIndex<span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    hooks<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'tab'</span> <span style="color: #339933;">+</span> tabIndex<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onstart</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// change Tab ... </span>
    hooks<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'tab'</span> <span style="color: #339933;">+</span> tabIndex<span style="color: #009900;">&#93;</span>.<span style="color: #000066;">onload</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// tab.show();</span>
    hooks<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'tab'</span> <span style="color: #339933;">+</span> tabIndex<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onfinished</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now that we are checking to see if the functionality has been extended. Lets look at how we can actually call this in a generic way?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> hooks <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>thisObject<span style="color: #339933;">,</span> hookList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>span <span style="color: #003366; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;internal typeof&quot;</span><span style="color: #339933;">&gt;</span>typeof hookList <span style="color: #339933;">==</span> <span style="color: #3366CC;">'function'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        hookList.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>thisObject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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><span style="color: #009966; font-style: italic;">/object|array/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>span <span style="color: #003366; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;internal typeof&quot;</span><span style="color: #339933;">&gt;</span>typeof hookList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> hookList.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            hookList<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>thisObject<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;">&#125;</span></pre></td></tr></table></div>

<p>What exactly is this doing? First of all the hooks function takes 2 parameters. thisObject is equal to the object that you want the callback function to have &#8220;this&#8221; set to. and a method to call back on called hookList. The reason we call it list, is that we will check to see if hookList is an array of functions or just a single function. The following call passes in the tabs object and the onstart callbacks.</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;">hooks<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'tab'</span> <span style="color: #339933;">+</span> tabIndex<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onstart</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In the hooks method, we have a little magic happening.</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;">hookList.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>thisObject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We use the .call(object) method on the function. Basically this sets the &#8220;this&#8221; inside of the function to the &#8220;thisObject&#8221; being passed into the function.</p>
<p></p>
<h2>Scope all selectors with the ‘this’ object</h2>
<p>This is actually really simple, but will control the scope of the objects returned by jQuery. Without this, you may clobber another widget on the page or jump outside of the element you are working on. Inside of the findTabs function we need to find all of the tabs and compare it to the current element that was clicked.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> findTabs <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> selected <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.tabs li'</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><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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: #339933;">==</span> element<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                            selected <span style="color: #339933;">=</span> index<span style="color: #339933;">;</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: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</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: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                        <span style="color: #009900;">&#125;</span>   
                                        index<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: #000066; font-weight: bold;">return</span> selected<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>On the surface, there is nothing wrong with this. However, what happens if we have two different tab groups on the page? Nothing? How about a DOM the size of Amazon.com? Would this slow down? Yes, actually it would and you may or may not be acting on a set of tabs that aren&#8217;t your own.</p>
<p>So how do we fix 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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> findTabs <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> selected <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.tabs li'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</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><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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: #339933;">==</span> element<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                            selected <span style="color: #339933;">=</span> index<span style="color: #339933;">;</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: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</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: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                        <span style="color: #009900;">&#125;</span>   
                                        index<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: #000066; font-weight: bold;">return</span> selected<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Wait, that is the same code? Close, but look closer at the $(&#8217;.tabs li&#8217;) selector. Notice, we now use $(&#8217;.tabs li&#8217;, this). (You could also use this.find(&#8217;.tabs li&#8217;); which uses less function lookups however thats a topic for another day). This limits the scope of the search for the tabs. It won&#8217;t clobber any other tabs on the page and you can even nest tabs once you are using this method.</p>
<p>* Update &#8211; Added in new <a href="http://trulyevil.com/demos/jquery-plugin-development-pattern/">Demo page</a>. You can get the full code to play around with.</p>
]]></content:encoded>
			<wfw:commentRss>http://trulyevil.com/2009/05/01/eexpanding-the-jquery-plugin-development-pattern/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>
