<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
	<channel>
		<title>McArthur GFX</title>
		<link>http://mcarthurgfx.com</link>
		<description>notes from a web developer</description>
		<pubDate>Thu, 29 Oct 2009 09:08:00 -0700</pubDate>
		<generator>http://www.blazonco.com/</generator>
		<language>en</language>
				<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/mcarthurgfx" type="application/rss+xml" /><feedburner:emailServiceId>mcarthurgfx</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
			<title>Select Tags in IE: innerHTML</title>
			<link>http://feedproxy.google.com/~r/mcarthurgfx/~3/pEJOU0ByT3E/select-tags-in-ie-innerhtml</link>
			<comments>http://mcarthurgfx.com/blog/article/select-tags-in-ie-innerhtml#comments</comments>
			<pubDate>Thu, 29 Oct 2009 09:08:00 -0700</pubDate>
			<dc:creator>Sean</dc:creator>
						<category><![CDATA[Javascript]]></category>
						
			<guid isPermaLink="false">http://mcarthurgfx.com/blog/article/select-tags-in-ie-innerhtml</guid>
			<description><![CDATA[<p>I just wanted to document this rather frustrating bug here, so I can look it up later, and hopefully help anyone else who is running into something similar. This bug involves <code>select</code> tags, specifically setting their <code>innerHTML&nbsp;</code>property.</p>
<p>I had a list of options to give to the user, and a select box would work perfectly. Since <acronym title="Document Object Model">DOM</acronym> methods have <a href="http://mcarthurgfx.com/blog/article/dont-use-the-dom-to-insert-flash">burned me in the past</a>, I felt <code>innerHTML</code> was the safer route. It appears, that they both have safety curves like a sine and cosine graph.</p>
<p><img src="http://mcarthurgfx.com/images/blog/safety_dom_vs_innerhtml.jpg" alt="Graph of Safety regarding DOM Methods vs innerHTML" /></p>]]></description>
			<content:encoded><![CDATA[<p>I just wanted to document this rather frustrating bug here, so I can look it up later, and hopefully help anyone else who is running into something similar. This bug involves <code>select</code> tags, specifically setting their <code>innerHTML&nbsp;</code>property.</p>
<p>I had a list of options to give to the user, and a select box would work perfectly. Since <acronym title="Document Object Model">DOM</acronym> methods have <a href="http://mcarthurgfx.com/blog/article/dont-use-the-dom-to-insert-flash">burned me in the past</a>, I felt <code>innerHTML</code> was the safer route. It appears, that they both have safety curves like a sine and cosine graph.</p>
<p><img src="http://mcarthurgfx.com/images/blog/safety_dom_vs_innerhtml.jpg" alt="Graph of Safety regarding DOM Methods vs innerHTML" /></p>
<h4>The Buggery</h4>
<p>Here's what I thought would work of stupendously!</p>
<pre><code>var options = '&lt;option&gt;' + item.options.join('&lt;/option&gt;&lt;option&gt;') + '&lt;/option&gt;';
selectEl.innerHTML = options;</code></pre>
<p>It works in Firefox, Safari, Chrome, etc. <em>Except Internet Explorer</em>. I first noticed in IE7, and so when I went to debug (using <a href="http://twitter.com/seanmonstar/status/3540098404" target="_blank">IE8's sweeter debugger</a>), I noticed it even breaks in IE8.</p>
<p>It disregards completely the first <code>&lt;option&gt;</code> in the string. Go ahead and try it yourself. Open IE8, press F12, go to Script, and try this super simple task:</p>
<pre><code>
var selectEl = document.createElement('select');
selectEl.innerHTML = '&lt;option&gt;this one breaks&lt;/option&gt;&lt;option&gt;we dont work because the first one is broken&lt;/option&gt;';
document.body.appendChild(selectEl);</code></pre>
<h4>Solution</h4>
<p>It breaks even that. So I resorted to DOM methods to build my select field.</p>
<pre><code>for(var i = 0; i &lt; item.options.length; i++) {
    var option = document.createElement('option');
    option.innerHTML = item.options[i];
    select.appendChild(option); 
}</code></pre><h3>Follow me on Twitter</h3>
<p>I have little spurts of ingenuity on Twitter, follow me: <a href="http://twitter.com/seanmonstar">@seanmonstar</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=pEJOU0ByT3E:SWBIFg8-8rc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=pEJOU0ByT3E:SWBIFg8-8rc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?i=pEJOU0ByT3E:SWBIFg8-8rc:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/mcarthurgfx/~4/pEJOU0ByT3E" height="1" width="1"/>]]></content:encoded>
		<feedburner:origLink>http://mcarthurgfx.com/blog/article/select-tags-in-ie-innerhtml</feedburner:origLink></item>
				<item>
			<title>Geocoding Addresses with Google</title>
			<link>http://feedproxy.google.com/~r/mcarthurgfx/~3/uqjr1_uRpdw/geocoding-addresses-with-google</link>
			<comments>http://mcarthurgfx.com/blog/article/geocoding-addresses-with-google#comments</comments>
			<pubDate>Wed, 14 Oct 2009 09:17:00 -0700</pubDate>
			<dc:creator>Sean</dc:creator>
						<category><![CDATA[PHP]]></category>
						
			<guid isPermaLink="false">http://mcarthurgfx.com/blog/article/geocoding-addresses-with-google</guid>
			<description><![CDATA[<p>We built a <a href="http://www.uniteeurotherapy.com/salon-locator" target="_blank">Store Locator</a> for our <a href="http://www.blazonco.com" target="_blank">Content Management System</a>, and needed to calculate what stores were close by. That's simple enough in PostgreSQL, which a Point unit type, and simple trigonometry. However, in the Admin area, where you can add new stores, I needed to accept an address in its normal format, and convert that in to longitude and latitude points to be stored. Google's Geocoding API helps us do that.</p>]]></description>
			<content:encoded><![CDATA[<p>We built a <a href="http://www.uniteeurotherapy.com/salon-locator" target="_blank">Store Locator</a> for our <a href="http://www.blazonco.com" target="_blank">Content Management System</a>, and needed to calculate what stores were close by. That's simple enough in PostgreSQL, which has a <code>Point</code> unit type, and simple trigonometry. However, in the Admin area, where you can add new stores, I needed to accept an address in its normal format, and convert that in to longitude and latitude points to be stored. Google's Geocoding API helps us do that.</p>
<h4>GoogleMaps::geocode</h4>
<pre><code>class GoogleMaps {
	
    private static $api_key = 'your_own_api_key';
    
    public static function geocode($address, $format = 'json') {
        
        $data = array(
            'q' =&gt; $address,
            'key' =&gt; self::$api_key,
            'sensor' =&gt; 'false',
            'output' =&gt; $format,
            'oe' =&gt; 'utf8'
        );
        
        $ch = curl_init();
        
        $get = 'http://maps.google.com/maps/geo?' . http_build_query($data);
        
        //set up endpoint
        curl_setopt($ch, CURLOPT_URL, $get);
        
        //make sure we get the response back
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        //setup timeout options
        curl_setopt($ch, CURLOPT_TIMEOUT, '30');
        
        $responseText = curl_exec($ch);
        
        return $responseText;
    }
    
}</code></pre>
<p>Grab your API key, plug it in, and then you can easily geocode an address. Let's take a look at the parameters that we send through curl.</p>
<ul>
<li>q - The address query.</li>
<li>key - <a href="http://code.google.com/apis/maps/signup.html" target="_blank">Your API key</a>.</li>
<li>sensor - If the device has GPS capabilities. Set this to false.</li>
<li>oe - The output encoding. You want utf8.</li>
<li>output - The output <em>format</em>. Can be json, csv, xml, or kml.</li>
</ul>
<p>I love working with JSON, since it works so perfectly in Ajax responses, and PHP has the super easy json_decode function, making it much easier to use than XML. I'll show you an example of the response you would receive in JSON:</p>
<pre><code>{
    "name": "1600 Amphitheatre Parkway, Mountain View, CA",
    "Status": {
        "code": 200,
        "request": "geocode"
    },
    "Placemark": [{
        "id": "p1",
        "address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
        "AddressDetails": {
            "Country":{
                "CountryNameCode": "US",
                "CountryName": "USA",
                "AdministrativeArea":{
                    "AdministrativeAreaName": "CA",
                    "Locality":{
                        "LocalityName": "Mountain View",
                        "Thoroughfare":{
                            "ThoroughfareName": "1600 Amphitheatre Pkwy"
                        },
                        "PostalCode":{
                            "PostalCodeNumber": "94043"
                        }
                    }
                }
            },
            "Accuracy": 8
        },
        "Point": {
          "coordinates": [ -122.0841430, 37.4219720, 0 ]
        }
    }] 
}</code></pre>
<p>Makes for easily grabbing of the longitude and latitude with <code>$response-&gt;Placemark[0]-&gt;Point-&gt;coordinates</code>.</p>
<p>If you're trying to get the address back out of the request, maybe to get the official, properly formatted address, you might hit a few quirks. Such as, some places will have a SubAdministrativeArea, and others won't. The addresses that do have that (such as a county), the LocalArea object will be inside. With a bit of wiggling, you'll get it.</p>
<p>Be sure to check out <a href="http://code.google.com/apis/maps/documentation/geocoding/index.html" target="_blank">Google Code</a> for more info on the geocoding services.</p><h3>Follow me on Twitter</h3>
<p>I have little spurts of ingenuity on Twitter, follow me: <a href="http://twitter.com/seanmonstar">@seanmonstar</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=uqjr1_uRpdw:RJgU_p6fLwk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=uqjr1_uRpdw:RJgU_p6fLwk:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?i=uqjr1_uRpdw:RJgU_p6fLwk:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/mcarthurgfx/~4/uqjr1_uRpdw" height="1" width="1"/>]]></content:encoded>
		<feedburner:origLink>http://mcarthurgfx.com/blog/article/geocoding-addresses-with-google</feedburner:origLink></item>
				<item>
			<title>Fade and Destroy Elements</title>
			<link>http://feedproxy.google.com/~r/mcarthurgfx/~3/eF8ZhM5eGiE/fade-and-destroy-elements</link>
			<comments>http://mcarthurgfx.com/blog/article/fade-and-destroy-elements#comments</comments>
			<pubDate>Wed, 30 Sep 2009 15:30:00 -0700</pubDate>
			<dc:creator>Sean</dc:creator>
						<category><![CDATA[Javascript]]></category>
						
			<guid isPermaLink="false">http://mcarthurgfx.com/blog/article/fade-and-destroy-elements</guid>
			<description><![CDATA[<p>One of my favorite methods that I use constantly in various interfaces combines fading and removing of an element. It could be a table row, or list item, or a div, but often times when I wanted to remove something,&nbsp;<em>I'd like it to remove with a fade</em>. And not always wanting to write to listen for the complete event and then remove the element. So I'll show the original I use in Prototype, and then a port to MooTools.</p>]]></description>
			<content:encoded><![CDATA[<p>One of my favorite methods that I use constantly in various interfaces combines fading and removing of an element. It could be a table row, or list item, or a div, but often times when I wanted to remove something, <em>I'd like it to remove with a fade</em>. And not always wanting to write to listen for the complete event and then remove the element. So I'll show the original I use in Prototype, and then a port to MooTools.</p>
<h4>Prototype</h4>
<pre><code>Element.addMethods({
	fadeAndRemove: function(element, duration) {
		element = $(element);
		duration = duration || 0.6;
		element.fade({
			duration:duration,
			afterFinish: function(e) {
				element.remove();
			}
		});
		return element;
	}
});</code></pre>
<h4>MooTools</h4>
<pre><code>Element.implement({
	fadeAndDestroy: function(duration) {
		duration = duration || 600;
		this.set('tween', {
			duration: duration,
			onComplete: this.destroy.bind(this)
		});
		this.fade('out');
	}
});</code></pre>
<p>The big differences in the two versions, is that Prototype's base time unit in the animations is the second, and MooTools uses the millisecond. And that Prototype calls element removal <code>remove</code>, and MooTools calls it <code>destroy</code>. Also be sure to use the proper way to extend Classes, <code>addMethods</code> or <code>implement</code>.</p>
<p>You'd proceed to use this combo function like so:</p>
<pre><code>$('elem').fadeAndDestroy();</code></pre><h3>Follow me on Twitter</h3>
<p>I have little spurts of ingenuity on Twitter, follow me: <a href="http://twitter.com/seanmonstar">@seanmonstar</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=eF8ZhM5eGiE:rG_XQ1ef7J8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=eF8ZhM5eGiE:rG_XQ1ef7J8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?i=eF8ZhM5eGiE:rG_XQ1ef7J8:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/mcarthurgfx/~4/eF8ZhM5eGiE" height="1" width="1"/>]]></content:encoded>
		<feedburner:origLink>http://mcarthurgfx.com/blog/article/fade-and-destroy-elements</feedburner:origLink></item>
				<item>
			<title>Exploring Javascript Date Math</title>
			<link>http://feedproxy.google.com/~r/mcarthurgfx/~3/AUfcuMZT7WE/exploring-javascript-date-math</link>
			<comments>http://mcarthurgfx.com/blog/article/exploring-javascript-date-math#comments</comments>
			<pubDate>Tue, 22 Sep 2009 07:25:00 -0700</pubDate>
			<dc:creator>Sean</dc:creator>
						<category><![CDATA[Javascript]]></category>
						
			<guid isPermaLink="false">http://mcarthurgfx.com/blog/article/exploring-javascript-date-math</guid>
			<description><![CDATA[<p>&nbsp;</p>
<p>Dates are a peculiar type of data that we have to work with. In some sense, they aren't a number, but instead are a combination of month, day, and year. But at the same time, in most programming languages, they are fundamentally a number: the number of seconds since the&nbsp;<a href="http://en.wikipedia.org/wiki/Unix_epoch" target="_blank">Unix epoch</a>.</p>
<p>In our heads, it's quite easy to do Date math. Moving forward a week requires just adding 7 days. How would you do that with a Javascript Date? Programming languages usually try to make working with Dates as logical as it is to do in our heads. So I a little surprised at an irregularity in using Dates in my math.</p>
<p>&nbsp;</p>]]></description>
			<content:encoded><![CDATA[<p>Dates are a peculiar type of data that we have to work with. In some sense, they aren't a number, but instead are a combination of month, day, and year. But at the same time, in most programming languages, they are fundamentally a number: the number of seconds since the <a href="http://en.wikipedia.org/wiki/Unix_epoch" target="_blank">Unix epoch</a>.</p>
<p>In our heads, it's quite easy to do Date math. Moving forward a week requires just adding 7 days. How would you do that with a Javascript Date? Programming languages usually try to make working with Dates as logical as it is to do in our heads. So I a little surprised at an irregularity in using Dates in my math.</p>
<p>Though to be fair, it seems I could blame it on Javascript's decision to make the addition operator and concatenation operator the same symbol. I only did some quick, sloppy tests in Firefox 3.5 and Chrome 3.</p>
<h4>Adding to Dates</h4>
<p>I first wanted to move a date forward a week, so I thought that perhaps I could add 7 days worth of milliseconds to the date to get next week. Go ahead and try it:</p>
<pre><code>new Date() + (1000 * 60 * 60 * 24 * 7)</code></pre>
<p>The value is a string: "Fri Sep 18 2009 16:37:01 GMT-0700 (Pacific Daylight Time)604800000". Yup, there it is! Our week's worth of milliseconds, attached on the end of a Date string. It would seem that Javascript decides that the Date should be evaluated as a string instead of a number at that point.</p>
<h5>How To Add</h5>
<p>In order to add to Dates, I had to create a new Date, using the value returned from <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Date/getTime" target="_blank">getTime</a>and adding milliseconds to it, and using that new (humongous) number in the constructor.</p>
<pre><code>var nextWeek = new Date(today.getTime() + 1000 * 60 * 60 * 24 * 7);</code></pre>
<p>Also to keep in mind here, is that unlike in, say, PHP where you provide seconds to the <a href="http://php.net/date" target="_blank">date</a> function, in Javascript, you must provide <em>milliseconds</em>, hence the extra 1000 in there.</p>
<h4>Subtracting from Dates</h4>
<p>Now, until I remembered that + means to different operations in Javascript, I was rather shocked that I could easily subtract from Dates, but not add.</p>
<pre><code>new Date() - (1000 * 60 * 60 * 24 * 7)</code></pre>
<p>Result is a number:1252714884521.This does the proper math, but not everything I had hoped and dreamed for. No matter, receiving a number is certainly a result I could expect, as opposed to the string joining from addition.</p><h3>Follow me on Twitter</h3>
<p>I have little spurts of ingenuity on Twitter, follow me: <a href="http://twitter.com/seanmonstar">@seanmonstar</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=AUfcuMZT7WE:Zemxo-_b1QM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=AUfcuMZT7WE:Zemxo-_b1QM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?i=AUfcuMZT7WE:Zemxo-_b1QM:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/mcarthurgfx/~4/AUfcuMZT7WE" height="1" width="1"/>]]></content:encoded>
		<feedburner:origLink>http://mcarthurgfx.com/blog/article/exploring-javascript-date-math</feedburner:origLink></item>
				<item>
			<title>Week Period of a Given Date</title>
			<link>http://feedproxy.google.com/~r/mcarthurgfx/~3/qzngC4Fr0aQ/week-period-of-a-given-date</link>
			<comments>http://mcarthurgfx.com/blog/article/week-period-of-a-given-date#comments</comments>
			<pubDate>Thu, 17 Sep 2009 08:00:00 -0700</pubDate>
			<dc:creator>Sean</dc:creator>
						<category><![CDATA[PHP]]></category>
						
			<guid isPermaLink="false">http://mcarthurgfx.com/blog/article/week-period-of-a-given-date</guid>
			<description><![CDATA[<p>As I work on an internal time tracker for our company, I needed to show all the TimeEntries for a specified week. To specify which week, it made sense to simply select 1 day in the week, since that's the easiest default control in Flex. &nbsp;This let's me get Sunday and Saturday, the start and end of the week, so I can build a query that grabs entries between those 2 dates.</p>]]></description>
			<content:encoded><![CDATA[<p>As I work on an internal time tracker for our company, I needed to show all the TimeEntries for a specified week. To specify which week, it made sense to simply select 1 day in the week, since that's the easiest default control in Flex. &nbsp;This let's me get Sunday and Saturday, the start and end of the week, so I can build a query that grabs entries between those 2 dates.</p>
<h4>In PHP</h4>
<pre><code>$dayOfWeek = date('w', $date);
$secondsInDay = 60 * 60 * 24;
$sunday = date('Y-m-d', $date - ($dayOfWeek * $secondsInDay));
$saturday = date('Y-m-d', $date + ((6 - $dayOfWeek) * $secondsInDay));

$entries = TimeEntry::find(array(
	'timestamp =&lt;' =&gt; $saturday,
	'timestamp &gt;=' =&gt; $sunday
));</code></pre>
<h4>In English</h4>
<p>First off, PHP's <a href="http://us2.php.net/manual/en/function.date.php" target="_blank">date</a> method can be given a parameter to return the numerical day of the week. So, if $date were today, we'd be storing 4 (Sunday is 0). Now, with just a little bit of math, we can subtract the number of days to get Sunday, and add the difference to get Saturday.</p>
<p>That last bit is a <a href="http://en.wikipedia.org/wiki/Model&ndash;view&ndash;controller" target="_blank">data model</a> searching for all entries between those dates.</p><h3>Follow me on Twitter</h3>
<p>I have little spurts of ingenuity on Twitter, follow me: <a href="http://twitter.com/seanmonstar">@seanmonstar</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=qzngC4Fr0aQ:UGGe0eOxr_4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=qzngC4Fr0aQ:UGGe0eOxr_4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?i=qzngC4Fr0aQ:UGGe0eOxr_4:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/mcarthurgfx/~4/qzngC4Fr0aQ" height="1" width="1"/>]]></content:encoded>
		<feedburner:origLink>http://mcarthurgfx.com/blog/article/week-period-of-a-given-date</feedburner:origLink></item>
				<item>
			<title>You Don't Always Need Identity Operators</title>
			<link>http://feedproxy.google.com/~r/mcarthurgfx/~3/YlLT5WQzb6o/you-dont-always-need-identity-operators</link>
			<comments>http://mcarthurgfx.com/blog/article/you-dont-always-need-identity-operators#comments</comments>
			<pubDate>Thu, 10 Sep 2009 08:00:00 -0700</pubDate>
			<dc:creator>Sean</dc:creator>
						<category><![CDATA[Javascript]]></category>
						<category><![CDATA[PHP]]></category>
						
			<guid isPermaLink="false">http://mcarthurgfx.com/blog/article/you-dont-always-need-identity-operators</guid>
			<description><![CDATA[<p>In two languages that I use often (PHP and Javascript), there's 2 different equality operators when comparing values. It's become quite common to see places expressly tell you that you should only ever use one of them. That the other is evil. People see this, and then point fingers whenever you use 2 equal signs instead of 3. Here's perfectly valid reasons to use equal operator (==) instead of identity(===).</p>]]></description>
			<content:encoded><![CDATA[<p>In two languages that I use often (PHP and Javascript), there's 2 different equality operators when comparing values. It's become quite common to see places expressly tell you that you should only ever use one of them. That the other is evil. People see this, and then point fingers whenever you use 2 equal signs instead of 3. Here's perfectly valid reasons to use equal operator (==) instead of identity(===).</p>
<p>In Javascript, depending on the browser, getting the value from inputs, especially when I expect to get a number value, can often times be a number in string format. And in those situations, <em>I don't care</em> . If I'm looking for someone to be 21, I don't care if they claim to be 21, or '21'. They both work for me.</p>
<p>My options are to use a nice, simple equal operator, or do something <strong>more verbose</strong> , <strong>harder to read</strong> , and <strong>suckier to write</strong> , to make sure I use the identity operator.</p>
<p>I'll do it this way:</p>
<pre><code>if(elem.value == 21) {
	//enter bar.
}</code>

</pre>
<p>So I don't have to write this sillyness:</p>
<pre><code>if(elem.value === 21 || elem.value === '21') {
 
}//or
if(parseInt(elem.value, 10) === 21) {
 
}</code>

</pre>
<p>Alternatively, I could write some method in PHP that manipulates a string of input. Before I do anything to it, I'm going to verify it's something I can manipulate. If it's null, or false, or an empty string, or zero, I don't much care. All those things should rejected. There's no need to use some sort of identity operator when <em>I can safely say that anything falsy is no good to me</em> .</p>
<pre><code>function echoProperName($str = null) {
	if($str != null) {
		echo ucwords($str);
	}
}</code>

</pre>
<p>I recognize I don't even need to use comparison operators at all in this example, yet this very usage I have seen flamed for not using the identity operator. Ridiculous. It doesn't fit here.</p>
<p>Programming languages give us loads of expressive tools, but by some sort of &quot;convention&quot;, we get told not to use many of them. <strong>You can smash your thumb with any tool</strong> . No need to tell me not to use '==' or '++' or some such because some people have hurt themselves with it.</p><h3>Follow me on Twitter</h3>
<p>I have little spurts of ingenuity on Twitter, follow me: <a href="http://twitter.com/seanmonstar">@seanmonstar</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=YlLT5WQzb6o:1mdPLMz9ZdM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=YlLT5WQzb6o:1mdPLMz9ZdM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?i=YlLT5WQzb6o:1mdPLMz9ZdM:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/mcarthurgfx/~4/YlLT5WQzb6o" height="1" width="1"/>]]></content:encoded>
		<feedburner:origLink>http://mcarthurgfx.com/blog/article/you-dont-always-need-identity-operators</feedburner:origLink></item>
				<item>
			<title>Protected Methods in MooTools Classes</title>
			<link>http://feedproxy.google.com/~r/mcarthurgfx/~3/vCxvuySQSU8/protected-methods-in-mootools-classes</link>
			<comments>http://mcarthurgfx.com/blog/article/protected-methods-in-mootools-classes#comments</comments>
			<pubDate>Fri, 04 Sep 2009 08:00:00 -0700</pubDate>
			<dc:creator>Sean</dc:creator>
						<category><![CDATA[Javascript]]></category>
						
			<guid isPermaLink="false">http://mcarthurgfx.com/blog/article/protected-methods-in-mootools-classes</guid>
			<description><![CDATA[<p>I've talked previously about getting <a href="http://mcarthurgfx.com/blog/article/getting-private-variables-in-a-mootools-class">private variables and methods in your classes</a> , but doing so was always kind of hacky.&nbsp; In Javascript, elements don't have a native way to hide properties.&nbsp; So we have to come up with creative ways like using <a href="http://snook.ca/archives/javascript/no-love-for-module-pattern/" target="_blank">closures</a> .&nbsp; In MooTools 1.2.3, we have a way to <strong>protect</strong> class methods.</p>]]></description>
			<content:encoded><![CDATA[<p>I've talked previously about getting <a href="http://mcarthurgfx.com/blog/article/getting-private-variables-in-a-mootools-class">private variables and methods in your classes</a> , but doing so was always kind of hacky. In Javascript, elements don't have a native way to hide properties. So we have to come up with creative ways like using <a href="http://snook.ca/archives/javascript/no-love-for-module-pattern/" target="_blank">closures</a> . In MooTools 1.2.3, we have a way to protect class methods.</p>
<h5>Private vs Protected</h5>
<p>The difference between private and protected variables in most languages is that private can <strong>only</strong> be used in the same class. Protected is similar, but it can also be used from a class that extends the base class. Private properties are off limits to children.</p>
<h4>Engines must Start before Running</h4>
<p>When writing your classes, you can specify methods that should be protected by building the function in a similar way you bind functions:</p>
<pre><code>var Engine = new Class({
	initialize: function() {
	
	},
	start: function() {
		//... starting magic ...
		this.rotate();
	},
	rotate: function() {
		var rotate = true;
	}.protect();
});</code>

</pre>
<p>See that <code>.protect()</code> on there? That function is now protected. Which means, that only other internal methods can call that function. If we instantiate a new Engine, we can't tell it to run ourselves. We can only tell it to start, and the engine will do all its starting magic before making it run. Capiche?</p>
<p>Now, when you're designing your classes, keep in mind the API you want to present the class' users. Several MooTools classes only share a couple methods for public use, and many other functions are written for internal use only. With protect, <em>you can make sure none of those methods are relied upon</em> , or <em>possibly called before another method that is supposed to be called first</em> .</p><h3>Follow me on Twitter</h3>
<p>I have little spurts of ingenuity on Twitter, follow me: <a href="http://twitter.com/seanmonstar">@seanmonstar</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=vCxvuySQSU8:jOj0LUl_9Ls:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=vCxvuySQSU8:jOj0LUl_9Ls:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?i=vCxvuySQSU8:jOj0LUl_9Ls:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/mcarthurgfx/~4/vCxvuySQSU8" height="1" width="1"/>]]></content:encoded>
		<feedburner:origLink>http://mcarthurgfx.com/blog/article/protected-methods-in-mootools-classes</feedburner:origLink></item>
				<item>
			<title>Web Developers Are Stupid</title>
			<link>http://feedproxy.google.com/~r/mcarthurgfx/~3/yRppB4ZnglI/web-developers-are-stupid</link>
			<comments>http://mcarthurgfx.com/blog/article/web-developers-are-stupid#comments</comments>
			<pubDate>Thu, 20 Aug 2009 08:00:00 -0700</pubDate>
			<dc:creator>Sean</dc:creator>
						<category><![CDATA[Opinion]]></category>
						
			<guid isPermaLink="false">http://mcarthurgfx.com/blog/article/web-developers-are-stupid</guid>
			<description><![CDATA[<p>Web development has become increasingly popular.&nbsp; Does that make it easy? Does that make us dumb?</p>
<p>Certainly, as <a href="http://michaelbraude.blogspot.com/2009/05/why-ill-never-be-web-guy.html" target="_blank">Michael Braude</a> says, the barrier to web development is incredibly low. HTML can be learned in a couple hours.&nbsp; A couple more and you can make something <em>"just work"</em> in Javascript. As a web programmer, I never have to worry about 3D rendering, rarely do I ever have to care about threading, writing to disk is taken care of for me by SQL.&nbsp; The problems we're trying to solve usually aren't so astronomical to need an ingenious algorithm designed.</p>]]></description>
			<content:encoded><![CDATA[<p>Web development has become increasingly popular.&nbsp; Does that make it easy? Does that make us dumb?</p>
<p>Certainly, as <a href="http://michaelbraude.blogspot.com/2009/05/why-ill-never-be-web-guy.html" target="_blank">Michael Braude</a> says, the barrier to web development is incredibly low. HTML can be learned in a couple hours.&nbsp; A couple more and you can make something <em>"just work"</em> in Javascript. As a web programmer, I never have to worry about 3D rendering, rarely do I ever have to care about threading, writing to disk is taken care of for me by SQL.&nbsp; The problems we're trying to solve usually aren't so astronomical to need an ingenious algorithm designed.</p>
<p>Instead, much of the work is on the user interface.&nbsp; The problems we solve is usually how to do make it simpler. It <strong>must</strong> work for general internet users.&nbsp; They must be able to use it easily. Our research and thinking must be more on human psychology and people's attempts to use our software.&nbsp; We don't have the luxury of assuming our software will be used by another nerdy programmer willing to bang away on a command line all night long.</p>
<h4>Not Smart Enough</h4>
<p>I don't mean to single Michael out (his post does have a bit of sense to it), but it's the most recent statement I've seen of this large view people seem to have towards web developers:</p>
<blockquote>
<p>The reason most people want to program for the web is that they&rsquo;re not smart enough to do anything else.&nbsp; They don&rsquo;t understand compilers, concurrency, 3D or class inheritance.&nbsp; They haven&rsquo;t got a clue why I&rsquo;d use an interface or an abstract class.&nbsp; They don&rsquo;t understand: virtual methods, pointers, references, garbage collection, finalizers, pass-by-reference vs. pass-by-value, virtual C++ destructors, or the differences between C# structs and classes.&nbsp; They also know nothing about process.&nbsp; Waterfall?&nbsp; Spiral?&nbsp; Agile?&nbsp; Forget it.</p>
</blockquote>
<p>So, can <em>"not smart web programmers"</em> understand this archaic desktop vocabulary?&nbsp; Of course.&nbsp; Compilers are used in 2 of the major development stacks (Java and .NET), we use class inheritance, interfaces, and abstraction all throughout MVC.&nbsp; And yes, several of us actually understand how it all works.&nbsp; References, garbage collection, pointers, of course!&nbsp; Many of us use all of this wizardry in building our web applications.&nbsp; At least, those that would actually call themselves programmers.</p>
<p>The fact is, most of us professional web programmers came from desktop programming.&nbsp; We've worked for desktops, now we're targeting the web.&nbsp; And we'll likely continue to go back and forth, until most&nbsp;<a href="http://www.codinghorror.com/blog/archives/001296.html" target="_blank">everything has moved to the web</a>.</p>
<h4>Why The Hating</h4>
<p>I've already said web-sites can be easily made.&nbsp; HTML is easy! So, sure, people will get involved who don't know any better.&nbsp; But that's not unique to the web.&nbsp; The same problem was around before web applications existed.&nbsp; Back then, you'd get people who take a look at BASIC, learn how to store variables, print to the screen, accept input, and write GOTOs.&nbsp; And their programs sucked, too.</p>
<p><a href="http://xkcd.com/292/" target="_blank"><img src="http://mcarthurgfx.com/images/blog/goto.png" alt="" /> </a></p>
<p>Where ever people have the power to build something, <strong>people will architect crap</strong> . Whatever comes after the web, where great and exciting things are going on, people will make crap there too. Apart from all the crap, though, there are talented programmers writing excellent applications using good design and principles.</p><h3>Follow me on Twitter</h3>
<p>I have little spurts of ingenuity on Twitter, follow me: <a href="http://twitter.com/seanmonstar">@seanmonstar</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=yRppB4ZnglI:SHJxIlgATqk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=yRppB4ZnglI:SHJxIlgATqk:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?i=yRppB4ZnglI:SHJxIlgATqk:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/mcarthurgfx/~4/yRppB4ZnglI" height="1" width="1"/>]]></content:encoded>
		<feedburner:origLink>http://mcarthurgfx.com/blog/article/web-developers-are-stupid</feedburner:origLink></item>
				<item>
			<title>One-Time Custom MooTools Events</title>
			<link>http://feedproxy.google.com/~r/mcarthurgfx/~3/KVDRjtn3Rgg/one-time-custom-mootools-events</link>
			<comments>http://mcarthurgfx.com/blog/article/one-time-custom-mootools-events#comments</comments>
			<pubDate>Mon, 17 Aug 2009 08:19:00 -0700</pubDate>
			<dc:creator>Sean</dc:creator>
						<category><![CDATA[Javascript]]></category>
						
			<guid isPermaLink="false">http://mcarthurgfx.com/blog/article/one-time-custom-mootools-events</guid>
			<description><![CDATA[<p>MooTools lets you listen to events from 2 mediums: Elements and Classes. From classes, you can fire any event you want, and listen for it elsewhere. But with Elements, events are usually tied towards native DOM events. MooTools gives you the ability to define custom events for elements, by adding an entry to <code>Element.Events</code> .</p>
<p>With this in mind, we will consolidate the process of keeping track of <strong>one-time events</strong> in our custom event.</p>]]></description>
			<content:encoded><![CDATA[<p>MooTools lets you listen to events from 2 mediums: Elements and Classes. From classes, you can fire any event you want, and listen for it elsewhere. But with Elements, events are usually tied towards native DOM events. MooTools gives you the ability to define custom events for elements, by adding an entry to <code>Element.Events</code> .</p>
<p>Some practical uses can be found, such as the domready event that the document fires, or extending the <a href="http://davidwalsh.name/mootools-add-event" target="_blank">click event to always change your cursor</a>. I want to touch on this interface though, to show how you might solve my problem I mentioned 2 weeks ago: looking for <a href="http://mcarthurgfx.com/blog/article/one-time-events-already-fired" target="_blank">events that would only fire once</a>.</p>
<p>This is only useful for Element Events. I haven't researched doing this with an event from a class. That will probably use the same principle. But let's move on!</p>
<h4>Element.Events</h4>
<p>When you add an event to <code>Element.Events</code> , you specify an object with up to 4 properties:</p>
<ul>
<li><code>base</code> : A DOM Event that is needed to trigger this. For instance, a doubleclick event would use <code>click</code> as it's base.</li>
<li><code>condition</code> : a function the returns a boolean for whether the event should fire. Our same example, doubleclick, would time the interval between clicks and return true if the time difference was small enough.</li>
<li><code>onAdd</code>: a function that gets called whenever you subscribe to the event somewhere else ( <code>someEl.addEvent('doubleclick', ... )</code> ). </li>
<li><code>onRemove</code> : a function the gets called when you called <code>removeEvent</code> </li>
</ul>
<p>With this in mind, we will consolidate the process of keeping track of <strong>one-time events</strong> in our custom event.</p>
<h4>oneClick<br /></h4>
<blockquote>
<p>Attach a listener, determine if the event has already fired, and if so, execute the listener immediately.</p>
</blockquote>
<p>I couldn't come up with an excellent custom event, so we'll pretend that I might have an element that can clicked once, and only once, no matter what. Let's implement oneClick.</p>
<p>Every time I subscribe to <code>oneClick</code> , we'll check a property to see if it's fired before, and if so, execute our new listener then and there. If not, continue as normal. Then, when the event does fire, we will set a property that the event has fired in the condition, and after that, make our condition return false, so that the base event never makes our custom event trigger more than once.</p>
<pre><code> 
Element.Events.oneClick = {
	base: 'click',
	condition: function(event) {
		var fire = !this._oneClicked;
		this._oneClicked = true;		
		return fire;
	},
	onAdd: function(fn) {
		if(this._oneClicked) {
			fn.call(this);
		}
	}
};</code></pre>
<p>Now, we can listen on an element for oneClick, and listen for it somewhere else, perhaps later. Who cares.  I can feel assured that this event will happen only once. And all new listeners that might have depended on my element being clicked (like a Start button) will work too!</p>
<pre><code>$('startButton').addEvent('oneClick', function() {
	Timer.start();
});
 
// ... else where
$('startButton').addEvent('oneClick', function() {
	Toolbar.show();
});</code></pre>
<h5>One Limitation</h5>
<p>So far, the <strong>one limitation</strong> I found, was that you had to not need the actualy Event object in your listener.  As in, you can't check the <code>event.target</code> , or mouse x and y coordinates from the event.  This is because from the onAdd method, we don't have access to a native event, since one hasn't actually fired.  I found this trying to call <code>e.stop()</code> in my testing script.  This doesn't bother me much, but be warned.</p>
<p>Implementing this for Elements was easier than for Classes, because of MooTools custom element events system that already exists. I have an actual need for the Class events to be one-time, but found this out along the way. <strong>Do you have a use for this?</strong></p><h3>Follow me on Twitter</h3>
<p>I have little spurts of ingenuity on Twitter, follow me: <a href="http://twitter.com/seanmonstar">@seanmonstar</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=KVDRjtn3Rgg:Fdhu2juvIxY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=KVDRjtn3Rgg:Fdhu2juvIxY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?i=KVDRjtn3Rgg:Fdhu2juvIxY:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/mcarthurgfx/~4/KVDRjtn3Rgg" height="1" width="1"/>]]></content:encoded>
		<feedburner:origLink>http://mcarthurgfx.com/blog/article/one-time-custom-mootools-events</feedburner:origLink></item>
				<item>
			<title>Simple Templating with MooTools Substitute</title>
			<link>http://feedproxy.google.com/~r/mcarthurgfx/~3/oTMywN2o3pk/simple-templating-with-mootools-substitute</link>
			<comments>http://mcarthurgfx.com/blog/article/simple-templating-with-mootools-substitute#comments</comments>
			<pubDate>Tue, 04 Aug 2009 09:00:00 -0700</pubDate>
			<dc:creator>Sean</dc:creator>
						<category><![CDATA[Javascript]]></category>
						
			<guid isPermaLink="false">http://mcarthurgfx.com/blog/article/simple-templating-with-mootools-substitute</guid>
			<description><![CDATA[<p>As I continue to flesh out my MooTools MVC framework, I found a neat way to implement a&nbsp;<strong>templating system</strong>, using only what MooTools gives me. The goal of templating systems is to allow you to write in your target format (usually HTML), and denote where variables should be tied in.</p>
<pre><code>var example = 'My name is {name}. &nbsp;I love {passion}';</code></pre>
<p>...</p>]]></description>
			<content:encoded><![CDATA[<p>As I continue to flesh out my MooTools MVC framework, I found a neat way to implement a <strong>templating system</strong>, using only what MooTools gives me. The goal of templating systems is to allow you to write in your target format (usually HTML), and denote where variables should be tied in.</p>
<p>MooTools gives this power directly to every String!</p>
<h4>String.prototype.substitute</h4>
<p><a href="http://mootools.net/docs/core/Native/String#String:substitute" target="_blank">Substitute</a> lets you write a string, declaring delimiters where you would like variables to belong. So we can write:</p>
<pre><code>var example = 'My name is {name}. &nbsp;I love {passion}';</code></pre>
<p>Now, all we have to do is use an object with those properties set, and call the substitute function, and it will do all the replace <a href="http://mcarthurgfx.com/blog/article/job-title-web-magician">magic</a> for us.</p>
<pre><code>var obj = { name: 'Sean', passion: 'programming' };
var subbed = example.substitute(obj);
alert(subbed); //My name is Sean. I love programming';</code></pre>
<p>Now, I said we can use this awesome power for a simple template usage. See where this is going?</p>
<h4>A Simple Template View</h4>
<p>My view has more going on than this, but I'll remove the rest so you can see simply the templating part that is going on. &nbsp;It's very easy to use, indeed!</p>
<pre><code>var View = new Class({
    
    template: '&lt;a href="/task/view/{id}"&gt;{name}&lt;/a&gt;',
    
    render: function(data) {
        var template = this.template.substitute(data);
        return new Element('div',{html: template}).getFirst();
    }
    
});
 
var firstTask = {
    id: 1,
    name:'Write Subsitute Article'
};
var taskView = new View();
document.grab(taskView.render(firstTask));</code></pre>
<p>By defining the template in a string, we just write out the html for an anchor tag of my view. &nbsp;We make a new View, make an object, and tell the view to render using the object. The last part is simply turning the string into HTML by inserting it into a div, and then grabbing the element back out.</p><h3>Follow me on Twitter</h3>
<p>I have little spurts of ingenuity on Twitter, follow me: <a href="http://twitter.com/seanmonstar">@seanmonstar</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=oTMywN2o3pk:t_NyZsta_pM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/mcarthurgfx?a=oTMywN2o3pk:t_NyZsta_pM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/mcarthurgfx?i=oTMywN2o3pk:t_NyZsta_pM:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/mcarthurgfx/~4/oTMywN2o3pk" height="1" width="1"/>]]></content:encoded>
		<feedburner:origLink>http://mcarthurgfx.com/blog/article/simple-templating-with-mootools-substitute</feedburner:origLink></item>
				
	</channel>
</rss>
