<?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>selfcontained</title>
	<atom:link href="http://www.selfcontained.us/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.selfcontained.us</link>
	<description>web-development</description>
	<lastBuildDate>Sat, 08 May 2010 19:49:39 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>First iPhone App &#8211; LDS Gems</title>
		<link>http://www.selfcontained.us/2010/05/08/first-iphone-app-lds-gems/</link>
		<comments>http://www.selfcontained.us/2010/05/08/first-iphone-app-lds-gems/#comments</comments>
		<pubDate>Sat, 08 May 2010 19:49:08 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Titanium]]></category>

		<guid isPermaLink="false">http://www.selfcontained.us/?p=201</guid>
		<description><![CDATA[After a bit of work, I finally got my first iPhone app on the App store.  It&#8217;s nothing extraordinary, but was a learning experience for the most part.  It&#8217;s called LDS Gems, and is an easier way to manage, read, and share the quotes and stories provided by the LDS church at http://gems.lds.org.
I [...]]]></description>
			<content:encoded><![CDATA[<p>After a bit of work, I finally got my first iPhone app on the App store.  It&#8217;s nothing extraordinary, but was a learning experience for the most part.  It&#8217;s called <a href="http://itunes.apple.com/us/app/lds-gems/id369807515">LDS Gems</a>, and is an easier way to manage, read, and share the quotes and stories provided by the LDS church at <a href="http://gems.lds.org">http://gems.lds.org</a>.</p>
<p>I used <a href="http://www.appcelerator.com/">Titanium Appcelerator</a> to build it, and was really impressed with how much easier it was to develop using Javascript as opposed to Objective-C.  This is probably due to having quite a bit more experience with Javascript than the later, but I really enjoyed it and would recommend it to anyone wanting to dive into iPhone App development, but not ready to take on learning Objective-C.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.selfcontained.us/2010/05/08/first-iphone-app-lds-gems/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Doctrine Model Hydration</title>
		<link>http://www.selfcontained.us/2010/03/30/doctrine-model-hydration/</link>
		<comments>http://www.selfcontained.us/2010/03/30/doctrine-model-hydration/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 17:54:21 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[doctrine]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.selfcontained.us/?p=178</guid>
		<description><![CDATA[I was just deep into figuring out what was causing a certain bug and wanted to take the time to share my findings.  When working with Doctrine we were having some scattered issues of models that we had loaded from the database, modified, and then they were getting stomped on by the database again [...]]]></description>
			<content:encoded><![CDATA[<p>I was just deep into figuring out what was causing a certain bug and wanted to take the time to share my findings.  When working with <a href="http://doctrine-project.org">Doctrine</a> we were having some scattered issues of models that we had loaded from the database, modified, and then they were getting stomped on by the database again before saving, wiping out all modifications we had made.  In this particular case we changed one attribute of a model, then called save.  Our save triggers some validation that checks quite a few relationships and business rules between them.   Somewhere in that process, a new copy of the model we were trying to save was being hydrated from the database, and overwriting our previous version.  This seemed like pretty odd, and unwanted behavior, especially in our situation.</p>
<p>After digging around Doctrine&#8217;s documentation and user group a bit, I came across an attribute setting that enables/disables this exact feature, <em>Doctrine::ATTR_HYDRATE_OVERWRITE</em>.   By default this is set to true.  If you hydrate a model from the database, and then somewhere later grab that same model through a reference, or a request to the Doctrine_Table, it will overwrite the model that is currently in memory for that key with a clean one from the database.   Setting this attribute to false was a great fix for us, and ensures our code will run as intended.</p>
<pre class="brush: php;">$doctrine = Doctrine_Manager::getInstance ();
$doctrine-&gt;setAttribute ( Doctrine::ATTR_HYDRATE_OVERWRITE, false );</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.selfcontained.us/2010/03/30/doctrine-model-hydration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting keycode values in Javascript</title>
		<link>http://www.selfcontained.us/2009/09/16/getting-keycode-values-in-javascript/</link>
		<comments>http://www.selfcontained.us/2009/09/16/getting-keycode-values-in-javascript/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 16:49:44 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.selfcontained.us/?p=168</guid>
		<description><![CDATA[I&#8217;ve had to work with getting user input values based off of the keycode from the event a few times, and figured I&#8217;d throw together some of that functionality in case anyone else is wrestling with this functionality.  Below is a simple &#8220;keycode&#8221; object that has a few utility functions, and maps of values [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve had to work with getting user input values based off of the keycode from the event a few times, and figured I&#8217;d throw together some of that functionality in case anyone else is wrestling with this functionality.  Below is a simple &#8220;<strong>keycode</strong>&#8221; object that has a few utility functions, and maps of values to keycodes.</p>
<pre class="brush: jscript;">keycode = {

	getKeyCode : function(e) {
		var keycode = null;
		if(window.event) {
			keycode = window.event.keyCode;
		}else if(e) {
			keycode = e.which;
		}
		return keycode;
	},

	getKeyCodeValue : function(keyCode, shiftKey) {
		shiftKey = shiftKey || false;
		var value = null;
		if(shiftKey === true) {
			value = this.modifiedByShift[keyCode];
		}else {
			value = this.keyCodeMap[keyCode];
		}
		return value;
	},

	getValueByEvent : function(e) {
		return this.getKeyCodeValue(this.getKeyCode(e), e.shiftKey);
	},

	keyCodeMap : {
		8:&quot;backspace&quot;, 9:&quot;tab&quot;, 13:&quot;return&quot;, 16:&quot;shift&quot;, 17:&quot;ctrl&quot;, 18:&quot;alt&quot;, 19:&quot;pausebreak&quot;, 20:&quot;capslock&quot;, 27:&quot;escape&quot;, 32:&quot; &quot;, 33:&quot;pageup&quot;,
		34:&quot;pagedown&quot;, 35:&quot;end&quot;, 36:&quot;home&quot;, 37:&quot;left&quot;, 38:&quot;up&quot;, 39:&quot;right&quot;, 40:&quot;down&quot;, 43:&quot;+&quot;, 44:&quot;printscreen&quot;, 45:&quot;insert&quot;, 46:&quot;delete&quot;,
		48:&quot;0&quot;, 49:&quot;1&quot;, 50:&quot;2&quot;, 51:&quot;3&quot;, 52:&quot;4&quot;, 53:&quot;5&quot;, 54:&quot;6&quot;, 55:&quot;7&quot;, 56:&quot;8&quot;, 57:&quot;9&quot;, 59:&quot;;&quot;,
		61:&quot;=&quot;, 65:&quot;a&quot;, 66:&quot;b&quot;, 67:&quot;c&quot;, 68:&quot;d&quot;, 69:&quot;e&quot;, 70:&quot;f&quot;, 71:&quot;g&quot;, 72:&quot;h&quot;, 73:&quot;i&quot;, 74:&quot;j&quot;, 75:&quot;k&quot;, 76:&quot;l&quot;,
		77:&quot;m&quot;, 78:&quot;n&quot;, 79:&quot;o&quot;, 80:&quot;p&quot;, 81:&quot;q&quot;, 82:&quot;r&quot;, 83:&quot;s&quot;, 84:&quot;t&quot;, 85:&quot;u&quot;, 86:&quot;v&quot;, 87:&quot;w&quot;, 88:&quot;x&quot;, 89:&quot;y&quot;, 90:&quot;z&quot;,
		96:&quot;0&quot;, 97:&quot;1&quot;, 98:&quot;2&quot;, 99:&quot;3&quot;, 100:&quot;4&quot;, 101:&quot;5&quot;, 102:&quot;6&quot;, 103:&quot;7&quot;, 104:&quot;8&quot;, 105:&quot;9&quot;,
		106: &quot;*&quot;, 107:&quot;+&quot;, 109:&quot;-&quot;, 110:&quot;.&quot;, 111: &quot;/&quot;,
		112:&quot;f1&quot;, 113:&quot;f2&quot;, 114:&quot;f3&quot;, 115:&quot;f4&quot;, 116:&quot;f5&quot;, 117:&quot;f6&quot;, 118:&quot;f7&quot;, 119:&quot;f8&quot;, 120:&quot;f9&quot;, 121:&quot;f10&quot;, 122:&quot;f11&quot;, 123:&quot;f12&quot;,
		144:&quot;numlock&quot;, 145:&quot;scrolllock&quot;, 186:&quot;;&quot;, 187:&quot;=&quot;, 188:&quot;,&quot;, 189:&quot;-&quot;, 190:&quot;.&quot;, 191:&quot;/&quot;, 192:&quot;`&quot;, 219:&quot;[&quot;, 220:&quot;\\&quot;, 221:&quot;]&quot;, 222:&quot;'&quot;
	},

	modifiedByShift : {
		192:&quot;~&quot;, 48:&quot;)&quot;, 49:&quot;!&quot;, 50:&quot;@&quot;, 51:&quot;#&quot;, 52:&quot;$&quot;, 53:&quot;%&quot;, 54:&quot;^&quot;, 55:&quot;&amp;&quot;, 56:&quot;*&quot;, 57:&quot;(&quot;, 109:&quot;_&quot;, 61:&quot;+&quot;,
		219:&quot;{&quot;, 221:&quot;}&quot;, 220:&quot;|&quot;, 59:&quot;:&quot;, 222:&quot;\&quot;&quot;, 188:&quot;&lt;&quot;, 189:&quot;&gt;&quot;, 191:&quot;?&quot;,
		96:&quot;insert&quot;, 97:&quot;end&quot;, 98:&quot;down&quot;, 99:&quot;pagedown&quot;, 100:&quot;left&quot;, 102:&quot;right&quot;, 103:&quot;home&quot;, 104:&quot;up&quot;, 105:&quot;pageup&quot;
	}

};</pre>
<p>Typically, you would have an event listener on an input that would call <strong>keycode.getValueByEvent()</strong>.  You would have to pass in the event, (which is by default the first parameter passed into your event listener function).  This code handles the shift key modifier and the value that results from using it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.selfcontained.us/2009/09/16/getting-keycode-values-in-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Scoping Javascript closures in loops</title>
		<link>http://www.selfcontained.us/2009/01/23/scoping-javascript-closures-in-loops/</link>
		<comments>http://www.selfcontained.us/2009/01/23/scoping-javascript-closures-in-loops/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 23:45:45 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[events]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://www.selfcontained.us/?p=114</guid>
		<description><![CDATA[It must have been something I ate, cause this is like the third post in 2 days I think!  This is a quick one, but super handy to know if you don&#8217;t already.  There is  semi-common problem I run into, and it has to do with scoping of closures inside of loops. [...]]]></description>
			<content:encoded><![CDATA[<p>It must have been something I ate, cause this is like the third post in 2 days I think!  This is a quick one, but super handy to know if you don&#8217;t already.  There is  semi-common problem I run into, and it has to do with scoping of closures inside of loops.  Lets get straight to the code so its easier to understand what I&#8217;m talking about:</p>
<pre class="brush: jscript;">
&lt;html&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://yui.yahooapis.com/2.6.0/build/utilities/utilities.js&quot; &gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	var values = [0,1,2,3,4,5,6,7,8,9];
	for(var idx in values) {
		//First Test - will have incorrent scoping
		YAHOO.util.Event.addListener(window, 'load', function() {
			YAHOO.util.Dom.get('wrong-scope').innerHTML += ' '+values[idx]+' ';
		});
		//Second Test - scoping will be correct
		YAHOO.util.Event.addListener(window, 'load', function(scopedValue) {
			return function() {
				YAHOO.util.Dom.get('right-scope').innerHTML += ' '+scopedValue+' ';
			}
		}(values[idx]));

	}
&lt;/script&gt;

&lt;body&gt;
&lt;div id=&quot;wrong-scope&quot;&gt;
&lt;h1&gt;Wrong Scope&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;right-scope&quot;&gt;
&lt;h1&gt;Right Scope&lt;/h1&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>In this example, there is a simple array of ordered values, and then a loop over those values.  For each iteration of the loop, there is an onload listener added that will dump that value into a div.  You&#8217;ll see the first loop always dumps 9, because the scoping is wrong when the closure executes, and the last time through the loop sets the scope of <strong>values[idx]</strong>.</p>
<p>The second section does some unique handy-work to correct the scoping.  A listener is added like before, but the closure is created in a specific fashion in order to get the scope to be the way we want at runtime.  For the closure in the second section, we create a function, and immediately execute that function, passing in a parameter that is the current value in our array of numbers.  That function runs, and returns another function that does the appending to the div of the value.  This second, <em>inner-function</em> is what will execute on page load.  Because of the outer-function we immediately called, the variable passed into it, the current value from our array, will be available, and properly scoped for our inner-function.</p>
<div style="text-align: center;">
<img src="http://www.selfcontained.us/wp-content/uploads/2009/01/scoping.gif" alt="scoping" title="scoping" width="250" height="198" class="alignnone size-full wp-image-120" />
</div>
<p>This is a handy trick when you have a situation where you are looping over a collection, and are providing some type of callback/closure for each entry, but need some proper scoping.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.selfcontained.us/2009/01/23/scoping-javascript-closures-in-loops/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Browser autocomplete and keyup events</title>
		<link>http://www.selfcontained.us/2009/01/23/browser-autocomplete-and-keyup-events/</link>
		<comments>http://www.selfcontained.us/2009/01/23/browser-autocomplete-and-keyup-events/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 19:43:36 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.selfcontained.us/?p=97</guid>
		<description><![CDATA[Browser oddities are nothing new, but I came across one today that I haven&#8217;t heard about before, and couldn&#8217;t seem to find many comments about on the interwebs.  To get to the gist of it, when the native browser autocomplete functionality pops up for a text input, it also triggers a keyup event for [...]]]></description>
			<content:encoded><![CDATA[<p>Browser oddities are nothing new, but I came across one today that I haven&#8217;t heard about before, and couldn&#8217;t seem to find many comments about on the interwebs.  To get to the gist of it, when the native browser autocomplete functionality pops up for a text input, it also triggers a keyup event for the input.  I had some logic going on where I was firing an ajax lookup request as a user types in a value, and was waiting for a delay in their typing to fire the ajax request.  I noticed I would often get two events fired at almost the same time.  At first I chalked it up to oddities with setTimeout() not being completely accurate, but with more investigation, the native autocomplete that the browser supplies was the culprit.</p>
<p>To try this for yourself, here is the test code I was using:</p>
<pre class="brush: jscript;">
&lt;html&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
		function handleEvent() {
			document.getElementById('keyup-test').innerHTML += document.getElementById('test').value + '&lt;br /&gt;';
		}
	&lt;/script&gt;
&lt;body&gt;
	&lt;form action=&quot;&quot;&gt;
	&lt;div id=&quot;keyup-test&quot;&gt;&lt;h1&gt;Test Keyup Event&lt;/h1&gt;&lt;/div&gt;
	&lt;input type=&quot;text&quot; id=&quot;test&quot; onkeypress=&quot;handleEvent();&quot; value=&quot;bradharris&quot; /&gt;
	&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>You&#8217;ll see the current value of the input written out into a div above the input on each keyup event.  To get the autocomplete functionality of the browser working, just type in a value, and then submit the page by pressing enter when the input is focused.  The browser will then have that entry in its history of values for that input, and will start firing the keyup an extra time when it finds a match and shows the autocomplete box.  To turn it off, just add <strong>autocomplete=&#8221;off&#8221;</strong><em> to the input tag, then it should only fire once per keyup.  I&#8217;m pretty perplexed as to why this exists, and I&#8217;ve seen it in IE7 and Firefox3 (Linux and Windows).  If anyone knows why its around, I&#8217;d love to hear.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.selfcontained.us/2009/01/23/browser-autocomplete-and-keyup-events/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Followup on YUI : getFirstDescendantBy()</title>
		<link>http://www.selfcontained.us/2009/01/22/followup-on-yui-getfirstdescendantby/</link>
		<comments>http://www.selfcontained.us/2009/01/22/followup-on-yui-getfirstdescendantby/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 00:59:09 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://www.selfcontained.us/?p=92</guid>
		<description><![CDATA[Awhile ago I posted on an additional function for YAHOO.util.Dom called getFirstDescendantBy().  I was following the ticket submitted to the YUI Sourceforge tracker, and saw that it had been closed out, and that a new function called YAHOO.util.Dom.getElementBy() was added to fill this request.  I decided to check out the newly available YUI [...]]]></description>
			<content:encoded><![CDATA[<p>Awhile ago<a href="http://www.selfcontained.us/2008/08/20/javascript-getfirstdescendantby/"> I posted on an additional function for YAHOO.util.Dom called <strong>getFirstDescendantBy()</strong></a>.  I was following <a href="http://sourceforge.net/tracker/?func=detail&#038;atid=836479&#038;aid=2068369&#038;group_id=165715">the ticket submitted to the YUI Sourceforge tracker</a>, and saw that it had been closed out, and that a new function called <strong>YAHOO.util.Dom.getElementBy()</strong> was added to fill this request.  I decided to check out <a href="http://github.com/yui">the newly available YUI source code on github</a>, and noticed some nice enhancements.  <strong>YAHOO.util.Dom.getElementBy(method, tag, root)</strong> is now available (not in the latest stable release yet though), and does what <strong>getFirstDescendantBy</strong> did, and in most cases its much faster.  Great job to the YUI team!</p>
<p>Instead of taking a recursive approach to walking the graph like I had, YUI is just grabbing all the children by tag name, even if you don&#8217;t supply a tag name (in this case it will use &#8216;*&#8217;).  This turns out to be much faster than the recursive approach for most cases.  If you happen to be looking for an element in a very large dom tree structure, and that object is located early on in the tree, the new YUI approach will be slower than the recursive approach.  Fortunately, when I say <strong>&#8220;large&#8221;</strong><em> dom tree, I&#8217;m talking about a tree about 8 nodes deep, iterated 500-1000 times.  Most of us don&#8217;t work with sites displaying that much html on a single page, so its definitely not a concern in my book.</p>
<p>Digging in further, I noticed that the new <strong>getElementBy</strong> just delegates to <strong>YAHOO.util.Dom.getElementsBy()</strong>, which has now been improved to accept a number of additional parameters than what 2.6.0 has available.  One of those is a boolean, <strong>firstOnly</strong>, which will stop after it finds the first match, and return it.  It looks like there are also additional parameters for passing in an object to your apply method, and making that object the scope.</p>
<p>I&#8217;m excited about this change, and it means I will soon be able to use the native YUI function for what I needed.  I&#8217;d suggest that anyone else that was using something such as the <strong>getFirstDescendantBy()</strong> that I had shared look at switching once YUI releases the new function.  Thanks again YUI.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.selfcontained.us/2009/01/22/followup-on-yui-getfirstdescendantby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Javascript widget approaches: Singleton vs Prototype</title>
		<link>http://www.selfcontained.us/2008/12/23/javascript-widget-approaches-singleton-vs-prototype/</link>
		<comments>http://www.selfcontained.us/2008/12/23/javascript-widget-approaches-singleton-vs-prototype/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 22:55:27 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://www.selfcontained.us/?p=69</guid>
		<description><![CDATA[Recently I&#8217;ve been doing some work setting up some standard javascript widgets for a web application I am working on.  By widget, I&#8217;m referring to items such as javascript date pickers, tooltips, autocomplete inputs, etc.  I&#8217;m building on top of YUI for this approach, but the principles I&#8217;d like to discuss are applicable [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been doing some work setting up some standard javascript widgets for a web application I am working on.  By widget, I&#8217;m referring to items such as javascript date pickers, tooltips, autocomplete inputs, etc.  I&#8217;m building on top of YUI for this approach, but the principles I&#8217;d like to discuss are applicable to any library.  YUI provides a fantastic javascript library, and a collection of widgets right out of the box.  More than likely, as you add them to your application, you&#8217;ll want to wrap or extend them in your own javascript implementations to get them functioning as desired.  To accomplish this, I typically have taken one of two approaches, and these are the topic I&#8217;d like to cover.  To provide a working example, I&#8217;ll use a simple wrapper for a YUI Calendar widget that is linked to a text input, and opens by clicking a calendar icon.</p>
<img src="http://www.selfcontained.us/wp-content/uploads/2008/12/widget1-150x150.gif" alt="image of a date picker widget" title="Date Picker widget" width="150" height="150" class="size-thumbnail wp-image-71" />
<h1>Prototype approach</h1>
<p>This approach basically creates an instance of the javascript widget for each input field, and the javascript widget object utilizes the prototype definition so the internal functions can be defined once in memory.  Below is an example of what a simple DatePicker widget that &#8220;wraps&#8221; the YUI Calendar widget, would look like:</p>
<pre class="brush: jscript;">
function DatePicker(icon, field) {
    this.icon = icon;
    this.field = field;
    YAHOO.util.Event.addListener(window, 'load', this.initialize, this, true);
}
DatePicker.prototype = {

	icon : null,

	field : null,

	calendar : null,

	id : 'date-calendar',

	container : null,

	initialize : function() {
		YAHOO.util.Event.addListener(this.icon, 'click', this.click, this, true);
		this.renderContainer();
	},

	renderContainer : function() {
		this.container = document.createElement('div');
		this.container.style.display = 'none';
		document.body.appendChild(this.container);
	},

	click : function(e) {
		if(this.calendar === null) {
			this.renderCalendar();
		}
		this.calendar.show();
		this.positionCalendar();
	},

	renderCalendar : function() {
		this.calendar = new YAHOO.widget.Calendar(this.field+'-calendar', this.container, { title:'Choose a date:', close:true, navigator: true } );
		this.calendar.selectEvent.subscribe(this.populateDateField, this, true);
		this.calendar.render();
	},

	positionCalendar : function() {
		var position = YAHOO.util.Dom.getXY(this.field);
		position[1] = position[1] + 25;
		YAHOO.util.Dom.setXY(this.container, position);
	},

	populateDateField : function() {
		var date = this.calendar.getSelectedDates()[0];
		YAHOO.util.Dom.get(this.field).value = date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear();
		this.calendar.hide();
	},

	hide : function() {
		if(this.calendar !== null) {
			this.calendar.hide();
		}
	}

};
</pre>
<p>The html for creating this widget is as simple as follows:</p>
<pre class="brush: xml;">
&lt;script type=&quot;text/javascript&quot;&gt;
	new DatePicker('date-icon', 'date-field');
&lt;/script&gt;
&lt;label&gt;Date: &lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;date-field&quot; id=&quot;date-field&quot; /&gt;
&lt;img src=&quot;images/calendar.png&quot; id=&quot;date-icon&quot; /&gt;
</pre>
<p>Some benefits to this approach are that the instance of the widget object has direct references to the input id and calendar icon id, and nothing has to be &#8216;inspected&#8217; at runtime execution of the events.  This leads to some cleaner code on a small level.  It also has some downsides as we&#8217;ll discuss below</p>
<h1>Singleton approach</h1>
<p>The Singleton approach creates a &#8217;singleton&#8217; wrapper object that creates <strong>ONE</strong> YUI Calendar widget that is re-used across all input fields.  At runtime, the icon clicked on is used to determine which input field is in use through an extra attribute added to the icon image called &#8216;field&#8217; that contains the id of the input it is linked to.  This extra attribute could be avoided by getting the icons previous sibling if it it important to you to validate your html.  This code would look as follows:</p>
<pre class="brush: jscript;">
DatePickerSingleton = {

	calendar : null,

	id : 'date-calendar', 

	container : 'date-calendar-container',

	activeInput : null,

	initialize : function() {
		var icons = YAHOO.util.Selector.query('.date-icon');
		YAHOO.util.Event.addListener(icons, 'click', this.click, this, true);
		this.renderContainer();
	},

	renderContainer : function() {
		var container = document.createElement('div');
		container.id = this.container;
		container.style.display = 'none';
		document.body.appendChild(container);
	},

	click : function(e) {
		this.activeInput = common.byEvent(e).getAttribute('field');
		if(this.calendar === null) {
			this.renderCalendar();
		}
		this.calendar.show();
		this.positionCalendar();
	},

	renderCalendar : function() {
		this.calendar = new YAHOO.widget.Calendar(this.id, this.container, { title:'Choose a date:', close:true, navigator: true } );
		this.calendar.selectEvent.subscribe(this.populateDateField, this, true);
		this.calendar.render();
	},

	positionCalendar : function() {
		var position = YAHOO.util.Dom.getXY(YAHOO.util.Dom.get(this.activeInput));
		position[1] = position[1] + 25;
		YAHOO.util.Dom.setXY(this.container, position);
	},

	populateDateField : function() {
		var date = this.calendar.getSelectedDates()[0];
		YAHOO.util.Dom.get(this.activeInput).value = date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear();
		this.calendar.hide();
	},

	hide : function() {
		if(this.calendar !== null) {
			this.calendar.hide();
		}
	}

};
YAHOO.util.Event.addListener(window, 'load', DatePickerSingleton.initialize, DatePickerSingleton, true);
&lt;/pre&gt;&lt;/div&gt;
The html for creating this widget is as simple as follows:
&lt;div class=&quot;code-highlight&quot;&gt;&lt;code&gt;
&lt;label&gt;Date: &lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;date_field&quot; /&gt;
&lt;img src=&quot;images/calendar.png&quot; class=&quot;date-icon&quot; field=&quot;date_field&quot; /&gt;
</pre>
<h1>Results</h1>
<p>After testing out each of these approaches using a range from 1 to 1000 inputs on a page, I noticed some interesting side effects.  Both approaches load using almost the same amount of resources.  You might think the Prototype approach would require more memory on page load to create each of the widgets for each input, but in reality, due to the prototype definition, the only additional memory needed for each widget is for the unique element id&#8217;s stored as attributes.  Each approach also uses a &#8216;lazy loading&#8217; approach, that causes the Calendar widget to not be created until the user actually clicks on an icon.  This is where the two approaches begin to differ.</p>
<p>The Singleton approach consumes a small amount of additional memory on the first click, as it creates the Calendar widget at this time.  For subsequent clicks the memory stays the same, as the objects have already been created, and are just being re-used.  A downside to this approach is that on page load, a javascript css selector query has to be executed to gather all date picker icons to set up click events for them.  This &#8216;can&#8217; be time consuming with a large number of elements (1000+).</p>
<p>The Prototype approach will consume additional memory for each new icon that is clicked, as there is a Calendar widget created lazily for each input at the runtime click event of the icon.  From my simple tests, I saw an increase ranging from 51.2 kb to 358.4 kb for each additional widget instantiated (each new icon clicked).  In contrast to the Singleton approach, on page load there is no css selector query to run in order to attach the click events, as the element ids are already in memory from the instantiation of each &#8216;wrapper&#8217; (DatePicker) object.  This saves the possibly heavy css based query, but adds an initialize function for each input to the page load, which can be time consuming as well.</p>
<p>Recently I have been using the Singleton approach for creating widgets where it is possible, as I believe it scales better, and avoids the problem of memory increasing as users go about using the application.  This can be accentuated even further when page life cycle is long such as in the case of single page web applications.  I found this little exercise interesting in my own work, and hope it is informative for some other people out there.  I&#8217;d love to hear any comments regarding this from everyone out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.selfcontained.us/2008/12/23/javascript-widget-approaches-singleton-vs-prototype/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>slikcalc 1.1 release</title>
		<link>http://www.selfcontained.us/2008/09/11/slikcalc-11-release/</link>
		<comments>http://www.selfcontained.us/2008/09/11/slikcalc-11-release/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 22:47:52 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[slikcalc]]></category>

		<guid isPermaLink="false">http://www.selfcontained.us/?p=68</guid>
		<description><![CDATA[I&#8217;ve just released a new build of slikcalc &#8211; javascript calculator library that includes a few small bug fixes, and some shortcuts to the API.  The new API for creating calculators looks as follows:

var columnCalc1 = slikcalc.create('column', {
	total: { id: 'cc-1-total' },
	registerListeners: true,
	calcOnLoad: true
});

as opposed to:

var columnCalc1 = new slikcalc.ColumnCalc({
	total: { id: 'cc-1-total' },
	registerListeners: [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just released a new build of <a href="http://slikcalc.selfcontained.us/">slikcalc &#8211; javascript calculator library</a> that includes a few small bug fixes, and some shortcuts to the API.  The new API for creating calculators looks as follows:</p>
<pre class="brush: jscript;">
var columnCalc1 = slikcalc.create('column', {
	total: { id: 'cc-1-total' },
	registerListeners: true,
	calcOnLoad: true
});
</pre>
<p>as opposed to:</p>
<pre class="brush: jscript;">
var columnCalc1 = new slikcalc.ColumnCalc({
	total: { id: 'cc-1-total' },
	registerListeners: true,
	calcOnLoad: true
});
</pre>
<p>Its a small change, but I think is easier to use.  Of course the old way will still work, so existing code will not break with the addition to the API.  Some other additions include a new fully commented debug version of the code, along with using YUI compressor for the minified version, which shaved off a few kb from previous versions.  <a href="http://slikcalc.selfcontained.us/docs/1.1/">Full documentation</a> is also included in the download.  Take a look and feel free to comment with any feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.selfcontained.us/2008/09/11/slikcalc-11-release/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Chrome &#8211; Holy Smokes</title>
		<link>http://www.selfcontained.us/2008/09/03/google-chrome-holy-smokes/</link>
		<comments>http://www.selfcontained.us/2008/09/03/google-chrome-holy-smokes/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 16:22:59 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[browser]]></category>
		<category><![CDATA[google]]></category>

		<guid isPermaLink="false">http://www.selfcontained.us/?p=67</guid>
		<description><![CDATA[The cats out of the bag, Google released their new browser, http://gears.google.com/chrome/.  I was pretty excited about the new features that this browser would include, some of which are a brand new Javascript VM called V8, along with using Webkit for the rendering engine.  The fact that each tab in the browser is [...]]]></description>
			<content:encoded><![CDATA[<p>The cats out of the bag, Google released their new browser, <a href="http://gears.google.com/chrome/">http://gears.google.com/chrome/</a>.  I was pretty excited about the new features that this browser would include, some of which are a brand new Javascript VM called V8, along with using Webkit for the rendering engine.  The fact that each tab in the browser is its own process is also awesome news for web developers.  I decided to take a run using it to run the web application I develop at my job, which is a large Case Management System by Justice Systems (http://www.justicesystems.com).</p>
<p>I decided to pick a fairly rich page with lots of content and javascript for the benchmark.  This page has multiple tabs, and updates a client side data model as the user edits it.  As far as page load times go, IE 7 and Firefox 2 were both right around 1.2 &#8211; 1.3 seconds just to render this page, and parse the javascript.    Chrome comes in at 663 milliseconds, about 50% faster.</p>
<p>The next test I performed was toggling between tabs (<a href="http://developer.yahoo.com/yui/tabview/">YUI tabs</a>).  This toggling also involves some front end validation, storing and retrieving of client side data, so I figured it was a pretty good use case for testing out the new Javascript engine.  IE 7 comes in at an average of 123 milliseconds to switch tabs, Firefox 2 at 143 milliseconds, and Chrome blows them away at 20 milliseconds.</p>
<p>With Chrome, Google is stating the the browser is a viable platform, which is great news for web developers who have started to see the limitations of current browsers.  It makes sense that Google would be the one to push a browser like this, as many of their products are web-based, and rely on a performant browser.  I can&#8217;t help but think that they have been waiting to see if other browser vendors could provide, but eventually just decided go ahead and get er dun&#8217; themselves.  Way to go Google, this is great news for users and developers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.selfcontained.us/2008/09/03/google-chrome-holy-smokes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Teaching an old Framework new tricks!</title>
		<link>http://www.selfcontained.us/2008/08/29/teaching-an-old-framework-new-tricks/</link>
		<comments>http://www.selfcontained.us/2008/08/29/teaching-an-old-framework-new-tricks/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 13:04:49 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.selfcontained.us/?p=66</guid>
		<description><![CDATA[In summary, I want to discuss an alternative approach for developing a rich, dynamic UI using Struts1.  The basic approach is:

Java Object to JSON string
JSON String consumed by Javascript
Manipulate UI, update Javascript model
On submit of form serialize Javascript model to JSON string
Set value of hidden input to JSON string
JSON string to Java Object on [...]]]></description>
			<content:encoded><![CDATA[<p>In summary, I want to discuss an alternative approach for developing a rich, dynamic UI using Struts1.  The basic approach is:</p>
<ul>
<li>Java Object to JSON string</li>
<li>JSON String consumed by Javascript</li>
<li>Manipulate UI, update Javascript model</li>
<li>On submit of form serialize Javascript model to JSON string</li>
<li>Set value of hidden input to JSON string</li>
<li>JSON string to Java Object on server side</li>
</ul>
<p>Modern web development frameworks have come a long way, in almost every language.  Many of the current popular frameworks have integrated widgets or processes for building rich ajax powered UI&#8217;s with little hand-rolled code.  This makes developing those applications faster and easier.  Unfortunately, we can&#8217;t always use the latest and greatest new framework as developers.</p>
<p>While working on a J2EE web application that was started over 5 years ago, I&#8217;ve learned a lot about how frameworks have evolved since then.  When this project was started, Struts 1 was very popular, and a proven framework, so was naturally adopted for the front end.  Over time, many things were built to integrate with Struts 1, but provide custom functionality that was required.  This makes it very difficult, and highly improbably that a new front end framework can be swapped in due to time and money that would have to be invested.  At some point that may be a necessity, but until then, you have to work with what you have.</p>
<p>Building a rich UI using ajax in Struts 1 is &#8230; interesting.  Sruts1 is very form centric, and all of your form inputs in your UI map directly back to some field on your Java form object in one way or another.  This makes it tricky to provide a flexible data structure for a rich UI that may be adding or removing elements.  Try adding/removing elements to your page dynamically, and having them map back to your Struts1 Form in a clean way and you&#8217;ll see what I&#8217;m talking about.  A recent approach I took to tackling this situation was to depend very little on the framework.  </p>
<p>Lets say I have a Java object graph I want to load into some front end javascript model to work with in the UI.  I can fetch this asynchronously as the page loads as a JSON payload, or just dump it to the page into the appropriate place so my javascript consumes it.  Either way, I need to make sure its painless to convert this data structure to a JSON string, or create it from a JSON string.  This will make passing it back and forth from the server side to the client side much easier.  </p>
<p>I can then use this as my front end data model to display, change, remove and add data.  When I&#8217;m ready to commit my changes, I then have to get that back to my server side Java action.  Obviously I could also make the persistence portion asynchronous as well, but sometimes thats problematic, as you may have a lot that goes on in the backend and the rendering of a new page (security, alerts, etc.) that you would have to replicate if you were to do this asynchronously.  To keep everything working as normal, one approach is to take your javascript data graph, and serialize it into a json string and set it into an html hidden inputs value.  This input can be mapped back to your Struts form, so back in your action, you have a json string you decode and create the appropriate Java objects from.  I&#8217;ve found this to be a very useful approach when trying to build a very rich UI on top of an older framework.  In summary, here are the steps followed:</p>
<ul>
<li>Build your data as Java objects that can be serialized into JSON, and created from a JSON string.</li>
<li>Consume the JSON data into your javascript and work with it as needed in the front end.</li>
<li>On submit of the form, serialize the Javascript object into a JSON string and set it onto the value of a hidden input</li>
<li>Get the value of the hidden input on the server side as your framework supports, and convert the JSON string back to your Java objects</li>
<li>Enjoy the praises as everyone is marveled at what you did (actually now days people already expect a rich UI, so you will probably just get to keep your job as the reward)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.selfcontained.us/2008/08/29/teaching-an-old-framework-new-tricks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
