<?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>Extraordinary Thoughts &#187; plugins</title>
	<atom:link href="http://extraordinarythoughts.com/tag/plugins/feed/" rel="self" type="application/rss+xml" />
	<link>http://extraordinarythoughts.com</link>
	<description>My World, My Blog!</description>
	<lastBuildDate>Tue, 14 Feb 2012 03:18:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Understanding jQuery Plugins</title>
		<link>http://extraordinarythoughts.com/2011/08/20/understanding-jquery-plugins/</link>
		<comments>http://extraordinarythoughts.com/2011/08/20/understanding-jquery-plugins/#comments</comments>
		<pubDate>Sat, 20 Aug 2011 08:45:28 +0000</pubDate>
		<dc:creator>Shawn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://extraordinarythoughts.com/?p=182</guid>
		<description><![CDATA[If you&#8217;re here, I&#8217;m sure it&#8217;s no surprise jQuery is an easy library to use. jQuery may be easy, but it still has its quirks and can be difficult for some to grasp beyond basic functionality and concepts. No worries, &#8230; <a href="http://extraordinarythoughts.com/2011/08/20/understanding-jquery-plugins/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img class="size-medium wp-image-212" style="border: 0px;" title="jQuery Logo" src="http://extraordinarythoughts.com/wp-content/uploads/2011/08/jquery_logo_color_onwhite-300x73.png" alt="jQuery Logo" width="300" height="73" /></p>
<p>If you&#8217;re here, I&#8217;m sure it&#8217;s no surprise jQuery is an easy library to use. jQuery may be easy, but it still has its quirks and can be difficult for some to grasp beyond basic functionality and concepts. No worries, I&#8217;ve got a simple guide here to help break down code, that may seem like overly complex syntax, into simple thoughts and patterns that can be easily understood.</p>
<p>Here, we&#8217;ve got a basic plugin layout:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Shawn Khameneh</span>
<span style="color: #006600; font-style: italic;">// ExtraordinaryThoughts.com</span>
&nbsp;
<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> privateFunction <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;">// code here</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> methods <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		init<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<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: #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: #003366; font-weight: bold;">var</span> $this <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: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> settings <span style="color: #339933;">=</span> $this.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pluginName'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>settings<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
					<span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
						propertyName<span style="color: #339933;">:</span> <span style="color: #3366CC;">'value'</span><span style="color: #339933;">,</span>
						onSomeEvent<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;">&#125;</span>
&nbsp;
					settings <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> defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					$this.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pluginName'</span><span style="color: #339933;">,</span> settings<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>
					settings <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> settings<span style="color: #339933;">,</span> options<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;">// run code here</span>
&nbsp;
			<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>
		destroy<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</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;">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: #003366; font-weight: bold;">var</span> $this <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: #339933;">;</span>
&nbsp;
				$this.<span style="color: #660066;">removeData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pluginName'</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>
		val<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> someValue <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">return</span> someValue<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: #660066;">fn</span>.<span style="color: #660066;">pluginName</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> method <span style="color: #339933;">=</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>methods<span style="color: #009900;">&#91;</span>method<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			method <span style="color: #339933;">=</span> methods<span style="color: #009900;">&#91;</span>method<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			arguments <span style="color: #339933;">=</span> Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</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: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'object'</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>method <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			method <span style="color: #339933;">=</span> methods.<span style="color: #660066;">init</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: #660066;">error</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'Method '</span> <span style="color: #339933;">+</span>  method <span style="color: #339933;">+</span> <span style="color: #3366CC;">' does not exist on jQuery.pluginName'</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: #000066; font-weight: bold;">return</span> method.<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>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<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></div></div>

<p>You may notice the structure I&#8217;ve provided is significantly different from others. Plugins can vary depending on usage and needs. Here, it is my goal to explain the concepts in code well enough for you to understand and author a plugin in such a way that suits you best.</p>
<p>So, let&#8217;s break it down!<br />
<span id="more-182"></span></p>
<h1>The Container: An Immediate Function</h1>
<p>Essentially every plugin is contained in an immediate function like so:</p>

<div class="wp_syntax"><div 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>arg1<span style="color: #339933;">,</span> arg2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #006600; font-style: italic;">// code</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>arg1<span style="color: #339933;">,</span> arg2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>An immediate function is, as its name insinuates, a function. What&#8217;s unique about it is it&#8217;s enclosure in parenthesis, this makes everything inside the function run within a local scope. This does not mean the DOM (global) is protected, but rather all public variables and object namespaces are inaccessible. This makes a perfect start for defining variables and objects to your hearts content without interfering with existing code.</p>
<p>Now since all public variables are inaccessible, that could be a problem seeing jQuery itself is, in fact, a public variable. Just like any function, immediate functions can be passed variables and objects are passed by reference. We can just pass jQuery into our immediate function like so:</p>

<div class="wp_syntax"><div 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>
   <span style="color: #006600; font-style: italic;">// run some jQuery using $ symbol locally</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></div></div>

<p>So what I&#8217;m doing here is passing &#8220;jQuery&#8221; as a public variable into the immediate function where I can reference it locally (inside the container) as &#8220;$&#8221;. In other words I&#8217;m calling the container as a function and passing jQuery as an argument. This also allows for compatibility with Prototype, since the public variable I am referencing is &#8220;jQuery&#8221; rather than it&#8217;s shortcode &#8220;$&#8221;. If you don&#8217;t use Prototype or any other library using &#8220;$&#8221; as a shortcode it isn&#8217;t as relevant but still good to know.</p>
<h1>The Plugin: A Function</h1>
<p>Every jQuery plugin is essentially a large function we shove into the jQuery namespace, while we can easily assign our function using &#8220;jQuery.pluginName = function&#8221;, but if we do so our plugin&#8217;s code isn&#8217;t protected. &#8220;jQuery.fn&#8221; is a shortcode to &#8220;jQuery.prototype&#8221;, meaning it can only be read (and not modified) when using the jQuery namespace to access it. What purpose does this actually serve you? Not much other than writing your code properly and understanding protecting your code from unwanted runtime modification. Best said, it&#8217;s just good practice!</p>
<p>We&#8217;ve got a basic jQuery function via a plugin:</p>

<div class="wp_syntax"><div 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: #660066;">fn</span>.<span style="color: #660066;">pluginName</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// code here</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>
&nbsp;
<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></div></div>

<p>The above code function can be run just like any other jQuery function using &#8220;$(&#8216;#element&#8217;).pluginName()&#8221;. Notice how I include &#8220;return this;&#8221;; this small piece of code allows chainability by returning the original elements (contained in &#8220;this&#8221;) wrapped in a jQuery object. You should also note that &#8220;this&#8221; within this particular scope is a jQuery object, the equivalence of &#8220;$(&#8216;#element&#8217;)&#8221;.</p>
<p>This could be summarized by stating the object returned, in the code above, by &#8220;$(&#8216;#element&#8217;).pluginName()&#8221; is exactly the same as &#8220;$(&#8216;#element&#8217;)&#8221;, and within your function&#8217;s immediate scope there is no need to wrap &#8220;this&#8221; into a jQuery object such as &#8220;$(this)&#8221; as it has already been done.</p>
<h1>Multiple Elements: Understanding Sizzle</h1>
<p>jQuery uses a selector engine called Sizzle, which supports selecting multiple elements (such as entire classes) for use with functions. This is one of the great features of jQuery, but it&#8217;s also something to be considered when developing a plugin. Even if you don&#8217;t anticipate grabbing multiple elements with your plugin, it&#8217;s still good practice to plan for such in the event.</p>
<p>Here I&#8217;ve added in a few pieces of code to account for and run code on multiple elements, individually:</p>

<div class="wp_syntax"><div 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: #006600; font-style: italic;">// Add the plugin to the jQuery (protected, &quot;fn&quot;) namespace under &quot;pluginName&quot; as the function name.</span>
	$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">pluginName</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Return &quot;this&quot; (result of &quot;each&quot; is also &quot;this&quot;) to allow for chaining the plugin</span>
		<span style="color: #000066; font-weight: bold;">return</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>
&nbsp;
			<span style="color: #006600; font-style: italic;">// code here, reference each individual element by &quot;this&quot;</span>
			<span style="color: #006600; font-style: italic;">// e.g. $(this).show();</span>
			<span style="color: #003366; font-weight: bold;">var</span> $this <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: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<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></div></div>

<p>In this example, I&#8217;m not using &#8220;each()&#8221; to run code over each element I have in the selector. Within the function called by &#8220;each()&#8221; the individual element being processed can be referenced in the local scope by &#8220;this&#8221; and used as a jQuery object by &#8220;$(this)&#8221;. It&#8217;s also good practice to store the jQuery object you with to manipulate rather than calling &#8220;$(this)&#8221; each time you wish to run a function. Doing such is, of course, optional and not always needed; I have included it for good measure. Also note that we will be moving the &#8220;each()&#8221; command into our individual functions, this way we can return values when calling methods.</p>
<p>One example of this would be a plugin that supports a &#8220;val&#8221; method for returning a value:</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;">'#element'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'val'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Would return a value instead of a jQuery object</span></pre></div></div>

<h1>Functionality: Public &amp; Private Methods</h1>
<p>A basic function may work for some cases, but more complex plugins will require a various supply of methods and private functions. You may be tempted to use various namespaces to provide various methods for you plugin, but it&#8217;s best not to clutter your sources with excess namespaces.</p>
<p>The code snippet below defines a JSON object to store public methods and demonstrates how you can use your plugin&#8217;s primary function to determine which method needs calling and call that method on each element in the selector.</p>

<div class="wp_syntax"><div 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: #006600; font-style: italic;">// Create a private function by creating a public variable within our plugin's container.</span>
	<span style="color: #003366; font-weight: bold;">var</span> privateFunction <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;">// code here</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Create an object literal for our methods</span>
	<span style="color: #003366; font-weight: bold;">var</span> methods <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Define individual methods within the literal</span>
		init<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>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Repeat over each element in selector</span>
			<span style="color: #006600; font-style: italic;">// Taken from our main function and moved into each method for flexibility</span>
			<span style="color: #000066; font-weight: bold;">return</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: #006600; font-style: italic;">// Create a jQuery object to use with this individual element</span>
				<span style="color: #003366; font-weight: bold;">var</span> $this <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: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// run code here</span>
				<span style="color: #006600; font-style: italic;">// e.g. privateFunction();</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>
		destroy<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;">// Repeat over each element in selector</span>
			<span style="color: #000066; font-weight: bold;">return</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: #006600; font-style: italic;">// run code here</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: #339933;">;</span>
&nbsp;
	$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">pluginName</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;">// Grab our method, sadly if we used function(method){}, it ruins it all</span>
		<span style="color: #003366; font-weight: bold;">var</span> method <span style="color: #339933;">=</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Check if the passed method exists</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>methods<span style="color: #009900;">&#91;</span>method<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// If the method exists, store it for use</span>
			<span style="color: #006600; font-style: italic;">// Note: I am only doing this for repetition when using &quot;each()&quot;, later.</span>
			method <span style="color: #339933;">=</span> methods<span style="color: #009900;">&#91;</span>method<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// If the method is not found, check if the method is an object (JSON Object) or one was not sent.</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: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'object'</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>method <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// If we passed parameters as the first object or no arguments, just use the &quot;init&quot; methods</span>
			method <span style="color: #339933;">=</span> methods.<span style="color: #660066;">init</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>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Not a method and not parameters, so return an error.  Something wasn't called correctly.</span>
			$.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'Method '</span> <span style="color: #339933;">+</span>  method <span style="color: #339933;">+</span> <span style="color: #3366CC;">' does not exist on jQuery.pluginName'</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;">// Call our selected method</span>
		<span style="color: #006600; font-style: italic;">// Once again, note how we move the &quot;each()&quot; from here to the individual methods</span>
		<span style="color: #000066; font-weight: bold;">return</span> method.<span style="color: #660066;">call</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: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<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></div></div>

<p>Notice how I have placed &#8220;privateFunction&#8221; as a global. This is more than acceptable considering all is within the plugin&#8217;s container, therefore it&#8217;s only accessible to the scope of the plugin. In the plugin&#8217;s main function, I am checking if a method to be passed exists. If a method does not exist or the argument passed is an object, the &#8220;init&#8221; method is run. Lastly, if the argument passed is not an object and a method doesn&#8217;t exist, we send out an error.</p>
<p>Also note how each method I am using &#8220;this.each()&#8221;. When I am calling &#8220;method.call(this)&#8221; in the main function, &#8220;this&#8221; is in fact a jQuery object and is being passed into each method as &#8220;this&#8221;. So within our methods&#8217; immediate scope it is already a jQuery object. Only in our function being called by &#8220;each()&#8221; do we need to contain &#8220;this&#8221; in a jQuery object.</p>
<p>Here are some usage examples:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Please note that these cases match the code posted earlier in this section.
Not all plugin layouts will use the same code structure. */</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Call method &quot;init&quot; on each element matching &quot;.className&quot;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'init'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'init'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Pass object &quot;{}&quot; to &quot;init&quot; as arguments</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Pass object &quot;{}&quot; to &quot;init&quot; as arguments</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Call method &quot;destroy&quot; on each element matching &quot;.className&quot;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'destroy'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'destroy'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Pass object &quot;{}&quot; to &quot;destroy&quot; as arguments</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Also works</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'init'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'argument1'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'argument2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Pass &quot;argument 1&quot; and &quot;argument 2&quot; to &quot;init&quot;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Incorrect</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'nonexistantMethod'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'nonexistantMethod'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</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;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'argument 1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Will attempt to call &quot;argument 1&quot; as method</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'argument 1'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'argument 2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Will attempt to call &quot;argument 1&quot; as method</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.className'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluginName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'privateFunction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 'privateFunction' is not a method</span></pre></div></div>

<p>The above examples do show &#8220;{}&#8221;, which would represent arguments. While it will still work with the code in this section, the arguments will not be passed. Proceed to the next section for passing arguments.</p>
<h1>Plugin Settings: Passing Arguments</h1>
<p>Many plugins support passing parameters such as configurations and callbacks. You can pass information either by function arguments or by a JS object using key-value pairs. If your plugin supports more than one or two parameters, it&#8217;s best practice to pass parameters in an object.</p>

<div class="wp_syntax"><div 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>
	<span style="color: #003366; font-weight: bold;">var</span> methods <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		init<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Repeat over each element in selector</span>
			<span style="color: #000066; font-weight: bold;">return</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: #003366; font-weight: bold;">var</span> $this <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: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Create a default settings variable</span>
				<span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
					propertyName<span style="color: #339933;">:</span> <span style="color: #3366CC;">'value'</span><span style="color: #339933;">,</span>
					onSomeEvent<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;">&#125;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Use extend to create settings from passed options and the defaults</span>
				<span style="color: #003366; font-weight: bold;">var</span> settings <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> defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// run code here</span>
&nbsp;
			<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: #339933;">;</span>
&nbsp;
	$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">pluginName</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> method <span style="color: #339933;">=</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>methods<span style="color: #009900;">&#91;</span>method<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			method <span style="color: #339933;">=</span> methods<span style="color: #009900;">&#91;</span>method<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Our method was sent as an argument, remove it using slice because it's not an argument for our method</span>
			arguments <span style="color: #339933;">=</span> Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</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: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'object'</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>method <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			method <span style="color: #339933;">=</span> methods.<span style="color: #660066;">init</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: #660066;">error</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'Method '</span> <span style="color: #339933;">+</span>  method <span style="color: #339933;">+</span> <span style="color: #3366CC;">' does not exist on jQuery.pluginName'</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;">// Use apply to sent arguments when calling our selected method</span>
		<span style="color: #000066; font-weight: bold;">return</span> method.<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>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<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></div></div>

<p>As seen above, an &#8220;options&#8221; argument has been added to the functions and arguments also added to the main function. If a method has been declared, that method is also removed from the arguments before passing them to the method. I have swapped &#8220;call()&#8221; for &#8220;apply()&#8221;; &#8220;apply()&#8221; essentially does the same as &#8220;call()&#8221;, with support for passing arguments. This structure also supports more than one argument, if you wish to use such instead, just modify the arguments for you methods. e.g. &#8220;init: function(arg1, arg2) {}&#8221;</p>
<p>If using a JS object for parameters, you may want to define a defaults variable. Once a defaults variable has been declared, you want to use &#8220;$.extend&#8221; to merge values into a final parameter object (in this case, &#8220;settings&#8221;) for use.</p>
<p>To demonstrate, here are a few examples of logic:</p>

<div class="wp_syntax"><div 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>
	customParameter<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Test 1'</span><span style="color: #339933;">,</span>
	propertyName<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Test 2'</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	propertyName<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Test 3'</span><span style="color: #339933;">,</span>
	onSomeEvent<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Test 4'</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> settings <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> defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">/*
settings == {
	propertyName: 'Test 2',
	onSomeEvent: 'Test 4',
	customParameter: 'Test 1'
}
*/</span></pre></div></div>

<h1>Retaining Settings: Adding Persistant Data</h1>
<p>Sometimes you&#8217;ll want to retain settings and information within your plugin, to do such the jQuery &#8220;data()&#8221; function can be used. It&#8217;s quite simple in practice, make an attempt to fetch data associated with the element and if such data doesn&#8217;t exist, create it and add it to the element. Once you add information using &#8220;data()&#8221;, make sure to keep in mind deallocating resources when no longer needed using &#8220;removeData()&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Shawn Khameneh</span>
<span style="color: #006600; font-style: italic;">// ExtraordinaryThoughts.com</span>
&nbsp;
<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> privateFunction <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;">// code here</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> methods <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		init<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Repeat over each element in selector</span>
			<span style="color: #000066; font-weight: bold;">return</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: #003366; font-weight: bold;">var</span> $this <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: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Attempt to grab saved settings, if they don't exist we'll get &quot;undefined&quot;.</span>
				<span style="color: #003366; font-weight: bold;">var</span> settings <span style="color: #339933;">=</span> $this.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pluginName'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// If we could't grab settings, create them from defaults and passed options</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>settings<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
					<span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
						propertyName<span style="color: #339933;">:</span> <span style="color: #3366CC;">'value'</span><span style="color: #339933;">,</span>
						onSomeEvent<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;">&#125;</span>
&nbsp;
					settings <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> defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">// Save our newly created settings</span>
					$this.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pluginName'</span><span style="color: #339933;">,</span> settings<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: #006600; font-style: italic;">// We got settings, merge our passed options in with them (optional)</span>
					settings <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> settings<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">// If you wish to save options passed each time, add:</span>
					<span style="color: #006600; font-style: italic;">// $this.data('pluginName', settings);</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// run code here</span>
&nbsp;
			<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>
		destroy<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// Repeat over each element in selector</span>
			<span style="color: #000066; font-weight: bold;">return</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;">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: #003366; font-weight: bold;">var</span> $this <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: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// run code here</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Remove settings data when deallocating our plugin</span>
				$this.<span style="color: #660066;">removeData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pluginName'</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>
		val<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// code here, use .eq(0) to grab first element in selector</span>
			<span style="color: #006600; font-style: italic;">// we'll just grab the HTML of that element for our value</span>
			<span style="color: #003366; font-weight: bold;">var</span> someValue <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</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;">// return one value</span>
			<span style="color: #000066; font-weight: bold;">return</span> someValue<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: #660066;">fn</span>.<span style="color: #660066;">pluginName</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> method <span style="color: #339933;">=</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>methods<span style="color: #009900;">&#91;</span>method<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			method <span style="color: #339933;">=</span> methods<span style="color: #009900;">&#91;</span>method<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			arguments <span style="color: #339933;">=</span> Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</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: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'object'</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>method <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			method <span style="color: #339933;">=</span> methods.<span style="color: #660066;">init</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: #660066;">error</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'Method '</span> <span style="color: #339933;">+</span>  method <span style="color: #339933;">+</span> <span style="color: #3366CC;">' does not exist on jQuery.pluginName'</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: #000066; font-weight: bold;">return</span> method.<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>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<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></div></div>

<p>In the code above, I am checking if data for the elements exists. If data for the element doesn&#8217;t exist, settings are created by merging the the passed &#8220;options&#8221; and &#8220;defaults&#8221;, then saved using the &#8220;data()&#8221; command.</p>
<h1>All Done: Continue Reading!</h1>
<div>Here are a couple links for reference:</div>
<ul>
<li><a href="http://docs.jquery.com/Plugins/Authoring">jQuery Documentation &#8211; Plugins/Authoring</a></li>
<li><a href="http://docs.jquery.com/Types#Context.2C_Call_and_Apply">jQuery Documentation &#8211; Context, Call, and Apply</a></li>
<li><a href="http://webcloud.se/log/Building-jQuery-plugins/">Alternative Method Using &#8220;$.fn.extend()&#8221;</a></li>
<li><a href="http://www.learningjquery.com/2007/10/a-plugin-development-pattern">Another Guide Demonstrating Secondary Functions</a></li>
</ul>
<p>Last but not least, I&#8217;d also highly recommend the book <a href="http://oreilly.com/catalog/9780596806767">Javascript Patterns</a>. Some of the best books I&#8217;ve found are code patterns, and this book can help give you a head start to developing cleaner code and plugins.</p>
]]></content:encoded>
			<wfw:commentRss>http://extraordinarythoughts.com/2011/08/20/understanding-jquery-plugins/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>ExtJS Snapping Windows</title>
		<link>http://extraordinarythoughts.com/2011/08/17/extjs-snapping-windows/</link>
		<comments>http://extraordinarythoughts.com/2011/08/17/extjs-snapping-windows/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 17:31:18 +0000</pubDate>
		<dc:creator>Shawn</dc:creator>
				<category><![CDATA[ExtJS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[extensions]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://extraordinarythoughts.com/?p=174</guid>
		<description><![CDATA[There&#8217;s a really useful feature in Windows 7 that allows windows to be resized just by dragging them to edges of the screen.  Well, I wanted that feature for an ExtJS desktop environment, so here&#8217;s my solution for Windows 7 &#8230; <a href="http://extraordinarythoughts.com/2011/08/17/extjs-snapping-windows/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a really useful feature in Windows 7 that allows windows to be resized just by dragging them to edges of the screen.  Well, I wanted that feature for an ExtJS desktop environment, so here&#8217;s my solution for Windows 7 style window dragging.</p>
<p>The following code overrides some functionality in ExtJS and allows windows to be resized in the same manner.</p>
<div>Features:</div>
<ul>
<li>Windows can be dragged when maximized and will resize when dragged.</li>
<li>Dragging a window (cursor within 20 pixels) to top of container will maximize it.</li>
<li>Dragging a window (cursor within 20 pixels) to left or right of container will resize it to that half of the container.</li>
<li>The window ghost will indicate its new size according to placement when dragging.</li>
<li>Windows will restore to their original size before being snapped. (Not working in ExtJS 4 version)</li>
<li>Adds snapLeft() and snapRight() functions to the Ext.Window component.</li>
<li>Unsnapping windows will position them relative to the original cursor position on the title bar. (v2 only)</li>
<li>Supports autoloading (v2 only)</li>
</ul>
<div>Bugs / Quirks:</div>
<div>
<ul>
<li>Windows that initialize maximized will restore to 800&#215;400</li>
<li>Windows that initialize with resizable = false cannot be dragged when maximized. (Only ExtJS 3.3.1 version)</li>
<li>Maximizing a window when snapped to half the screen will make it resize to half of the screen.  (only ExtJS 3.3.1 version when using resize button)</li>
</ul>
<div>Yeah, the quirks can be easily fixed, I just have no immediate need to fix them.</div>
<p><span id="more-174"></span></p>
<p><a href="http://extraordinarythoughts.com/wp-content/uploads/2011/08/SnapWindows.js">Ext.ux.SnapWindows.js v2 for ExtJS 4 (Nov 7, 2001)</a> - <a href="http://extraordinarythoughts.com/code/ext-4.0.7/examples/snapwindows/">Demo</a><br />
<a href="http://extraordinarythoughts.com/wp-content/uploads/2011/08/Ext.ux_.SnapWindows.js">Ext.ux.SnapWindows.js v1 for ExtJS 4 (Oct 6, 2011)</a><br />
<a href="http://extraordinarythoughts.com/wp-content/uploads/2011/08/Ext.ux_.SnapWindows1.js">Ext.ux.SnapWindows.js v1 for ExtJS 3.3.1</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://extraordinarythoughts.com/2011/08/17/extjs-snapping-windows/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

