<?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>Source Allies Blog &#187; YUI</title>
	<atom:link href="http://blogs.sourceallies.com/tag/yui/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.sourceallies.com</link>
	<description>Technical and process thinking from Source Allies employees</description>
	<lastBuildDate>Thu, 19 Aug 2010 18:35:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>YUI3 Lets You Call Webservices With No Server Required</title>
		<link>http://blogs.sourceallies.com/2010/01/yui3-lets-you-call-webservices-with-no-server-required/</link>
		<comments>http://blogs.sourceallies.com/2010/01/yui3-lets-you-call-webservices-with-no-server-required/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 14:00:21 +0000</pubDate>
		<dc:creator>John  Bracy</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Webservice]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=805</guid>
		<description><![CDATA[I like YUI3, mostly because I can use it to fill in the gaps between HTML4 and HTML5, and also because a single line of code can make a button zoom around the page:

  var myAnim = new YAHOO.util.Anim&#40;'testButton', 
               [...]]]></description>
			<content:encoded><![CDATA[<p>I like <a href="http://developer.yahoo.com/yui/3/">YUI3</a>, mostly because I can use it to fill in the gaps between HTML4 and HTML5, and also because a single line of code can make a button zoom around the page:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> myAnim <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Anim</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'testButton'</span><span style="color: #339933;">,</span> 
                      <span style="color: #009900;">&#123;</span> width<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> to<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span> <span style="color: #009900;">&#125;</span>  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
                      <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> 
                      YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Easing</span>.<span style="color: #660066;">easeOut</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Also, I can define data sources and polling intervals completely client side, without having to wait for everyone to finish implementing the <b>event-source</b> tag. YUI3 defines 3 useful objects: DataSource, DataSchema, and DataType. DataSource defines a generic source of data. It could be a function, a file, or a web service. Once the DataSource has been configured, you can pull from and publish to the specified location. Here&#8217;s a simple example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">YUI<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #003366; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;datasource-function&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>Y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> getDate <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>
     <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
   dateDataSource <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Y.<span style="color: #660066;">DataSource</span>.<span style="color: #003366; font-weight: bold;">Function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>source<span style="color: #339933;">:</span>getDate<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
   callback <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
      success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Current Time is: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">response</span>.<span style="color: #660066;">results</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      failure<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error!: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">error</span>.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    dateDataSource.<span style="color: #660066;">request</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Above, we&#8217;ve got some basic YUI steps. The first line defines a block of YUI code that depends on the datasource-function component. In the block, we define a method to get the current date and time, and a DataSource the pulls its information from that function. Next up, we define a callback to handle the possible return situations when the data source requests information. The last line requests data from the function. </p>
<p>YUI will call the <b>getDate</b> function, take the output from that function and pass it as an event to the callback. If there was no problem, the browser will pop up a window telling you what time it is. </p>
<p>Enough of that, let&#8217;s turn it up to 11. We will change up the data source to pull from a webservice first:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">   stockDataSource <span style="color: #339933;">=</span> 
            <span style="color: #003366; font-weight: bold;">new</span> Y.<span style="color: #660066;">DataSource</span>.<span style="color: #660066;">Get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>source<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;http://www.stocks.com/NASDAQ&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>The <b>Get</b> data source knows to look for a URL. We&#8217;ll pretend this one will give us the NASDAQ index number. We will also pretend that the response looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;stocks<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;stock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NASDAQ<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>189.14<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/stock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/stocks<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So, now we need to define the schema for the data source. Simple enough, we just need to plug in a schema definition:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  stockDataSource.<span style="color: #660066;">plug</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>fn<span style="color: #339933;">:</span> Y.<span style="color: #660066;">Plugin</span>.<span style="color: #660066;">DataSourceXMLSchema</span><span style="color: #339933;">,</span> cfg<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
     schema<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
          resultListLocator<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;stocks&quot;</span><span style="color: #339933;">,</span>
          resultFields<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>key<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Above, we&#8217;ve defined the <b>resultListLocator</b> to the be xpath to a list of results, in this case <b>stock</b> nodes. Then we specify the fields of that we&#8217;re interested in, namely the <b>value</b> node. The DataSchema will be applied to all data coming into the data source. Now, we just modify the callback handler to deal with the new information, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">   callback <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
      success<span style="color: #339933;">:</span> 
        <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The NASDAQ Index is currently: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">response</span>.<span style="color: #660066;">results</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      ...
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The response contains an array <b>results</b>, which is a list of objects exposing the resultFields as properties. And now when <b>stockDataSource.sendRequest(null, callback);</b> is executed, the DataSource will consume the webservice, format the XML into an array of objects, attach them to the response event and pass it on to the callback function. No server handling required. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/01/yui3-lets-you-call-webservices-with-no-server-required/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
