seam + YUI
holly_bony May 4, 2010 4:52 PMSometimes you need integrate Seam remote with other java script frameworks like YUI so the only problem can rise when we using components that makes request internally (autocomplete, datatable, etc). The right way and the best simple is through of a datasource. I share my datasource:
var DS = YAHOO.util.DataSourceBase;
YAHOO.widget.SeamDataSource = function(oLiveData, oConfigs) {
this.dataType = DS.TYPE_XHR;
oLiveData = oLiveData || "";
YAHOO.widget.SeamDataSource.superclass.constructor.call(this, oLiveData, oConfigs);
};
YAHOO.lang.extend(YAHOO.widget.SeamDataSource, 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 tId = DS._nTransactionId++;
this.fireEvent("requestEvent", {tId:tId,request:oRequest,callback:oCallback,caller:oCaller});
var oSelf = this;
/**
* Define seam success handler
*
* @method _seamSuccess
* @param oResponse {Object} seam Response
* @private
*/
var _seamSuccess = function(oResponse) {
// If response ID does not match last made request ID,
// silently fail and wait for the next response
if(oResponse && (oSelf.connXhrMode == "ignoreStaleResponses") &&
(oResponse.tId != oQueue.conn.tId)) {
YAHOO.log("Ignored stale response", "warn", oSelf.toString());
return null;
}
// Error if no response
else if(!oResponse) {
this.fireEvent("dataErrorEvent", {request:oRequest, response:null,
callback:oCallback, caller:oCaller,
message:DS.ERROR_DATANULL});
// 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 {
// Try to sniff data type if it has not been defined
if(oSelf.responseType === DS.TYPE_UNKNOWN) {
var ctype = (oResponse.getResponseHeader) ? oResponse.getResponseHeader["Content-Type"] : null;
if(ctype) {
// xml
if(ctype.indexOf("text/xml") > -1) {
oSelf.responseType = DS.TYPE_XML;
}
else if(ctype.indexOf("application/json") > -1) { // json
oSelf.responseType = DS.TYPE_JSON;
}
else if(ctype.indexOf("text/plain") > -1) { // text
oSelf.responseType = DS.TYPE_TEXT;
}
}
}
oSelf.handleResponse(oRequest, oResponse, oCallback, oCaller, tId);
}
};
// Get ready to send the request URL
var webMethod = this.liveData.webRemote;
webMethod.call(this.liveData.scope, oRequest, _seamSuccess);
//webMethod(oRequest, _seamSuccess);
//manager.filterItems( oRequest, _seamSuccess);
return tId;
}
});
YAHOO.lang.augmentObject(YAHOO.widget.SeamDataSource, DS);
/*Using with the datatable*/
var manager = Seam.Component.getInstance("manager");
var filterTable = new YAHOO.widget.DataTable(
'filterContainer',
[
{key:'id',label:'Id'},
{key:'name',label:'Name'},
{key:'price',label:'Price'}
],
//new YAHOO.widget.SeamDataSource(manager.filterItems)
new YAHOO.widget.SeamDataSource(
{
webRemote:manager.filterItems,
scope:manager
}
,{
responseType:YAHOO.util.DataSource.TYPE_JSARRAY,
responseSchema: {
fields:['id','name','price']
}
})
,{initialRequest:"hola2"});
var DS = YAHOO.util.DataSourceBase;
YAHOO.widget.SeamDataSource = function(oLiveData, oConfigs) {
this.dataType = DS.TYPE_XHR;
oLiveData = oLiveData || "";
YAHOO.widget.SeamDataSource.superclass.constructor.call(this, oLiveData, oConfigs);
};
YAHOO.lang.extend(YAHOO.widget.SeamDataSource, 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 tId = DS._nTransactionId++;
this.fireEvent("requestEvent", {tId:tId,request:oRequest,callback:oCallback,caller:oCaller});
var oSelf = this;
/**
* Define seam success handler
*
* @method _seamSuccess
* @param oResponse {Object} seam Response
* @private
*/
var _seamSuccess = function(oResponse) {
// If response ID does not match last made request ID,
// silently fail and wait for the next response
if(oResponse && (oSelf.connXhrMode == "ignoreStaleResponses") &&
(oResponse.tId != oQueue.conn.tId)) {
YAHOO.log("Ignored stale response", "warn", oSelf.toString());
return null;
}
// Error if no response
else if(!oResponse) {
this.fireEvent("dataErrorEvent", {request:oRequest, response:null,
callback:oCallback, caller:oCaller,
message:DS.ERROR_DATANULL});
// 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 {
// Try to sniff data type if it has not been defined
if(oSelf.responseType === DS.TYPE_UNKNOWN) {
var ctype = (oResponse.getResponseHeader) ? oResponse.getResponseHeader["Content-Type"] : null;
if(ctype) {
// xml
if(ctype.indexOf("text/xml") > -1) {
oSelf.responseType = DS.TYPE_XML;
}
else if(ctype.indexOf("application/json") > -1) { // json
oSelf.responseType = DS.TYPE_JSON;
}
else if(ctype.indexOf("text/plain") > -1) { // text
oSelf.responseType = DS.TYPE_TEXT;
}
}
}
oSelf.handleResponse(oRequest, oResponse, oCallback, oCaller, tId);
}
};
// Get ready to send the request URL
var webMethod = this.liveData.webRemote;
webMethod.call(this.liveData.scope, oRequest, _seamSuccess);
//webMethod(oRequest, _seamSuccess);
//manager.filterItems( oRequest, _seamSuccess);
return tId;
}
});
YAHOO.lang.augmentObject(YAHOO.widget.SeamDataSource, DS);
/*Using with the datatable*/
var manager = Seam.Component.getInstance("manager");
var filterTable = new YAHOO.widget.DataTable(
'filterContainer',
[
{key:'id',label:'Id'},
{key:'name',label:'Name'},
{key:'price',label:'Price'}
],
//new YAHOO.widget.SeamDataSource(manager.filterItems)
new YAHOO.widget.SeamDataSource(
{
webRemote:manager.filterItems,
scope:manager
}
,{
responseType:YAHOO.util.DataSource.TYPE_JSARRAY,
responseSchema: {
fields:['id','name','price']
}
})
,{initialRequest:"hola2"});