<?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>JavaChap Blog &#187; Programming</title>
	<atom:link href="http://blog.javachap.com/index.php/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.javachap.com</link>
	<description>Java and Technology musings for the masses</description>
	<lastBuildDate>Tue, 15 Nov 2011 10:19:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>&#9733; YUI Components with DWR &#8211; DWR DataSource</title>
		<link>http://blog.javachap.com/index.php/yui-components-with-dwr-dwrdatasource/</link>
		<comments>http://blog.javachap.com/index.php/yui-components-with-dwr-dwrdatasource/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 09:22:28 +0000</pubDate>
		<dc:creator>JavaChap</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[datasource]]></category>
		<category><![CDATA[dwr]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://blog.javachap.com/?p=23</guid>
		<description><![CDATA[I'm working on project which uses YUI and DWR. The hardest part is intergrating these two. I googled on how people integrated YUI and DWR, found this and this. But these solutions will only work with earlier versions of YUI. I'm using YUI 2.7. So i ended up writing my own DWRDataSource. Might be useful [...]]]></description>
			<content:encoded><![CDATA[<p>I'm working on project which uses YUI and DWR. The hardest part is intergrating these two. I googled on how people integrated YUI and DWR, found <a href="http://www.jroller.com/carlossg/entry/ajax_with_yahoo_ui_components">this</a> and <a href="http://tech.groups.yahoo.com/group/ydn-javascript/message/27281">this</a>. But these solutions will only work with earlier versions of YUI. I'm using YUI 2.7. So i ended up writing my own DWRDataSource. Might be useful for guys who want to integrate DWR and YUI 2.7.</p>
<p><strong>Drawbacks :</strong></p>
<ul>
<li>This version unlike XHRDataSource does not support queuing and aborting the requests, as DWR has no support for aborting the requests.</li>
<li>No support to call java methods which has multiple arguments. Eg: You cannot invoke UserService.getUser(name, email).</li>
</ul>
<pre class="brush: jscript; title: ; notranslate">
YAHOO.namespace(&quot;javachap.util&quot;);
javachap = YAHOO.javachap;

var DS = YAHOO.util.DataSourceBase,
	lang = YAHOO.lang;

javachap.util.DWRDataSource = function(oLiveData, oConfigs) {
    this.dataType = DS.TYPE_XHR;
    oLiveData = oLiveData || &quot;&quot;;

    javachap.util.DWRDataSource.superclass.constructor.call(this, oLiveData, oConfigs);
};

//DWRDataSource extends DataSourceBase
YAHOO.lang.extend(javachap.util.DWRDataSource, DS, {
	/**
	 * Overriding method passes query to Connection Manager. The returned
	 * response is then forwarded to the handleResponse function.
	 *
	 * @method makeConnection
	 * @param oRequest {Object} Request object.
	 * @param oCallback {Object} Callback object literal.
	 * @param oCaller {Object} (deprecated) Use oCallback.scope.
	 * @return {Number} Transaction ID.
	 */
	makeConnection : function(oRequest, oCallback, oCaller) {
	    var oRawResponse = null;
	    var tId = DS._nTransactionId++;
	    this.fireEvent(&quot;requestEvent&quot;, {tId:tId,request:oRequest,callback:oCallback,caller:oCaller});

	    // Set up the callback object and
	    var oSelf = this;

	    /**
	     * Define DWR success handler
	     *
	     * @method _dwrSuccess
	     * @param oResponse {Object} DWR Response
	     * @private
	     */
	    var _dwrSuccess = function(oResponse) {
	        // If response ID does not match last made request ID,
	        // silently fail and wait for the next response
	        if(oResponse &amp;&amp; (this.connXhrMode == &quot;ignoreStaleResponses&quot;) &amp;&amp;
	                (oResponse.tId != oQueue.conn.tId)) {
	            YAHOO.log(&quot;Ignored stale response&quot;, &quot;warn&quot;, this.toString());
	            return null;
	        }
	        // Error if no response
	        else if(!oResponse) {
	        	oSelf.fireEvent(&quot;dataErrorEvent&quot;, {request:oRequest,
	                    callback:oCallback, caller:oCaller,
	                    message:DS.ERROR_DATANULL});
	            YAHOO.log(DS.ERROR_DATANULL, &quot;error&quot;, this.toString());

	            // Send error response back to the caller with the error flag on
	            DS.issueCallback(oCallback, [oRequest, {error:true}], true, oCaller);

	            return null;
	        }
	        // Forward to handler
	        else {
	            //oSelf.responseType = DS.TYPE_JSARRAY;
	            // Try to sniff data type if it has not been defined
	            if(oSelf.responseType === DS.TYPE_UNKNOWN) {
	                if(YAHOO.lang.isArray(oResponse)) { // array
	                	oSelf.responseType = DS.TYPE_JSARRAY;
	                }
	                 // xml
	                else if(oResponse &amp;&amp; oResponse.nodeType &amp;&amp; oResponse.nodeType == 9) {
	                	oSelf.responseType = DS.TYPE_XML;
	                }
	                else if(oResponse &amp;&amp; oRawResponse.nodeName &amp;&amp; (oResponse.nodeName.toLowerCase() == &quot;table&quot;)) { // table
	                	oSelf.responseType = DS.TYPE_HTMLTABLE;
	                }
	                else if(YAHOO.lang.isObject(oResponse)) { // json
	                	oSelf.responseType = DS.TYPE_JSON;
	                }
	                else if(YAHOO.lang.isString(oResponse)) { // text
	                	oSelf.responseType = DS.TYPE_TEXT;
	                }
	            }
	            oSelf.handleResponse(oRequest, oResponse, oCallback, oCaller, tId);
	        }
	    };

	    /**
	     * Define DWR failure handler
	     *
	     * @method _dwrFailure
	     * @param oResponse {Object} Exception object
	     * @private
	     */
	    var _dwrFailure = function(oResponse) {
	        this.fireEvent(&quot;dataErrorEvent&quot;, {request:oRequest,
	                callback:oCallback, caller:oCaller,
	                message:DS.ERROR_DATAINVALID});
	        YAHOO.log(DS.ERROR_DATAINVALID + &quot;: &quot; +
	                oResponse.statusText, &quot;error&quot;, this.toString());

	        // Send failure response back to the caller with the error flag on
	        oResponse = oResponse || {};
	        oResponse.error = true;
	        DS.issueCallback(oCallback, [oRequest,oResponse], true, oCaller);

	        return null;
	    };

	    /**
	     * Define DWR call meta data
	     *
	     * @property _dwrCallMetaData
	     * @private
	     */
	     var _dwrCallMetaData = {
	    	callback:_dwrSuccess,
	        exceptionHandler:_dwrFailure,
	        scope: this
	    };

	    // Get ready to send the request URL
	    var dwrServiceMethod = this.liveData;
	    dwrServiceMethod.call(this, oRequest, _dwrCallMetaData)

	    return tId;
	}
});
YAHOO.lang.augmentObject(javachap.util.DWRDataSource, DS);
</pre>
<p>And configure YUI AutoComplete to use the DWRDatasource. Here <strong>Userservice.getUsers</strong> is the javascript function that you want to invoke to fetch the data from the server.</p>
<pre class="brush: jscript; title: ; notranslate">
// Use an DWRDataSource
var oDS = new javachap.util.DWRDataSource(UserService.getUsers);

// Instantiate the AutoComplete
var oAC = new YAHOO.widget.AutoComplete(
    'userName', 'userNameContainer', oDS);
// we dont need '?query=sQuery', just return the sQuery instead.
oAC.generateRequest = function(sQuery) {
    return  sQuery;
};
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.javachap.com/index.php/yui-components-with-dwr-dwrdatasource/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

