<?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/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>coder and technology lover</description>
	<lastBuildDate>Sun, 16 May 2010 16:55:18 +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>Using jQuery to load an XML configuration file</title>
		<link>http://www.daveoncode.com/2009/03/11/using-jquery-to-load-an-xml-configuration-file/</link>
		<comments>http://www.daveoncode.com/2009/03/11/using-jquery-to-load-an-xml-configuration-file/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 16:36:40 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=346</guid>
		<description><![CDATA[At work I&#8217;m integrating Viamichelin&#8217;s map api on a site and I realized a Javascript class which works as a proxy to Viamichelin&#8217;s services. The api provide several methods to customize appearance of map&#8217;s controls and ui components (colors, dimensions, opacity and so on) but following the api&#8217;s documentation and examples this means to have]]></description>
			<content:encoded><![CDATA[<p>At work I&#8217;m integrating Viamichelin&#8217;s map api on a site and I realized a Javascript class which works as a proxy to Viamichelin&#8217;s services.<br />
The api provide several methods to customize appearance of map&#8217;s controls and ui components (colors, dimensions, opacity and so on) but following the api&#8217;s documentation and examples this means to have several &#8220;hard coded&#8221; settings inside my code, which is an issue I want to avoid, so I thought to use an external XML configuration file and then call the methods using parametric values (which are retrieved by reading the file). Is pretty simple with ajax to load a text file, the only problem is that by default (or better, by nature) ajax is asynchronous and this can be an obstacle but fortunately with jQuery (but even with other libraries and personal XMLHttpRequest implementation) we can make synchronous calls by specifying it. </p>
<p><span id="more-346"></span><br />
In jQuery my ajax call looks is the following:</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">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;viamichelin_proxy_config.xml&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;xml&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; cache<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; async<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// process xml returned</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; error<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; <span style="color: #006600; font-style: italic;">// handle error</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><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>The code above executes a synchronous (async: false) ajax call and returns an XML document object, inside &#8220;success&#8221; function&#8217;s body I pass all my desired parameters to my class constructor, in this way:</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 /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ViaMichelinMapProxy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; apiKey<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;api-key&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; language<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;language&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; genericErrorMsg<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;messages.generic-error-message&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; noResultMsg<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;messages.no-result-message&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; noDepartureMsg<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;messages.no-departure-result-message&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; noDestinationMsg<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;messages.no-destination-result-message&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; defaultZoom<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;default-zoom-level&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; highlightColor<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;route-highlight-color&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; highlightSize<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;route-highlight-size&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; highlightOpacity<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;route-highlight-opacity&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; canvasId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.map-canvas&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; routeSheetId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.route-sheet&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; departurePointId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.departure-point&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; destinationPointId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.destination-point&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; totalCostId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.total-cost&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; tollCostId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.toll-cost&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; petrolCostId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.petrol-cost&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; totalTimeId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.total-time&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; motorwayTimeId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.motorway-time&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; totalDistanceId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.total-distance&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; motorwayDistanceId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.motorway-distance&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><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>getConfig() is an utility function which, by using standard DOM functions (like getElementsByTagName()), reads and returns the required node value. Once the parameters are well defined, inside my class the parametric methods will work fine, and my colleagues won&#8217;t have to dig into my code in future to change a desired value, they only will need to open the xml file and quickly change an xml node value. The methods inside the class are so defined in this way:</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 /></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: #006600; font-style: italic;">// clean and flexible code, no hardcoded settings ^_^</span><br />
getItiAsVMComplexLayer<span style="color: #009900;">&#40;</span>_highlightColor<span style="color: #339933;">,</span> _highlightSize<span style="color: #339933;">,</span> _highlightOpacity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>My only doubt was &#8220;how much time it will takes to read and process an xml file and its several nodes?&#8221;&#8230; and the good answer was: only 0.05 seconds!! I&#8217;m very satisfied of my code implementation :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/11/using-jquery-to-load-an-xml-configuration-file/feed/</wfw:commentRss>
		<slash:comments>0</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]]></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>
]]></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>
