<?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>randys.org &#187; Code Chunks</title>
	<atom:link href="http://www.randys.org/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randys.org</link>
	<description>Wasting your precious bandwidth since 1999</description>
	<lastBuildDate>Thu, 03 Jun 2010 02:43:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Lightty Cake: CakePHP + Lighttpd Rewrite Rules</title>
		<link>http://www.randys.org/2010/01/05/lightty-cake-cakephp-lighttpd-rewrite-rules/</link>
		<comments>http://www.randys.org/2010/01/05/lightty-cake-cakephp-lighttpd-rewrite-rules/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 04:57:25 +0000</pubDate>
		<dc:creator>randy</dc:creator>
				<category><![CDATA[General Nerdery]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Code Chunks]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.randys.org/2010/01/05/lightty-cake-cakephp-lighttpd-rewrite-rules/</guid>
		<description><![CDATA[This little snippet will also allow you to capture your query string variables should the need arise. url.rewrite-once = ( "^/(css&#124;files&#124;img&#124;js)/(.*)" =&#62; "/$1/$2", "^/([^?]*)(?:\?(.+))?$" =&#62; "/index.php?url=$1&#38;$2" ) Posted via email from shakeit]]></description>
			<content:encoded><![CDATA[<p>This little snippet will also allow you to capture your query string variables should the need arise.</p>

<pre><code>url.rewrite-once = (    
    "^/(css|files|img|js)/(.*)" =&gt; "/$1/$2",
    "^/([^?]*)(?:\?(.+))?$" =&gt; "/index.php?url=$1&amp;$2"
)
</code></pre>

<p style="font-size: 10px;">
    <a href="http://posterous.com">Posted via email</a> from <a href="http://shakeit.randys.org/lightty-cake-cakephp-lighttpd-rewrite-rules">shakeit</a>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randys.org/2010/01/05/lightty-cake-cakephp-lighttpd-rewrite-rules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prototype: It&#8217;s Not Just a JavaScript Library</title>
		<link>http://www.randys.org/2009/05/14/prototype-its-not-just-a-javascript-library/</link>
		<comments>http://www.randys.org/2009/05/14/prototype-its-not-just-a-javascript-library/#comments</comments>
		<pubDate>Fri, 15 May 2009 05:08:25 +0000</pubDate>
		<dc:creator>randy</dc:creator>
				<category><![CDATA[Code Chunks]]></category>
		<category><![CDATA[General Nerdery]]></category>
		<category><![CDATA[Home]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.randys.org/2009/05/14/prototype-its-not-just-a-javascript-library/</guid>
		<description><![CDATA[Used to be that writing front-end code (HTML, CSS, JavaScript) wasn&#8217;t terribly complex. The syntax of HTML and CSS isn&#8217;t all that difficult to get the hang of and JavaScript (back in the day) was just a tool to validate form fields and play funny tricks on poor unsuspecting visitors. These days, JavaScript has become [...]]]></description>
			<content:encoded><![CDATA[<p>Used to be that writing front-end code (HTML, CSS, JavaScript) wasn&#8217;t terribly complex. The syntax of HTML and CSS isn&#8217;t all that difficult to get the hang of and JavaScript (back in the day) was just a tool to validate form fields and play funny tricks on poor unsuspecting visitors. These days, JavaScript has become <em>the</em> language for front-end development and it&#8217;s not just for printing the &#8216;lastModifiedDate&#8217; of a document.</p>

<script src="http://static.randys.org/js/prototype-date.js" type="text/javascript"></script>

<p>Anyone who has kept up with the advancements of JavaScript knows the <a href="http://www.prototypejs.org/">Prototype</a> library. For those who don&#8217;t know, it&#8217;s a JavaScript library that wraps a whole bunch of functionality into easy to use (and remember) &#8220;shortcuts&#8221; for doing things like getting elements on a page, manipulating said elements, and dealing with data. It&#8217;s all written in <a href="http://www.json.org/">JSON</a> notation and allows you do things like:</p>

<pre><code>$('element-id').addClassName('active').show();
</code></pre>

<p>Instead of</p>

<pre><code>var element = document.getElementById('element-id');
    element.className = 'active';
    element.style.display = 'block';
</code></pre>

<p>Anyway, things like Prototype, <a href="http://jquery.com/">jQuery</a>, <a href="http://www.dojotoolkit.org/">Dojo</a>, and <a href="http://developer.yahoo.com/yui/">YUI</a> all provide some convenience to writing custom JavaScript applications. I haven&#8217;t dug super deep into any of the frameworks&#8217; source (mostly because the code has been somewhat obfuscated and &#8220;compressed&#8221; to save space), but I imagine that they all have one thing in common; they make use of the <a href="http://phrogz.net/JS/Classes/ExtendingJavaScriptObjectsAndClasses.html#prototype">prototype</a> property to extend both existing and custom built objects/classes in JavaScript.</p>

<h3>The prototype Property</h3>

<p>Even if you don&#8217;t make heavy use of one of the afforementioned framework/toolkits, using the <em>prototype</em> property to extend existing JavaScript objects and/or classes can be quite useful. Say you want an easy way to print out a date. Rather than createing a separate function, just extend the <code>Date</code> object itself.</p>

<pre><code>Date.prototype.months = new Array(
    {name: "January",   abbrev: "Jan", number: "01"},
    {name: "February",  abbrev: "Feb", number: "02"},
    {name: "March",     abbrev: "Mar", number: "03"},
    {name: "April",     abbrev: "Apr", number: "04"},
    {name: "May",       abbrev: "May", number: "05"},
    {name: "June",      abbrev: "Jun", number: "06"},
    {name: "July",      abbrev: "Jul", number: "07"},
    {name: "August",    abbrev: "Aug", number: "08"},
    {name: "September", abbrev: "Sep", number: "09"},
    {name: "October",   abbrev: "Oct", number: "10"},
    {name: "November",  abbrev: "Nov", number: "11"},
    {name: "December",  abbrev: "Dec", number: "12"}
);
Date.prototype.dow = new Array(
    {name: 'Sunday',    abbrev: 'Sun', number: "01"},
    {name: 'Monday',    abbrev: 'Mon', number: "02"},
    {name: 'Tuesday',   abbrev: 'Tue', number: "03"},
    {name: 'Wednesday', abbrev: 'Wed', number: "04"},
    {name: 'Thursday',  abbrev: 'Thu', number: "05"},
    {name: 'Friday',    abbrev: 'Fri', number: "06"},
    {name: 'Saturday',  abbrev: 'Sat', number: "07"}
);
Date.prototype.getShortDate = function() {
    return this.months[this.getMonth()].abbrev + ' ' + this.getDate() + ' ' + this.getFullYear();
};
Date.prototype.getLongDate = function() {
    return this.dow[this.getDay()].name + ', ' + this.months[this.getMonth()].name + ' ' + this.getDate() + ', ' + this.getFullYear();
};
Date.prototype.getValueDate = function() {
    var d = (this.getDate() &lt; 10) ? '0'+this.getDate():this.getDate();
    return this.getFullYear() + '/' + this.months[this.getMonth()].number + '/' + d;
};

var now = new Date();
document.write(now.getLongDate());
</code></pre>

<p>And you get something like this <script type="text/javascript">var now = new Date();document.write(now.getLongDate());</script>. Handy.</p>

<p>Now, there&#8217;s a couple of issues with the above script. One, the names aren&#8217;t localized and two, there&#8217;s probalby a more efficient way to formatting a date (much like the example on <a href="http://phrogz.net/JS/Classes/ExtendingJavaScriptObjectsAndClasses.html#example2">this page</a>). But, it works in all the browsers I tested (Chrome, Firefox, IE7, Safari [Mac]).</p>

<p>You can prototype most of the default objects in JavaScript. Say you have an application the has to validate a bunch of text fields. Prototype the <code>String</code> objects to add built in parsing methods for various fields.</p>

<pre><code>String.prototype.isValidEmail = function() { ... }
String.prototype.isValidPhone = function() { ... }
</code></pre>

<p>You get the idea.</p>

<p>The <code>prototype</code> property is a handy little tool. There maybe some limitations between browsers, but overall, it should help simplify your code and prevent repetitive and reduntant methods.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randys.org/2009/05/14/prototype-its-not-just-a-javascript-library/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
