<?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>A Better Way &#187; web</title>
	<atom:link href="http://www.thejacklawson.com/index.php/tag/web/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thejacklawson.com</link>
	<description>Finding a better way to get things done, a technical journey</description>
	<lastBuildDate>Tue, 24 Aug 2010 21:10:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>n-Tiered AJAX</title>
		<link>http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/</link>
		<comments>http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 18:39:06 +0000</pubDate>
		<dc:creator>Jack</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.thejacklawson.com/?p=5</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p><span style="font-style: italic;">n</span>-Tiered AJAX is one of my my<br />
favorite development architectures. It&#8217;s how I developed Gevalum, and<br />
it&#8217;s how I&#8217;ll develop every website to come that has to touch a<br />
database, but doesn&#8217;t require a full-scale ASP.NET application<br />
platform. It&#8217;s fast, it&#8217;s scalable, and in the end, you&#8217;ve got one<br />
sweet site with amazing processing efficiency and <span style="font-style: italic;">pizazz</span>. It&#8217;s a pretty simple idea. This is how I develop:</p>
<p>First<br />
off: make a mockup of the design. Figure out what you want it to look<br />
like; draw it up in Photoshop, or write up the CSS in notepad, or<br />
however you&#8217;re doing what you do. Make a note of every bit of static<br />
information, and every bit of dynamic information, and dump it all into<br />
DIV tags.</p>
<p>Next, after the interface tier, comes the big part:<br />
your JavaScript tier. There are three distinct parts to this, although<br />
you may only require two.</p>
<p>Part 1: your object layer. If you have<br />
things like I do (players, items, etc) you&#8217;ll want to make objects. An<br />
example would be a player object, with player.strength, player.health,<br />
player.level, player.name&#8230; etc. A variable for every bit of connected<br />
information on your page.</p>
<p>Part 2: your update layer. Make a<br />
library of functions for each bit of information that you update. Don&#8217;t<br />
directly call the AJAX here; update the objects you made, or use these<br />
as a wrapper to validate then call your AJAX functions. You&#8217;ll probably<br />
want to include some DOM functions like:</p>
<p>function $(div){<br />   return document.getElementById(div);<br />}</p>
<p>and interface updater functions like:</p>
<p>function updateStats(){<br />   $(&#8220;playerStr&#8221;).innerHTML = player.str;<br />   //etc for all other stats<br />}</p>
<p>and variable updater functions like:</p>
<p>function uSTR(newStr){<br />   player.str = newStr<br />}<br />and<br />
anything else that your page does. Try to keep everything seperated; at<br />
a minimum, I always have a dom.js, functions.js, and then finally<br />
AJAX.js. You&#8217;ll call your AJAX layer by using something like this:</p>
<p>function uSTR(){<br />   player.str = evalAJAXPost(&#8220;playerstats.php?stat=str&#8221;);<br />}</p>
<p>Part<br />
3: Your AJAX layer. These are the functions that use your server-side<br />
pages. You always want to separate these because they&#8217;re things you use<br />
over, and over, and over again. Another trick that I learned a while<br />
ago, is that if you have simultaneous updates (e.g. sending dynamic<br />
updates while requesting pages), make an array of AJAX objects, and<br />
pass each page request through a different object. I&#8217;ve found that<br />
there&#8217;s no need for more than two or three (even Gevalum only has<br />
seven, and that&#8217;s probably overkill.) This is an excerpt from Gevalum&#8217;s<br />
AJAXFun.js:</p>
<p>var ajaxes = new Array();<br />//if we&#8217;re FF<br />if(window.XMLHttpRequest){<br />   ajaxes[0] = new XMLHttpRequest();<br />   ajaxes[1] = new XMLHttpRequest();<br />       ajaxes[2] = new XMLHttpRequest();<br />      ajaxes[3] = new XMLHttpRequest();<br />      ajaxes[4] = new XMLHttpRequest();<br />      ajaxes[5] = new XMLHttpRequest();<br />      ajaxes[6] = new XMLHttpRequest();<br />//or, if we&#8217;re IE<br />}else if(window.ActiveXObject){<br />       ajaxes[0] = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />      ajaxes[1] = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />      ajaxes[2] = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />      ajaxes[3] = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />      ajaxes[4] = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />      ajaxes[5] = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />      ajaxes[6] = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />}</p>
<p>function evalAJAXHtml(source,nr){<br />       ajaxes[nr].open(&#8220;GET&#8221;,source,true);<br />      ajaxes[nr].onreadystatechange = function(){<br />             try{<br />                       if(ajaxes[nr].readyState==4){<br />                               if (ajaxes[nr].status == 200){<br />                                       eval(ajaxes[nr].responseText);<br />                               }<br />                       }<br />               }<br />               catch(e){<br />                       //Exception-bug in FF<br />       }<br />      }<br />       ajaxes[nr].send(null);<br />}</p>
<p>function evalpostAJAXHtml(source,datan,nr){<br />       ajaxes[nr].open(&#8220;POST&#8221;,source,true);<br />       ajaxes[nr].setRequestHeader(&#8220;Content-type&#8221;, &#8220;application/x-www-form-urlencoded&#8221;);<br />       ajaxes[nr].setRequestHeader(&#8220;Content-length&#8221;, datan.length);<br />       ajaxes[nr].setRequestHeader(&#8220;Connection&#8221;, &#8220;close&#8221;);<br />       ajaxes[nr].onreadystatechange = function(){<br />               try{<br />                      if(ajaxes[nr].readyState==4){<br />                               if (ajaxes[nr].status == 200){<br />                                       eval(ajaxes[nr].responseText);<br />                               }<br />                       }<br />               }<br />               catch(e){<br />                       //Exception-bug in FF<br />               }<br />       }<br />       ajaxes[nr].send(datan);<br />}</p>
<p>Now,<br />
for the server-side part of this AJAX tiered application. There are a<br />
thousand ways to do this, depending on personal taste; I choose PHP,<br />
since it&#8217;s lightweight. You could use ruby, or asp, or asp.net, or<br />
anything that outputs text. All I have to do is pull the information I<br />
need, and write out my JavaScript updater function, and I&#8217;m done.</p>
<p>You<br />
may be asking: why bother? Well, the easiest is that it&#8217;s the absolute<br />
fastest way to get information securely from a database to the client.<br />
It sends the minimum information possible, and so saves you bandwidth.<br />
It&#8217;s invaluable for a little hosted server, or if you&#8217;re hosting<br />
yourself and can&#8217;t afford a business-class internet line. Not to<br />
mention, it&#8217;s faster on the user&#8217;s end, so they wait less, making them<br />
less likely to turn away from some lag-o-licious old monolith of a<br />
website. It&#8217;s streamlined, it&#8217;s dynamic; what else could you ask for?</p>
<!-- Social Bookmarking Reloaded BEGIN --><div class="social_bookmark"><em> </em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/&amp;title=n-Tiered+AJAX" title="Add 'n-Tiered AJAX' to Del.icio.us"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'n-Tiered AJAX' to Del.icio.us" alt="Add 'n-Tiered AJAX' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/&amp;title=n-Tiered+AJAX" title="Add 'n-Tiered AJAX' to digg"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'n-Tiered AJAX' to digg" alt="Add 'n-Tiered AJAX' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/&amp;title=n-Tiered+AJAX" title="Add 'n-Tiered AJAX' to reddit"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'n-Tiered AJAX' to reddit" alt="Add 'n-Tiered AJAX' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/" title="Add 'n-Tiered AJAX' to Technorati"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'n-Tiered AJAX' to Technorati" alt="Add 'n-Tiered AJAX' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/&amp;h=n-Tiered+AJAX" title="Add 'n-Tiered AJAX' to Newsvine"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/newsvine.png" title="Add 'n-Tiered AJAX' to Newsvine" alt="Add 'n-Tiered AJAX' to Newsvine" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/&amp;title=n-Tiered+AJAX" title="Add 'n-Tiered AJAX' to Stumble Upon"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'n-Tiered AJAX' to Stumble Upon" alt="Add 'n-Tiered AJAX' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/&amp;title=n-Tiered+AJAX" title="Add 'n-Tiered AJAX' to Google Bookmarks"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'n-Tiered AJAX' to Google Bookmarks" alt="Add 'n-Tiered AJAX' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="https://favorites.live.com/quickadd.aspx?url=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/&amp;title=n-Tiered+AJAX" title="Add 'n-Tiered AJAX' to Live-MSN"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/live.png" title="Add 'n-Tiered AJAX' to Live-MSN" alt="Add 'n-Tiered AJAX' to Live-MSN" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?title=n-Tiered+AJAX&amp;url=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/" title="Add 'n-Tiered AJAX' to SlashDot"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/slashdot.png" title="Add 'n-Tiered AJAX' to SlashDot" alt="Add 'n-Tiered AJAX' to SlashDot" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/&amp;t=n-Tiered+AJAX" title="Add 'n-Tiered AJAX' to FaceBook"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'n-Tiered AJAX' to FaceBook" alt="Add 'n-Tiered AJAX' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/" title="Add 'n-Tiered AJAX' to Twitter"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'n-Tiered AJAX' to Twitter" alt="Add 'n-Tiered AJAX' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/reader/link?url=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/&amp;title=n-Tiered+AJAX&amp;srcURL=http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/" title="Add 'n-Tiered AJAX' to Google Buzz"><img src="http://www.thejacklawson.com/wp-content/plugins/social-bookmarking-reloaded/googlebuzz.png" title="Add 'n-Tiered AJAX' to Google Buzz" alt="Add 'n-Tiered AJAX' to Google Buzz" /></a></div>
<!-- Social Bookmarking Reloaded END -->]]></content:encoded>
			<wfw:commentRss>http://www.thejacklawson.com/index.php/2008/02/n-tiered-ajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
