<?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>DaveOnCode &#187; ajax</title>
	<atom:link href="http://www.daveoncode.com/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>Objective C, iOS and more programming stuff</description>
	<lastBuildDate>Mon, 19 Dec 2011 12:11:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>goog.net.XhrIo: make simple ajax calls with Google Closure</title>
		<link>http://www.daveoncode.com/2009/11/17/goog-net-xhrio-make-simple-ajax-calls-with-google-closure/</link>
		<comments>http://www.daveoncode.com/2009/11/17/goog-net-xhrio-make-simple-ajax-calls-with-google-closure/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 15:10:46 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[google closure]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[xhr]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=520</guid>
		<description><![CDATA[Closure has a consistent package called goog.net, which contains a lot of classes to work with ajax and remote http requests. In this post I want to show how to create a basic xhr object to make get/post calls, listen for related ajax events and send data to server. Once imported the main js file [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/11/17/goog-net-xhrio-make-simple-ajax-calls-with-google-closure/' addthis:title='goog.net.XhrIo: make simple ajax calls with Google Closure ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Closure has a consistent package called <strong>goog.net</strong>, which contains a lot of classes to work with ajax and remote http requests. In this post I want to show how to create a basic xhr object to make get/post calls, listen for related ajax events and send data to server. Once imported the main js file (base.js), we will require the following:</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.require(&quot;goog.dom&quot;);<br />
goog.require(&quot;goog.net.XhrIo&quot;);<br />
goog.require(&quot;goog.structs.Map&quot;);<br />
goog.require(&quot;goog.Uri.QueryData&quot;);</div></td></tr></tbody></table></div>
<p>The most important import is <strong>goog.net.XhrIo</strong>, which represents the object wrapping the XmlHttpRequest and allows us to retrieve and send information to/from the server. This class is a pretty low level api, which means that several tasks are not as simple and automatic as they would in jQuery or such libraries, but on the other hand this offers us more control and consciousness of what we are going to do.<br />
The first step is instantiate an object of type <strong>goog.net.XhrIo</strong> and take a reference to it:</p>
<p><span id="more-520"></span></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> goog.<span style="color: #660066;">net</span>.<span style="color: #660066;">XhrIo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Then we will be able to listen for events:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">events</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;complete&quot;</span><span style="color: #339933;">,</span> ajaxCallBack<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>In this case we will listen to a &#8220;complete&#8221; event, which is an event always dispatched by <strong>goog.net.XhrIo</strong> even if the call fails. We can however listen to a &#8220;success&#8221;, an &#8220;error&#8221;, an &#8220;abort&#8221; or a &#8220;readystatechange&#8221; event, but in most cases listen only to &#8220;complete&#8221; is the best and easy choice. If we decide to listen to &#8220;complete&#8221; event, we will handle the result and ask to <strong>goog.net.XhrIo</strong> if the ajax call has been successful or not:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">events</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;complete&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>request.<span style="color: #660066;">isSuccess</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// do something cool&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// display an apologize message &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>To start the request we will use the <strong>send()</strong> method, which accepts the following parameters: the url to call (the only mandatory argument), the method to use (usually &#8220;GET&#8221; or &#8220;POST&#8221;),  a query string containing the parameters to send and a map (key-value object) representing custom headers to send.<br />
Before starting the ajax call, let&#8217;s see how to create an object that will holds data to send to the server by using the class <strong>goog.Uri.QueryData</strong>:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> goog.<span style="color: #660066;">Uri</span>.<span style="color: #660066;">QueryData</span>.<span style="color: #660066;">createFromMap</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> goog.<span style="color: #660066;">structs</span>.<span style="color: #660066;">Map</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Test ajax data&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; content<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; param1<span style="color: #339933;">:</span> <span style="color: #CC0000;">2500</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>As I can see from Closure reference, the approach above is the faster way to create a &#8220;data container&#8221; in order to send data with ajax. We can use the static method <strong>createFromMap()</strong> of <strong>goog.Uri.QueryData</strong> and pass an anonymous map (which has no reference) to it. A map is one of the several objects coming from the Java world that has been introduced in Closure by Google under the package <strong>goog.structs</strong>, it is essentially an object containing a certain number of keys holding a determinate value. Once we have our data stored into a <strong>goog.Uri.QueryData</strong> we will use the <strong>toString()</strong> method to send it through ajax, <strong>send()</strong> method in fact expects a string representation of the data (param1=value&amp;param2=value&#8230;) not an object:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">request.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ajax-test.jsp&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The complete example&#8217;s code is the following:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;goog.dom&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;goog.net.XhrIo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;goog.structs.Map&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;goog.Uri.QueryData&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #003366; font-weight: bold;">function</span> doRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// create the xhr object</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> goog.<span style="color: #660066;">net</span>.<span style="color: #660066;">XhrIo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// create a QueryData object by initializing it with a simple Map</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> goog.<span style="color: #660066;">Uri</span>.<span style="color: #660066;">QueryData</span>.<span style="color: #660066;">createFromMap</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> goog.<span style="color: #660066;">structs</span>.<span style="color: #660066;">Map</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Test ajax data&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; content<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; param1<span style="color: #339933;">:</span> <span style="color: #CC0000;">2500</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// listen to complete event</span><br />
&nbsp; &nbsp; goog.<span style="color: #660066;">events</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;complete&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>request.<span style="color: #660066;">isSuccess</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// inject response into the dom</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goog.<span style="color: #660066;">dom</span>.$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;response&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> request.<span style="color: #660066;">getResponseText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// print confirm to the console</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Satus code: &quot;</span><span style="color: #339933;">,</span> request.<span style="color: #660066;">getStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot; - &quot;</span><span style="color: #339933;">,</span> request.<span style="color: #660066;">getStatusText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// print error info to the console</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;Something went wrong in the ajax call. Error code: &quot;</span><span style="color: #339933;">,</span> request.<span style="color: #660066;">getLastErrorCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot; - message: &quot;</span><span style="color: #339933;">,</span> request.<span style="color: #660066;">getLastError</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// start the request by setting POST method and passing</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// the data object converted to a queryString</span><br />
&nbsp; &nbsp; request.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ajax-test.jsp&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/11/17/goog-net-xhrio-make-simple-ajax-calls-with-google-closure/' addthis:title='goog.net.XhrIo: make simple ajax calls with Google Closure ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/11/17/goog-net-xhrio-make-simple-ajax-calls-with-google-closure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Jetpack: building Firefox plugin with Javascript (and jQuery)</title>
		<link>http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/</link>
		<comments>http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/#comments</comments>
		<pubDate>Fri, 29 May 2009 15:08:00 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[browsers]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=427</guid>
		<description><![CDATA[I discovered a couple of days ago a new open source project from Mozilla labs: Jetpack. It&#8217;s an &#8220;API set&#8221; which allows to build Firefox&#8217;s plugins by using Javascript, HTML (including new HTML 5 tags: &#8220;video&#8221; and &#8220;audio&#8220;) and CSS (oh yes, in a single word Ajax). Jetpack is still in an early development phase [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/' addthis:title='Jetpack: building Firefox plugin with Javascript (and jQuery) ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I discovered a couple of days ago a new open source project from Mozilla labs: <a href="https://jetpack.mozillalabs.com">Jetpack</a>.<br />
It&#8217;s an &#8220;API set&#8221; which allows to build Firefox&#8217;s plugins by using Javascript, HTML (including new HTML 5 tags: &#8220;<strong>video</strong>&#8221; and &#8220;<strong>audio</strong>&#8220;) and CSS (oh yes, in a single word Ajax).<br />
Jetpack is still in an early development phase and released as 0.1 version.<br />
<a href="https://jetpack.mozillalabs.com/api.html">Here</a> you can find the (really brief) api documentation and <a href="https://wiki.mozilla.org/Labs/Jetpack/In_The_Wild">here</a> some examples.<br />
I would like to try it in the future</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/' addthis:title='Jetpack: building Firefox plugin with Javascript (and jQuery) ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Handle multiple ajax calls in a fixed incremental order</title>
		<link>http://www.daveoncode.com/2008/11/10/handle-multiple-ajax-calls-in-a-fixed-incremental-order/</link>
		<comments>http://www.daveoncode.com/2008/11/10/handle-multiple-ajax-calls-in-a-fixed-incremental-order/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 16:11:46 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://daveoncode.wordpress.com/?p=6</guid>
		<description><![CDATA[In a project which I&#8217;m working on, I&#8217;ve realized a Javascript object (I&#8217;ve called it MediaBrowser), which displays images and flv videos into a box (a dynamic div rendered at runtime). The data used by this object is contained into multiple XML files, which are loaded by ajax (potentially it can loads infinite files passed [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2008/11/10/handle-multiple-ajax-calls-in-a-fixed-incremental-order/' addthis:title='Handle multiple ajax calls in a fixed incremental order ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>In a project which I&#8217;m working on, I&#8217;ve realized a Javascript object (I&#8217;ve called it MediaBrowser), which displays images and flv videos into a box (a dynamic div rendered at runtime). The data used by this object is contained into multiple XML files, which are loaded by ajax (potentially it can loads infinite files passed as array of urls). The problem was: ajax is by definition asynchronous, so each time I load the page with my cool MediaBrowser, the order in which the objects (images and videos) are processed and displayed varies based on the order in which responses to the ajax calls are returned. IE: if I call file_1.xml, file_2.xml and file_3.xml, which contain respectively:</p>
<p><span id="more-6"></span></p>
<ul>
<li>file_A, file_B, file_C</li>
<li>file_D, file_E</li>
<li>file_F</li>
</ul>
<p>I can get the following display order:</p>
<ol>
<li>file_A</li>
<li>file_B</li>
<li>file_C</li>
<li>file_D</li>
<li>file_E</li>
<li>file_F</li>
</ol>
<p>which is what I want, but more probably I will get a similar order:</p>
<ol>
<li>file_D</li>
<li>file_E</li>
<li>file_A</li>
<li>file_B</li>
<li>file_C</li>
<li>file_F</li>
</ol>
<p>because the ajax responses are returned (due to the case) in this order: file_2.xml, file_1.xml and file_3.xml. This is not acceptable, specially due to a project requirement which force the reload of the page each time the user select any of the object displayed in order to see its details.</p>
<p>So, I&#8217;ve realized a sort of &#8220;priority tail&#8221; by adding an extra XML node, called &lt;priority&gt; to the XML document returned from the ajax call. This has been possible by using the parameter <em><strong>dataFilter</strong></em> provided by jQuery&#8217;s $.ajax() method, which accepts a function callback to apply an extra data-processing operation.</p>
<p>This is a piece of code:</p>
<pre>$.ajax({
  type: "GET",
  url: XMLUrl,
  dataType: "xml",
  cache: this.cacheAjax,
  success: this._storeData,
  error: this._storeError,
  dataFilter: function(data) {

    var root, newNode, newNodeValue;

    root = data.getElementsByTagName('pics')[0];
    newNode = data.createElement("priority");
    newNodeValue = data.createTextNode(priority);
    newNode.appendChild(newNodeValue);
    root.appendChild(newNode);

    return data;

  }
});</pre>
<p>The <em><strong>priority</strong></em> variable is passed as a parameter to a method (this._startAjaxRequest()) that wraps the jQuery code ($.ajax()). In this way I can store the data retrieved (basically i put the XML documents into an array) from the ajax calls randomly (based on the responses order) and then I can sort the store (by using the <em><strong>priority</strong></em> value) and display the objects always in the same order.</p>
<p>This is a piece of code of <em><strong>this._storeData()</strong></em> that shows how the data is stored for further processing operations:</p>
<pre>this._storeData = function(xhr) {

  var XMLDoc = xhr.documentElement;

  // adds the XML document to the MediaBrowser's dataStore
  instance.dataStore.push({
	doc: XMLDoc,
	priority: XMLDoc.getElementsByTagName("priority")[0].firstChild.nodeValue
  });
...</pre>
<p>&#8230;and this is the method which sorts the data into array:</p>
<pre>this._sortDataStore = function() {
  instance.dataStore.sort(
    function(a, b) {
      return a.priority - b.priority;
    }
  );
}</pre>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2008/11/10/handle-multiple-ajax-calls-in-a-fixed-incremental-order/' addthis:title='Handle multiple ajax calls in a fixed incremental order ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2008/11/10/handle-multiple-ajax-calls-in-a-fixed-incremental-order/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

