<?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>Cameron McKay</title>
	<atom:link href="http://cdmckay.org/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming, Politics and Game Design</description>
	<lastBuildDate>Tue, 24 Aug 2010 13:57:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to do an AJAX search with jQTouch, Part 2</title>
		<link>http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/</link>
		<comments>http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 15:53:25 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1667</guid>
		<description><![CDATA[In the last installment of How to do an AJAX search with jQTouch we looked at how to setup a jQTouch interface with the goal of performing an AJAX search. In this article, we will write the necessary JavaScript to perform that AJAX search, as well as a PHP script to respond to those calls.
This [...]]]></description>
			<content:encoded><![CDATA[<p>In the last installment of <a href="http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/">How to do an AJAX search with jQTouch</a> we looked at how to setup a jQTouch interface with the goal of performing an AJAX search. In this article, we will write the necessary JavaScript to perform that AJAX search, as well as a PHP script to respond to those calls.</p>
<p>This article assumes you have read <a href="http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/">the first part of this series</a>.</p>
<p><span id="more-1667"></span></p>
<p>Before we start, we should take a step back and consider what we&#8217;re about to do.  Our Searcher app has three parts:</p>
<ul>
<li>the client-side jQTouch UI,</li>
<li>the client-side jQuery AJAX calls,</li>
<li>the server-side PHP script (that could easily be backed by a database)</li>
</ul>
<p>At this point we have finished the jQTouch UI.  However, instead of immediately moving on to writing the jQuery AJAX calls, let&#8217;s start with the PHP script instead.  This will help us understand our AJAX calls when it comes time to write them.</p>
<h3>The PHP Script</h3>
<p>Since our PHP script will be performing a search, let&#8217;s create a file called <code>search.php</code> and add a single line of boilerplate:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></pre></div></div>

<p>There&#8217;s no need for an end tag.  In fact, in files containing only PHP, <a href="http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html">it is recommended that you do not include an end tag</a>:</p>
<blockquote><p>For files that contain only PHP code, the closing tag (&#8220;?&gt;&#8221;) is never permitted. It is not required by PHP, and omitting it prevents the accidental injection of trailing white space into the response.</p></blockquote>
<p>Our Searcher app needs to search for something, so let&#8217;s pretend it&#8217;s an interface for a music player and we&#8217;re searching for songs. In order to express that idea into PHP code, we need the following elements:</p>
<ul>
<li>a Song class to hold the artist name and the title,</li>
<li>an array holding a bunch of Song objects,</li>
<li>a function for searching the Song array,</li>
<li>a way to get a search value from the AJAX request,</li>
<li>a way to set the MIME type to JSON,</li>
<li>a way to encode our Song array as JSON</li>
</ul>
<p>First, we&#8217;ll need a class to hold the songs (place this starting at line 3 in <code>search.php</code>):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">class</span> Song <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$artist</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$artist</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">artist</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$artist</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s break it down for those not familiar with PHP classes:</p>
<ul>
<li>at line 3, we declare the class name</li>
<li>at lines 4-5, we declare two public fields to represent the artist&#8217;s name and the title of the song,</li>
<li>at lines 7-10, we declare the class constructor that we use to initialize the two fields of a Song object</li>
</ul>
<p>Now that we have a way to model songs in our PHP script, we need to make an array of some dummy songs that we can query using AJAX:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000088;">$songs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Say It Ain't So&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Undone&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cake&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Meanwhile, Rick James...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The Stars&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Ageless Beauty&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Roy Orbison&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;In Dreams&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now we have something of a faux database setup. How will we search it?  Why, with a search function, of course! Let&#8217;s write that now:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">function</span> search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$songs</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$songs</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$matches</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$songs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$matchArtist</span> <span style="color: #339933;">=</span> stripos<span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">artist</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$matchTitle</span> <span style="color: #339933;">=</span> stripos<span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matchArtist</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$matchTitle</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$s</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$matches</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So what does this function do? Let&#8217;s take a look line-by-line:</p>
<ul>
<li>at lines 22-23, we check to see if the search value is empty (i.e. an empty string or null). If that&#8217;s the case, then we just return all the songs</li>
<li>at line 25, we declare the <code>$matches</code> array. This will contain all the songs that match the search value <code>$value</code></li>
<li>at line 26, we use a foreach loop to iterate through all the Song objects in the <code>$songs</code> array</li>
<li>at lines 28-29, we check to see if the artist name or song title contain the search value <code>$value</code></li>
<li>at line 30-31, we add the Song object to the <code>$matches</code> array if it matches the artist name or song title</li>
<li>at line 33, we return whatever matches (if any) we have found</li>
</ul>
<p>Ok, so now we have a Song class, an array of Song objects, and a function that searches that array. What&#8217;s left in our PHP script? Well, as it stands, we have no way to interface with it. What we need to do now is have some sort of way to respond to the AJAX GET request that our Searcher app will send. To do that, we harness the awesome power of the <code>$_GET</code> <a href="http://php.net/manual/en/language.variables.superglobals.php">superglobal</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>36
37
</pre></td><td class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$matches</span> <span style="color: #339933;">=</span> search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$songs</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>On the first line, we extract &#8220;value&#8221; parameter from the <code>$_GET</code> superglobal and assign it to the <code>$value</code> variable.  The second line runs our search function on the <code>$songs</code> database and searches for songs that have an artist or title that contains the <code>$value</code> string.</p>
<p>We&#8217;re almost there!  At this point we&#8217;ve managed to get an array of Song objects that match the passed search value.  All we need to do now is return that PHP array of objects as a JSON-encoded array of objects.  Let&#8217;s do that now:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>39
40
</pre></td><td class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> json_encode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The first line sets the the <a href="http://en.wikipedia.org/wiki/Internet_media_type">MIME type</a> of an HTTP response.  MIME types are a sort of meta data that is used to describe what sort of content is being returned.  In this case, we are telling jQuery (or whatever client that issued the request) that we are returning content that is JSON formatted.  The second line is a built-in PHP function that converts PHP types into a string containing their JSON counterparts.  Basically, it allows us to encode our PHP objects into JavaScript and allow us to access them in a nearly identical way:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #666666; font-style: italic;">// PHP</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Artist 1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Title 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Artist 2&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Title 2&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Becomes... (after json_encode)</span>
<span style="color: #666666; font-style: italic;">// JavaScript</span>
<span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#123;</span> artist<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Artist 1&quot;</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Title 1&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> artist<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Artist 2&quot;</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Title 2&quot;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And we&#8217;re done the PHP script! If we want to test it, we can load execute the PHP file on our server and try different search values.  For example, entering this URL should return a JSON array with two Song objects:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #666666; font-style: italic;">// URL</span>
http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//example.com/jQTouch-ajax-search/search.php?value=Weezer</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Response</span>
<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;artist&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Say It Ain't So&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;artist&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Undone&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span></pre></div></div>

<h3>The jQuery AJAX calls</h3>
<p>The finish line is now in sight.  We&#8217;ve successfully written our jQTouch UI and our PHP script.  The last thing we need to do is write some jQuery AJAX calls to execute our search and load the results into our UI.  In order to do that, we need to do the following:</p>
<ul>
<li>listen for the submission of the search form in the jQTouch UI</li>
<li>when the submission happens, get the search value from the form and set it as a parameter in an AJAX call to <code>search.php</code></li>
<li>register a callback that will populate the jQTouch search results when the AJAX call completes</li>
</ul>
<p>Let&#8217;s start with listening for the submit event on the search form.  First, we&#8217;ll need to create a new JavaScript source file <code>search.js</code> and fill it with the following boilerplate:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, 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;">// Put code here.    </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>What this boilerplate does is give our JavaScript script its own context so that can minimize leaking variables into the global JavaScript namespace.  It also gives us access to the jQuery object in the convenient <code>$</code> form, regardless of whether or not <a href="http://api.jquery.com/jQuery.noConflict/">&#8220;no conflict&#8221; mode is enabled</a>.</p>
<p>Recall in part 1 we defined an HTML search form that looked 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
</pre></td><td class="code"><pre class="html4strict" style="font-family:Consolas, monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;toolbar&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Search<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;back&quot;</span>&gt;</span>Back<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;rounded&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-text&quot;</span> placeholder<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Search&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-text&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;edgetoedge&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-results&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sep&quot;</span>&gt;</span>Results<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>                
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></td></tr></table></div>

<p>Note that on line 1 we defined the id of the form as <code>search</code>. We&#8217;ll use that to find the form and register a handler for its submit event so that we can take action when someone tries to perform a search using the jQTouch UI.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:Consolas, 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: #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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> info<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[id=search-text]&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        text.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> results <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search-results&quot;</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;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        results.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;li&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sep&quot;</span><span style="color: #339933;">,</span>
            text<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Results for &quot;'</span> <span style="color: #339933;">+</span> text.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;search.php&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#123;</span> value<span style="color: #339933;">:</span> text.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            <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>
                $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> song<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> song.<span style="color: #660066;">artist</span> 
                        <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; - &quot;</span>
                        <span style="color: #339933;">+</span> song.<span style="color: #660066;">title</span><span style="color: #339933;">;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;li&gt;&quot;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>       
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&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><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>

<p>There&#8217;s a lot there, so let&#8217;s break it down line-by-line:</p>
<ul>
<li>starting at line 3 and ending at line 32 we have <a href="http://api.jquery.com/ready/">the jQuery ready handler</a> which is run once the DOM has finished loading</li>
<li>starting at line 5 and ending at line 30 we bind a submit event handler to the submit event for the <code>#search</code> form</li>
<li>at lines 6-7, we retrieve the <code>search-text</code> element, assign it to a variable (for later use on line 16), and then blur (i.e. deselect) the element so that the iPhone keyboard will go away</li>
<li>at lines 9-13, we clear any previous items in the list that may be hanging around from a previous search and then load in a new separator (&#8220;sep&#8221;) that contains the search value</li>
<li>at line 15-27, we perform our AJAX GET request to <code>search.php</code> with the value of the search text field as our value parameter (line 16) and a callback that adds the results to the list (lines 17-26)</li>
<li>finally, at line 29, we return false to prevent the default form behaviour (i.e. submitting the form) from being carried out</li>
</ul>
<p>And we&#8217;re done.  All we need to do now and include <code>search.php</code> in the header of <code>index.html</code> so that it&#8217;ll be run:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
</pre></td><td class="code"><pre class="html" style="font-family:Consolas, monospace;">&lt;script src=&quot;search.js&quot; type=&quot;application/x-javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;</pre></td></tr></table></div>

<h3>Conclusion</h3>
<p>In this, the final installment of <em>How to do an AJAX search with jQTouch</em>, we finished off the example application we started in <a href="http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/">the first part of this series</a>.  </p>
<p>In part 1, we covered building a UI using jQTouch.  In part 2, we helped visualize our problem by pretending that we were writing a front-end UI to a server-side music player.  To do that, we jumped into server side programming with PHP to write a script to serve us JSON-encoded Song objects.  Once we had that setup, we moved back into JavaScript.  In order to make use of the PHP script, we wrote a jQuery handler to respond to a jQTouch form submission.  The handler, once triggered, issued an AJAX call to the PHP script to pull down the appropriate Song objects for display in our UI.</p>
<h3>What Now?</h3>
<p>With respect to the goals of this tutorial, nothing.  However, if you want to pimp up the application a bit more, some things you could do are: </p>
<ul>
<li>Add genres and albums to the PHP Song object and then make the jQTouch UI menu have two additional options: Genres and Albums</li>
<li>Try to make a Now Playing sub-menu that is activated when a Song is picked from the Search screen</li>
</ul>
<h3>Source</h3>
<p>The source code I have written in this article is public domain, and you are free to do what you will with it. Here is <a href="http://cdmckay.org/files/jqtouch-ajax-search-part2.zip">a zip of the source, along with the required jQTouch source</a>. It should work pretty much right out of the box.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to use custom jQuery animation queues</title>
		<link>http://cdmckay.org/blog/2010/06/22/how-to-use-custom-jquery-animation-queues/</link>
		<comments>http://cdmckay.org/blog/2010/06/22/how-to-use-custom-jquery-animation-queues/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 05:07:53 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1620</guid>
		<description><![CDATA[You may not know this, but whenever you use jQuery commands like fadeIn, slideDown, and delay, you are implicitly making use of a jQuery queue behind the scenes.  That queue is named fx, and it is the default queue that all animations use unless otherwise specified.
In this article, we will look at how jQuery [...]]]></description>
			<content:encoded><![CDATA[<p>You may not know this, but whenever you use jQuery commands like <code>fadeIn</code>, <code>slideDown</code>, and <code>delay</code>, you are implicitly making use of a jQuery queue behind the scenes.  That queue is named <code>fx</code>, and it is the default queue that all animations use unless otherwise specified.</p>
<p>In this article, we will look at how jQuery animation queues work, how to create and manipulate them, and how to use them in a meaningful way.</p>
<p><span id="more-1620"></span></p>
<h3>Queue fundamentals</h3>
<p>Each element has its own set of queues.  For example, if we execute this code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</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;">&quot;#bar&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The elements <code>#foo</code> and <code>#bar</code> will fade out more or less simultaneously. This is because they have their own <code>fx</code> queues. However, if we execute the code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</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;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The element <code>#foo</code> will fade out and then fade in, instead of getting stuck in some sort of tug of war for opacity. This is because they are both using the <code>fx</code> queue of <code>#foo</code>, and thus run one after another.</p>
<h3>Creating and manipulating queues</h3>
<p>jQuery provides 5 commands for interacting with queues, which are summarized in the table below.</p>
<table>
<tr>
<th>Command</th>
<th>Summary</th>
</tr>
<tr>
<td>animate</td>
<td>Perform a custom animation of a set of CSS properties.</td>
</tr>
<tr>
<td>queue</td>
<td>Show the queue of functions to be executed on the matched elements, or manipulate the queue of functions to be executed on the matched elements.</td>
</tr>
<tr>
<td>dequeue</td>
<td>Execute the next function on the queue for the matched elements.</td>
</tr>
<tr>
<td>clearQueue</td>
<td>Remove from the queue all items that have not yet been run.</td>
</tr>
<tr>
<td>delay</td>
<td>Set a timer to delay execution of subsequent items in the queue.</td>
</tr>
</table>
<p>So how do we create a queue? We simply use a queue command that takes a <code>queueName</code> argument.  For example, we can use the <code>queue</code> command and add a function to queue. Let&#8217;s try it first with the regular <code>fx</code> queue.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I was queued!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    next<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;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>When this code is run on a page with a <code>#foo</code> element, it immediately emits an alert saying &#8220;I was queued!&#8221;.  This is because the <code>fx</code> queue will dequeue elements right after they&#8217;re added.   We can verify that the <code>fx</code> queue is being used by queuing an animation before our alert.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I was queued!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    next<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;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>When this code is run the <code>#foo</code> element fades out and then an alert is shown. Now let&#8217;s try it with a custom queue.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;custom&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I was queued!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    next<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;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Nothing happens. Why? Because unlike the default <code>fx</code> queue, custom queues are not dequeued immediately. They require an explicit <code>dequeue</code> call to get them started.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;custom&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I was queued!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    next<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;">&#41;</span>
.<span style="color: #660066;">dequeue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;custom&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>What&#8217;s the point?</h3>
<p>At this point you may be wondering, what&#8217;s the point? Why would I ever need to use queues? The answer is: to implement animation time-lines. Imagine you&#8217;re making a game and you want to have an object float up wards for 2000 milliseconds. Furthermore, you would like said object to stay completely opaque for 1000 milliseconds before slowly becoming completely transparent over the remaining 1000 milliseconds.  This time-line is shown below in tabular form (assuming that the object starts at <code>top: 100px</code>).</p>
<table>
<tr>
<th>Time</th>
<th>Top</th>
<th>Opacity</th>
</tr>
<tr>
<td>0</td>
<td>100px</td>
<td>1.0</td>
</tr>
<tr>
<td>500</td>
<td>90px</td>
<td>1.0</td>
</tr>
<tr>
<td>1000</td>
<td>80px</td>
<td>1.0</td>
</tr>
<tr>
<td>1500</td>
<td>70px</td>
<td>0.5</td>
</tr>
<tr>
<td>2000</td>
<td>60px</td>
<td>0.0</td>
</tr>
</table>
<p>At first glance, it appears that the <code>animate</code> command could take care of this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#object&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>opacity<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;-=40&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">2000</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Unfortunately, this code will fade the object out over 2000 ms, instead of waiting 1000 ms then fading out over the remaining 1000 ms. <code>delay</code> can&#8217;t help either, because it would delay the upward floating as well. At this point we can either fiddle with timeouts or, you guessed it, use queues.</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:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#object&quot;</span><span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">delay</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;fader&quot;</span><span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fader&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>next<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;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>opacity<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
        <span style="color: #009900;">&#123;</span>duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> queue<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    next<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;">&#41;</span>
.<span style="color: #660066;">dequeue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fader&quot;</span><span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>top<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;-=40&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">2000</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>In this example, we have two queues: the <code>fx</code> queue and the <code>fader</code> queue. First off, we setup the <code>fader</code> queue. Since we want to wait 1000 ms before fading, we use the <code>delay</code> command with 1000 ms (line 2). Next, we queue up an animation that fades the object out over 1000 ms (line 3-7). Pay close attention to the <code>queue: false</code> option (line 5) we set in the <code>animate</code> command. This is critical, as it ensures that the animate doesn&#8217;t use the <code>fx</code> queue. Finally, we unleash the queue using <code>dequeue</code> and immediately follow it with a regular <code>fx</code>-using <code>animate</code> call to move the top of the object up 40 pixels (line 9).</p>
<h3>Conclusion</h3>
<p>jQuery makes use of implicit animation queues behind the scenes whenever you run an animation. By taking advantage of jQuery queue commands, custom animation queues can be constructed that enable certain complex animation stuctures, like time-lines, that would be otherwise impossible without using intervals or timeouts.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/06/22/how-to-use-custom-jquery-animation-queues/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Biomorph.js: Natural Selection in JavaScript</title>
		<link>http://cdmckay.org/blog/2010/06/06/biomorph-js-natural-selection-in-javascript/</link>
		<comments>http://cdmckay.org/blog/2010/06/06/biomorph-js-natural-selection-in-javascript/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 00:44:44 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Gaming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1588</guid>
		<description><![CDATA[For his 1986 book The Blind Watchmaker, Richard Dawkins developed a program that created &#8220;biomorphs&#8221;: virtual creatures created by a computer simulation.  The simulation, called Biomorph, was developed in order to demonstrate the power of natural selection.

Recently, in an effort to better understand how the Biomorph program worked, I decided to implement it myself [...]]]></description>
			<content:encoded><![CDATA[<p>For his 1986 book <a href="http://en.wikipedia.org/wiki/The_Blind_Watchmaker">The Blind Watchmaker</a>, <a href="http://en.wikipedia.org/wiki/Richard_Dawkins">Richard Dawkins</a> developed a program that created &#8220;biomorphs&#8221;: virtual creatures created by a computer simulation.  The simulation, called <a href="http://www.rennard.org/alife/english/biomintrgb.html">Biomorph</a>, was developed in order to demonstrate the power of natural selection.</p>
<p><a href="http://cdmckay.org/biomorph"><img class="aligncenter size-full wp-image-1606" title="Biomorph" src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/06/biomorph-ui.png" alt="" width="350" height="190" /></a></p>
<p>Recently, in an effort to better understand how the Biomorph program worked, I decided to implement it myself in JavaScript using the HTML5 canvas element.  <a href="http://cdmckay.org/biomorph">The result of this endeavour can be found here.</a> Read more to learn how to use the Biomorph program.</p>
<p><span id="more-1588"></span></p>
<h3>How do I work it?</h3>
<p>The user interface contains 9 biomorphs arranged in a 3&#215;3 grid.  This is a biomorph generation.  The centre the grid (with the blue border) contains the parent biomorph.  The parent is the biomorph that the remaining 8 children biomorphs are based on.  The children biomorphs are created by slightly mutating each of the eight genes in a biomorph.</p>
<p>In order to evolve a biomorph towards a certain goal, look at the 8 child biomorphs and select the one that is closest to looking like the biomorph you are aiming for.  For example, if you&#8217;re trying to make a bat biomorph, pick the biomorph that looks most like a bat.  Once you have a chosen a biomorph, click it to generate a new generation of biomorphs based on it.</p>
<p>If you choose the parent biomorph, you will simple be regenerating the child generation.  However, since each gene can move in two directions, you have 16 possible children for each parent.  Thus, if you are unhappy with your choice of children, you can try clicking the parent biomorph again to get one of the 8 other child biomorphs you haven&#8217;t seen.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/06/06/biomorph-js-natural-selection-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A little bit about Webble and JavaScript games</title>
		<link>http://cdmckay.org/blog/2010/05/19/a-little-bit-about-webble-and-javascript-games/</link>
		<comments>http://cdmckay.org/blog/2010/05/19/a-little-bit-about-webble-and-javascript-games/#comments</comments>
		<pubDate>Wed, 19 May 2010 21:37:48 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Wezzle]]></category>
		<category><![CDATA[Wezzle Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1535</guid>
		<description><![CDATA[Webble is a pure HTML/CSS/JavaScript implementation of some of the core features Wezzle game engine.  By &#8220;pure&#8221; I mean that there are no external plugins like Flash used.  Furthermore, Webble does not make use of the HTML5 canvas element: all the animation is done purely by moving around HTML elements using JavaScript.
Writing a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://couchware.ca/webble">Webble</a> is a pure HTML/CSS/JavaScript implementation of some of the core features <a href="http://couchware.ca/www/wezzle">Wezzle game engine</a>.  By &#8220;pure&#8221; I mean that there are no external plugins like Flash used.  Furthermore, Webble does not make use of the HTML5 canvas element: all the animation is done purely by moving around HTML elements using JavaScript.</p>
<p>Writing a simple game engine using the DOM and JavaScript poses some interesting problems that are atypical to regular website development.  In this article I will discuss a few of the problems that I encountered while writing Webble and how I solved them.</p>
<p><span id="more-1535"></span></p>
<h3>Tracking Mouse Movement</h3>
<p>In Wezzle, there is a selector that follows around the mouse cursor.  This was fairly easy to implement in Java, as having a mouse movement listener isn&#8217;t too costly.  In JavaScript, however, listening to the <code>mousemove</code> event is very expensive.  In fact, listening to <code>mousemove</code> slows down the browser window so much that JavaScript animations become unusable.  This is fine if your game does not animate while you listen to mouse movements.  Unfortunately, with Webble, this was not the case.  I needed to be able to follow the cursor at all times.</p>
<p>What was the solution? Webble has a 8&#215;10 grid called the &#8220;board&#8221; (see image below).  Each board cell can contain a tile. Overlayed on that board is a selector centered on whatever board cell the mouse cursor is on. Thus, Webble only needs to know when the mouse moves over a new board cell. This means that instead of using the <code>mousemove</code> event, I could instead use an invisible 8&#215;10 grid of divs and listen for their less expensive <code>mouseover</code> event.  This solution ended up working extremely well, allowing me to easily keep track of the mouse while simultaneously running animations.</p>
<p><img class="aligncenter size-full wp-image-1557" title="The We*le Board" src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/05/board.png" alt="" width="256" height="320" /></p>
<h3>Animation</h3>
<p>Animation in Webble was quite painless, thanks to jQuery. For most animations, it was as easy as setting the correct parameters for the <a href="http://api.jquery.com/animate/">animate function</a>. In particular, there were some situations were I needed animations to run in lockstep.  In Webble, when you get a line, all the tiles in that line must animated away together.  Thus, doing something like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;"><span style="color: #006600; font-style: italic;">// Gets all the line tiles on the board.</span>
<span style="color: #003366; font-weight: bold;">var</span> tiles <span style="color: #339933;">=</span> board.<span style="color: #660066;">findLineTiles</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;">// Get all the underlying DOM elements.</span>
<span style="color: #003366; font-weight: bold;">var</span> elements <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>tiles.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>tile<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #000066; font-weight: bold;">return</span> tile.<span style="color: #660066;">element</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Fade out the elements.</span>
tiles.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</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;">// Remove tiles from internal board structure.</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230;would not work very well and would be inefficient. Why? Because if I had, say, 10 tiles to animate, jQuery would create 10 tile animations.  This wasteful.  Moreover, since they&#8217;re independent animations, there&#8217;s no guarantee they will be moving the same amount each animation step. This visual disparity is exacerbated when the JS engine lags.</p>
<p>Fortunately, the jQuery animate function provides <a href="http://cdmckay.org/blog/2010/03/01/the-jquery-animate-step-callback-function/">a convenient step callback function</a> that allows you to animate many elements in lockstep. As a result, the code above can become a single animation that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;"><span style="color: #006600; font-style: italic;">// The elements to animate in lockstop.</span>
<span style="color: #003366; font-weight: bold;">var</span> elements <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> ... <span style="color: #660066;">same</span> <span style="color: #000066; font-weight: bold;">as</span> above ... <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> elementsTail <span style="color: #339933;">=</span> elements.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
elements
    .<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;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>opacity<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span>
        step<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>now<span style="color: #339933;">,</span> fx<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            elementsTail.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span>fx.<span style="color: #660066;">prop</span><span style="color: #339933;">,</span> fx.<span style="color: #660066;">now</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        complete<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;">// Remove tiles from internal board structure.</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Audio</h3>
<p>The Wezzle game has several sound effects that need to play at specifc times (i.e. when a tile is inserted, when a line is removed, etc.).  Webble features some experimental sound support via the HTML5 audio element.  Unfortunately, like HTML5 video, not everyone can agree on a format.  Firefox supports Vorbis.  Safari (and Safari Mobile) supports MP3.  Chrome supports both.  IE supports none (until IE9, in the distant, distant future).  Thus, in order to support sounds across all modern browsers I needed to make both Vorbis and MP3 versions of all the sound effects.  This is a minor inconvenience that could have been avoided if Apple just played nice and included Vorbis support, at the very least on desktop Safari.</p>
<p>Other than that issue, HTML5 audio is pretty nice to work with.  All it takes to play a sound is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;"><span style="color: #003366; font-weight: bold;">var</span> lineSound <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Audio<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sounds/line.ogg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
lineSound.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is a cinche compared to the hoops that need to be jumped through to get decent sound in Java.  Although HTML5 audio still has a little bit further to go, I think it has a happy future in HTML5.</p>
<h3>Conclusion</h3>
<p>Writing games purely in HTML, CSS and JavaScript is easier than it ever has been thanks to wonderful libraries like jQuery and next generation technologies like HTML5.  Although clever hacks are still required to make everything work together across browsers, I firmly believe that HTML/CSS/JavaScript combo will be a viable browser game platform in the near future.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/05/19/a-little-bit-about-webble-and-javascript-games/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to query posts by template in WordPress</title>
		<link>http://cdmckay.org/blog/2010/05/13/how-to-query-posts-by-template-in-wordpress/</link>
		<comments>http://cdmckay.org/blog/2010/05/13/how-to-query-posts-by-template-in-wordpress/#comments</comments>
		<pubDate>Thu, 13 May 2010 17:39:37 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1508</guid>
		<description><![CDATA[Ever wanted to filter a query_posts call by template?  If you&#8217;re reading this post, you probably have or need to right now.  You probably went to the query_posts documentation and scanned for something like &#8220;template=foo&#8221; and were deeply disappointed.  Then maybe, in an act of desperation, you started to trudge around the [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to filter a query_posts call by template?  If you&#8217;re reading this post, you probably have or need to right now.  You probably went to <a href="http://codex.wordpress.org/Function_Reference/query_posts">the query_posts documentation</a> and scanned for something like &#8220;template=foo&#8221; and were deeply disappointed.  Then maybe, in an act of desperation, you started to trudge around <a href="http://wordpress.org/extend/plugins/">the plugin library</a>.  Stop!  Stop right there!  You don&#8217;t need a plugin to do this.  Everything you need is already in <a href="http://wordpress.org">WordPress</a>.</p>
<p><span id="more-1508"></span></p>
<p>Let&#8217;s start with a question: how does WordPress associate templates?  The answer: as a secret custom field.  What&#8217;s a custom field, you ask?  They&#8217;re those useful little suckers under the content area in the WP editor:</p>
<p><a href="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/05/wordpress-custom-fields.png"><img class="aligncenter size-full wp-image-1522" title="WordPress Custom Fields" src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/05/wordpress-custom-fields.png" alt="" width="300" height="406" /></a></p>
<p>But if the template is a custom field, you ask, then how come I don&#8217;t see it down there?  Well, I just said they&#8217;re secret.  After poking around WP database, I discovered the template for a page is determined by the custom field name <code>_wp_page_template</code>.  My suspicion is that anything with the prefix _wp is hidden from the WP admin area and therefore secret.</p>
<h3>Harnessing the awesome power of custom fields</h3>
<p>On the bright side, even if you can&#8217;t see the <code>_wp_page_template</code> in the custom fields area, I can assure you it&#8217;s still there.  And if it&#8217;s there, you can use it in any of your nefarious schemes.</p>
<p>Query_posts provides 3 parameters relating to custom fields: meta_key, meta_value, and meta_compare.  With those in mind, imagine you have a template called Profile (profile.php), and you want to exclude any page that is a Profile from our query.  In order to do that, you&#8217;d would make a query_posts call like so:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;">query_posts<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'page'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'meta_key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'_wp_page_template'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'meta_value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'profile.php'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'meta_compare'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'!='</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The main thing to note here is that the template is identified by the name of the PHP file (profile.php) and not by the the name of template (Profile).</p>
<h3>Wrap-up</h3>
<p>Filtering your query_posts query by template is as easy as filtering by any custom field.  No plugin required.  Just remember: use the PHP file name as the template identifier, not the template name.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/05/13/how-to-query-posts-by-template-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP interfaces and optional parameters</title>
		<link>http://cdmckay.org/blog/2010/04/24/php-interfaces-and-optional-parameters/</link>
		<comments>http://cdmckay.org/blog/2010/04/24/php-interfaces-and-optional-parameters/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 04:03:32 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1480</guid>
		<description><![CDATA[Interfaces in object-oriented languages like Java, C# and PHP are useful language constructs for ensuring that objects will respond to certain methods.  However, unlike Java and C# (until recently), PHP has optional parameters.  As it turns out, this feature has an interesting (and a little bit unexpected) effect on how we can implement [...]]]></description>
			<content:encoded><![CDATA[<p>Interfaces in object-oriented languages like <a href="http://java.sun.com/docs/books/tutorial/java/IandI/createinterface.html">Java</a>, <a href="http://msdn.microsoft.com/en-us/library/ms173156.aspx">C#</a> and <a href="http://php.net/manual/en/language.oop5.interfaces.php">PHP</a> are useful language constructs for ensuring that objects will respond to certain methods.  However, unlike Java and C# (<a href="http://msdn.microsoft.com/en-us/library/dd264739.aspx">until recently</a>), <a href="http://php.net/manual/en/functions.arguments.php">PHP has optional parameters</a>.  As it turns out, this feature has an interesting (and a little bit unexpected) effect on how we can implement interfaces in PHP.</p>
<p><span id="more-1480"></span></p>
<p>Consider a simple interface for making objects comparable:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">interface</span> IComparable <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> compareTo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Normally, in Java and C# (before 4.0) the only way to implement this interface would be to make a carbon copy of the method signature (save for the name of the parameter) and then fill in the method body.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">class</span> NetString implements IComparable <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> compareTo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #339933;">...</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This satisfies the interface requirements because we need to have a method that responds to a method named <code>compareTo</code> with a single argument.  However, things get interesting when we introduce optional parameters.  That&#8217;s because we can write a method signature that is different from that found in the interface but still satisfies the requirement that the method have the correct name and accept only 1 parameter.</p>
<p>How is that possible?  Well, imagine we wanted our NetString class to optionally ignore case when comparing:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">class</span> NetString implements IComparable <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> compareTo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ignoreCase</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #339933;">...</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice how we slipped in another (optional) argument <code>$ignoreCase</code>? Does this code still satisfy the IComparable interface?  Absolutely.  Why?  Because we can still call <code>compareTo($object)</code> in a meaningful way, and that&#8217;s all the matters.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/24/php-interfaces-and-optional-parameters/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Adding Extension Methods to PHP</title>
		<link>http://cdmckay.org/blog/2010/04/23/adding-extension-methods-to-php/</link>
		<comments>http://cdmckay.org/blog/2010/04/23/adding-extension-methods-to-php/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 19:51:16 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1432</guid>
		<description><![CDATA[If you&#8217;ve ever used the .NET Framework version 3.5 of later, you&#8217;ll probably have encountered extension methods.  Microsoft describes extension methods as:
Extension methods enable you to &#8220;add&#8221; methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever used the .NET Framework version 3.5 of later, you&#8217;ll probably have encountered <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx">extension methods</a>.  Microsoft describes extension methods as:</p>
<blockquote><p>Extension methods enable you to &#8220;add&#8221; methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.
</p></blockquote>
<p>In this article, I will show you how to write a base class that will allow you to add methods to any PHP class that inherits from it at runtime.  You will be able to call these methods transparently, without any special syntax.</p>
<p><span id="more-1432"></span></p>
<p>For those unfamiliar with .NET, I&#8217;ll explain extension methods in a PHP context.  For example, consider the following code for a limited .NET-style string class in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;">final <span style="color: #000000; font-weight: bold;">class</span> NetString <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> toUpper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> NetString<span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> string<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NetString<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$upper</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$str</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toUpper</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">string</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// $upper now contains &quot;FOO&quot;</span></pre></div></div>

<p>What if, at some point in the future, we wanted to add a <code>toLower</code> method and we didn&#8217;t have access to the source code?  The first thing that probably comes to mind is to use inheritance.  Unfortuantely, <code>NetString</code> is a final class, so that&#8217;s out of the question.  So what else?  </p>
<p>All we&#8217;re left with is using a function or a static method.  Wouldn&#8217;t it be nice if we could do something like this?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;">NetString<span style="color: #339933;">::</span><span style="color: #004000;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;toLower&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> NetString<span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">string</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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>
&nbsp;
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NetString<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$upper</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$str</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toUpper</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">string</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// $upper now contains &quot;FOO&quot;</span>
&nbsp;
<span style="color: #000088;">$lower</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$upper</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toLower</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">string</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// $lower now contains &quot;foo&quot;</span></pre></div></div>

<p>Starting with PHP 5.3, this is possible thanks to closures and some other new features.  The rest of this article will be dedicated to explaining how to setup extension methods in PHP.</p>
<h3>How It Works</h3>
<p>In order to add extension methods to PHP, we need to do a couple of things.  First off, since an extension method is basically a missing method, we need to find some way to catch any calls to missing methods.  Fortunately, PHP provides <a href="http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods">a mechanism for doing this</a>.  It&#8217;s a magic method called <code>__call</code>.</p>
<p>What we need to do is make sure any class that we want to have extension methods have it&#8217;s <code>__call</code> method rerouted to a method dispatcher.  This dispatcher will have to be static so all instances of a class can access it.  In order to ensure that these calls are rerouted to the dispatcher, we&#8217;ll create a common base class that does that automatically for us.  We&#8217;ll call this base class <code>Extensible</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">class</span> Extensible <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">methodDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> methodDispatcher<span style="color: #009900;">&#40;</span> 
            <span style="color: #000088;">$instance</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</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></pre></div></div>

<p>Now that we have our missed calls rerouted to our dispatcher, we need two more things.  We need a way to add methods to the object (<code>addMethod</code>) and we need somewhere to store them to look them up with the dispatcher (<code>$methodTable</code>).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">class</span> Extensible <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">methodDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #990000;">static</span> <span style="color: #000088;">$methodTable</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> methodDispatcher<span style="color: #009900;">&#40;</span> 
            <span style="color: #000088;">$instance</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> addMethod<span style="color: #009900;">&#40;</span><span style="color: #000088;">$methodName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</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></pre></div></div>

<p>We&#8217;re almost there.  How can we store a method so that we can uniquely identify it?  The easiest way would be in an associative array indexed by class name.  Each value in the that associative array would be another associative array holding all the extension methods (i.e. the closures) for that class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">class</span> Extensible <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">methodDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #990000;">static</span> <span style="color: #000088;">$methodTable</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> methodDispatcher<span style="color: #009900;">&#40;</span> 
            <span style="color: #000088;">$instance</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> addMethod<span style="color: #009900;">&#40;</span><span style="color: #000088;">$methodName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> get_called_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
        <span style="color: #000088;">$table</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$methodTable</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>        
            <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                
&nbsp;
        <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$methodName</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$method</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now that we have all the extension methods stored, how do we invoke them?  Well, if they&#8217;re going to be useful at all, they&#8217;ll need to have access to the instance they&#8217;re attached to.  Like .NET, we&#8217;ll provide that automatically as the first argument.  We also need to pass the arguments that were passed in the call.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">class</span> Extensible <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">methodDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #990000;">static</span> <span style="color: #000088;">$methodTable</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> methodDispatcher<span style="color: #009900;">&#40;</span> 
            <span style="color: #000088;">$instance</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$table</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$methodTable</span><span style="color: #339933;">;</span>        
        <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$instance</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">do</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#41;</span> 
                    <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_parent_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span> <span style="color: #339933;">!==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span> <span style="color: #339933;">===</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Method not found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$func</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">array_unshift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #339933;">,</span> <span style="color: #000088;">$instance</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$func</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> addMethod<span style="color: #009900;">&#40;</span><span style="color: #000088;">$methodName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> get_called_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
        <span style="color: #000088;">$table</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$methodTable</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>        
            <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                
&nbsp;
        <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$methodName</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$method</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And we&#8217;re there.  Now, in order to add extension methods to a class, we just have to ensure that inherits from our Extensible class.  I&#8217;ll admit this is kind of a crappy requirement, but it&#8217;s the best we can do for now.</p>
<p>Here&#8217;s an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;">final <span style="color: #000000; font-weight: bold;">class</span> Bug <span style="color: #000000; font-weight: bold;">extends</span> Extensible <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$arms</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arms</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">arms</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$arms</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getArms<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">arms</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Bug<span style="color: #339933;">::</span><span style="color: #004000;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hug&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bug</span><span style="color: #339933;">,</span> <span style="color: #000088;">$otherBug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #000088;">$bug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; hugs &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$otherBug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</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;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Bug<span style="color: #339933;">::</span><span style="color: #004000;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;punch&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bug</span><span style="color: #339933;">,</span> <span style="color: #000088;">$otherBug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #000088;">$bug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
       <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; punches &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$otherBug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
       <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; with &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$bug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getArms</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; fists &quot;</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: #000088;">$doug</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Doug&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fred</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Fred&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fred</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hug</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$doug</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$doug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">punch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fred</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Limitations</h3>
<p>This implementation of extension methods in PHP isn&#8217;t all puppy dogs and ice cream.  It has its share of drawbacks:</p>
<ul>
<li>Classes must inherit from <code>Extensible</code> to be capable of having extension methods.  This sucks, but you can get around it in existing classes by using composition.</li>
<li>Speed.  Although I have not tested it myself, magic methods like <code>__call</code> are apparently quite slow.</li>
<li><del datetime="2010-04-23T19:52:15+00:00">Ignores inheritance.  This particular implementation does not follow the inheritance chain to see if any parent classes have extension methods for a given method call.  This could be easily remedied with a bit of code.</del> See the update below.</li>
</ul>
<h3>Source</h3>
<p>The source code I have written in this article is public domain, and you are free to do what you will with it. <a href="http://cdmckay.org/files/Extensible.zip">Here is the source file.</a></p>
<h3>Update</h3>
<p>I&#8217;ve updated the code so that <code>methodDispatcher</code> will search the inheritance chain for a suitable extension method.  This behaviour is illustrated by this example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #000000; font-weight: bold;">extends</span> Extensible <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Bar <span style="color: #000000; font-weight: bold;">extends</span> Foo <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
Foo<span style="color: #339933;">::</span><span style="color: #004000;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;customMethod&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Foo&quot;</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: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// $result contains the string &quot;Foo&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/23/adding-extension-methods-to-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to do an AJAX search with jQTouch, Part 1</title>
		<link>http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/</link>
		<comments>http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 06:22:35 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jqtouch]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1371</guid>
		<description><![CDATA[jQTouch is a pretty nifty little jQuery plugin for making websites look like native iPhone apps.  Unfortunately, beyond a bunch of examples included with the distribution, there&#8217;s not a whole lot of online documentation written for jQTouch.  Having started to use jQTouch for a professional project, I thought I&#8217;d help remedy this deficiency [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.jqtouch.com/">jQTouch</a> is a pretty nifty little <a href="http://jquery.com">jQuery</a> plugin for making websites look like native iPhone apps.  Unfortunately, beyond a bunch of examples included with the distribution, there&#8217;s not a whole lot of online documentation written for jQTouch.  Having started to use jQTouch for a professional project, I thought I&#8217;d help remedy this deficiency by posting what I learned along the way.</p>
<p>We&#8217;ll start with something fairly straightforward by making a jQTouch app that makes an AJAX call to a PHP script to search a database.  It then displays the results of that search in the jQTouch app.  This article is split up into two parts.  The first part deals with the jQTouch UI and the second part deals with the PHP script and the supporting JavaScript.</p>
<p>This is part 1 <a href="http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/">of a two part series</a>.</p>
<p><span id="more-1371"></span></p>
<h3>jQTouch UI</h3>
<p>Let&#8217;s start with our jQTouch UI code.  First, <a href="http://jqtouch.googlecode.com/files/jqtouch-1.0-beta-2-r109.zip">we&#8217;ll need to grab the jQTouch distribution</a>.  Use your favourite archiver to extract two folders: <code>jqtouch</code> and <code>themes</code> and put them in our document root.</p>
<p>Next, we need to create an index page, <code>index.html</code>, in our document root.  We&#8217;ll need some boilerplate to get things started:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="html4strict" style="font-family:Consolas, monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;viewport&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.com/jsapi&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span> google.load(&quot;jquery&quot;, &quot;1.4.2&quot;); <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jqtouch/jqtouch.min.js&quot;</span> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span> $.jQTouch(); <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span>&gt;</span>@import &quot;jqtouch/jqtouch.min.css&quot;;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span>&gt;</span>@import &quot;themes/jqt/theme.min.css&quot;;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Searcher<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></td></tr></table></div>

<p>Here&#8217;s the breakdown: </p>
<ul>
<li>on line 5, we tell the iPhone not to allow zooming,</li>
<li>on lines 6-7, we load jQuery from the Google CDN,</li>
<li>on line 8, we load the jQTouch JavaScript source,</li>
<li>on line 9, we initialize jQTouch,</li>
<li>on lines 10-11, we load the jQTouch core stylesheet and the theme stylesheet.</li>
</ul>
<h3>The Home Page</h3>
<p>Now that we have the boilerplate setup, it&#8217;s time to start adding some UI elements.  jQTouch makes this extremely simple by handling most of the heavy lifting (add this between the <code>body</code> tags):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="html4strict" style="font-family:Consolas, monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;home&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;current&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;toolbar&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Searcher<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;rounded&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;arrow&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#search&quot;</span>&gt;</span>Search<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>Woah, that&#8217;s a lot packed into these 8 lines.  Let&#8217;s break it down:</p>
<ul>
<li>on line 1, we have a div with the id &#8220;home&#8221;.  This is a special id for jQTouch that indicates the entry point (i.e. the first page shown).</li>
<li>on line 2-4, we have the toolbar.  This is something that is repeated for every jQTouch page.  It&#8217;s the part that is shown at the top of any iPhone GUI app and, consequently, any jQTouch app.</li>
<li>on line 5-7, we have the content of this page.  Basically, we are telling jQTouch that we want a list of rounded buttons, one of which is &#8220;Search&#8221;.  The <code>#search</code> href in the anchor tag tells jQTouch what page we want to load when that button is clicked.</li>
</ul>
<p>Before I move on, let&#8217;s look at a screenshot of what we have so far: </p>
<p><a href="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/04/home.jpg"><img src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/04/home.jpg" alt="" title="home" width="320" height="480" class="aligncenter size-full wp-image-1408" /></a></p>
<p>Not too shabby considering we&#8217;ve written all of 8 lines of HTML to make that happen!  Unfortunately, as you&#8217;ve probably noticed, clicking the Search button does nothing but make it turn green.</p>
<h3>The Search Page</h3>
<p>Let&#8217;s hook up the Search button from the previous page to a search page.  This will be where we enter our search text.  Here&#8217;s the code for the search page (it goes right after the code in the last listing):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="html4strict" style="font-family:Consolas, monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;toolbar&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Search<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;back&quot;</span>&gt;</span>Back<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;rounded&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-text&quot;</span> placeholder<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Search&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-text&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;edgetoedge&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-results&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sep&quot;</span>&gt;</span>Results<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>                
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></td></tr></table></div>

<ul>
<li>on line 1, we have the id &#8220;search&#8221;.  This corresponds to the href we had in the link on the home page.</li>
<li>on lines 2-5, we have the toolbar again, this time for the search page.  Of particular note here is the addition of the back button.  A div with class &#8220;back&#8221; is another special case for jQTouch.  It will automatically be linked back to the previous page.</li>
<li>on lines 6-8, we have another familiar face, the unordered button list.  However, this time we have an input with a special name &#8220;search-text&#8221;.  In jQTouch, any text input element that has a name that starts with &#8220;search&#8221; will act as a search input.</li>
<li>on lines 9-11, we have another unordered list of buttons, this time using a different visual class &#8220;edgetoedge&#8221;.  In this list we have one item, a separator (class &#8220;sep&#8221;), that we use to label the list as our results.  Later on, in part 2, we&#8217;ll place our search results in this list.</li>
</ul>
<p>After we add this code to our <code>index.html</code> file, let&#8217;s reload the web page in our iPhone or iPad and try clicking the Search button again.  You&#8217;ll see it brings you to a screen like this:</p>
<p><a href="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/04/search.jpg"><img src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/04/search.jpg" alt="" title="search" width="320" height="480" class="aligncenter size-full wp-image-1409" /></a></p>
<p>You&#8217;ll also notice that if you click the &#8220;Back&#8221; button at the top, it&#8217;ll bring you back to the &#8220;home&#8221; page.</p>
<h3>Conclusion</h3>
<p>In this tutorial we made a jQTouch UI that looks like an iPhone app.  We touched on a few jQTouch concepts:</p>
<ul>
<li>Pages are linked together using ids.</li>
<li>All jQTouch pages have toolbars (which is a div with class &#8220;toolbar&#8221;).  The toolbar contains an h1 element that represents the title of the page and two optional buttons, one of which can be a back button by using the class &#8220;back&#8221;.
<li>Lists of buttons are created by using unordered lists (&#8220;ul&#8221; elements) with special classes like &#8220;rounded&#8221; and &#8220;edgetoedge&#8221;.</li>
<li>Text input elements will act differently based on their name attributes.  For example, a search text input element is defined as such by having a &#8220;search&#8221; prefix in it&#8217;s name attribute.</li>
</ul>
<p>However this is only scratching the surface of jQTouch.  If you want to learn more I highly recommend your check out the <code>demos</code> directory in the jQTouch distribution.  There is also <a href="http://code.google.com/p/jqtouch/">a Google Code page</a> that has a Wiki and links to other tutorials.</p>
<h3>What Now?</h3>
<p>With respect to the UI, we&#8217;re done.  All that remains to be done now is to write a PHP script that we&#8217;ll query with an jQuery AJAX call.  <a href="http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/">This is covered in part 2 of this tutorial</a>.</p>
<h3>Source</h3>
<p>The source code I have written in this article is public domain, and you are free to do what you will with it.  Here is <a href="http://cdmckay.org/files/jqtouch-ajax-search.zip">a zip of the source, along with the required jQTouch source</a>.  It should work pretty much right out of the box.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Anders Hejlsberg talks about C# 4.0</title>
		<link>http://cdmckay.org/blog/2010/04/18/anders-hejlsberg-talks-about-c-4-0/</link>
		<comments>http://cdmckay.org/blog/2010/04/18/anders-hejlsberg-talks-about-c-4-0/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 01:46:37 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[.net 4.0]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1365</guid>
		<description><![CDATA[Channel 9 has posted a great talk by Anders Hejlsberg, the original author of Turbo Pascal, the chief architect of Delphi, and the lead architect of C#.
History lesson: Anders was lured away from Borland by Microsoft with a hefty offer of a $1.5 million signing bonus, a base salary of up to $200,000, and options [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://channel9.msdn.com/posts/matthijs/C-40-and-beyond-by-Anders-Hejlsberg/">Channel 9 has posted a great talk</a> by <a href="http://en.wikipedia.org/wiki/Anders_Hejlsberg">Anders Hejlsberg</a>, the original author of Turbo Pascal, the chief architect of Delphi, and the lead architect of C#.</p>
<p>History lesson: Anders was <a href="http://news.cnet.com/2009-1023-229218.html">lured away from Borland by Microsoft</a> with a hefty offer of a $1.5 million signing bonus, a base salary of up to $200,000, and options to buy 75,000 shares of Microsoft stock.</p></blockquote>
<p>Looks like it was worth it, as each C# release has added many interesting and powerful features, while maintaining a fine balance between complexity and simplicity.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/18/anders-hejlsberg-talks-about-c-4-0/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Trimming in ActionScript jQuery-style</title>
		<link>http://cdmckay.org/blog/2010/04/07/trimming-in-actionscript-jquery-style/</link>
		<comments>http://cdmckay.org/blog/2010/04/07/trimming-in-actionscript-jquery-style/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 18:26:45 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1344</guid>
		<description><![CDATA[Sometimes we want to remove whitespace from the ends of our strings.  In fact, this task is so common on the web that the ubiquitous jQuery library includes a utility method for that purpose.
What about in ActionScript 3?  Well, not so much.  The String class in AS3 is, in my opinion, a [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we want to remove whitespace from the ends of our strings.  In fact, this task is so common on the web that <a href="http://jquery.com">the ubiquitous jQuery library</a> includes <a href="http://api.jquery.com/jQuery.trim/">a utility method</a> for that purpose.</p>
<p>What about in ActionScript 3?  Well, not so much.  The <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.htm">String class in AS3 is</a>, in my opinion, a bit lacking compared to languages like <a href="http://msdn.microsoft.com/en-us/library/system.string.aspx">C#</a> and <a href="http://www.scala-lang.org/docu/files/api/scala/runtime/RichString.html">Scala</a>.  What&#8217;s worse, you can&#8217;t augment prototypes easily like you can in JavaScript to add missing methods (see update at the end for more information on this).</p>
<p><span id="more-1344"></span></p>
<p>So I needed to trim a string in AS3. A quick <a href="http://www.google.ca/#hl=en&#038;safe=off&#038;q=as3+trim">Google search</a> showed a couple so-so methods.  Then I got to thinking: Hey, doesn&#8217;t jQuery have a trim method?  Aren&#8217;t they both ECMAScript languages?  Shouldn&#8217;t something as basic as this be pretty similar in both languages?</p>
<p>The answer was yes to all those questions.  So I popped open <a href="http://code.jquery.com/jquery-1.4.2.js">the jQuery source</a> and did a quick search and found this a few lines into it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;"><span style="color: #006600; font-style: italic;">// Used for trimming whitespace</span>
rtrim <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^(\s|\u00A0)+|(\s|\u00A0)+$/g</span><span style="color: #339933;">,</span></pre></div></div>

<p>Perfect.  I created a StringHelper AS3 file, plunked it in, and presto, we have an efficient trimmer in AS3:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:Consolas, monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> final <span style="color: #9900cc; font-weight: bold;">class</span> StringHelper
<span style="color: #000000;">&#123;</span>               
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> StringHelper<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> trim<span style="color: #000000;">&#40;</span>str<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> str.<span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">/^</span><span style="color: #000000;">&#40;</span>\s<span style="color: #000000; font-weight: bold;">|</span>\u00A0<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+|</span><span style="color: #000000;">&#40;</span>\s<span style="color: #000000; font-weight: bold;">|</span>\u00A0<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+</span>$<span style="color: #000000; font-weight: bold;">/</span>g, <span style="color: #990000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> trimStart<span style="color: #000000;">&#40;</span>str<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> str.<span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">/^</span><span style="color: #000000;">&#40;</span>\s<span style="color: #000000; font-weight: bold;">|</span>\u00A0<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+/</span>g, <span style="color: #990000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> trimEnd<span style="color: #000000;">&#40;</span>str<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> str.<span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">&#40;</span>\s<span style="color: #000000; font-weight: bold;">|</span>\u00A0<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+</span>$<span style="color: #000000; font-weight: bold;">/</span>g, <span style="color: #990000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Update</h3>
<p>After this was <a href="http://www.reddit.com/r/flash/comments/bnrsm/trimming_in_actionscript_jquerystyle/">posted on Reddit</a>, <strong>theillustratedlife</strong> made the following comment regarding the prototype chain in ActionScript:</p>
<blockquote><p>
You are supposed to be able to enhance the ECMAScript prototypes, but you might have to call a compiler flag when you do it. If you look in the docs, you&#8217;ll notice that there are ECMAScript prototypes and interchangeable native code classes for the primitive types.</p></blockquote>
<p>After doing a bit more experimenting, it looks like you can fairly easily augment the prototype chain in ActionScript as well.  However, there are some caveats when running AS3 in strict mode.  For example, consider having the String class augmented like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:Consolas, monospace;"><span style="color: #004993;">String</span>.<span style="color: #004993;">prototype</span>.trim = <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> StringHelper.trim<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If we try to call the <code>trim()</code> method in the usual way</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:Consolas, monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> str<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;foo&quot;</span>;
str.trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>we&#8217;ll get a compile-time error because the AS3 compiler can&#8217;t find the method in the String class definition.  Fortunately, we can get around this by using a dynamic reference</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:Consolas, monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> str<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;foo&quot;</span>;
str<span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;trim&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>It&#8217;s a lot uglier, but it works.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/07/trimming-in-actionscript-jquery-style/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
