Sunday, September 10, 2006

Customizing Rico AJAX to process JavaScript Response Types

The Issue

Rico Ajax currently does not know how to process xml responses of type "text/javascript". In a situation when We want to be able to send a javascript ajax response back, we totally are out of option. An example use would be is an XML Reponse that updates the state of an image (i.e., highlighted version of an image).

Consider the Ajax XML Response:
    <response type="javascript" id="myResponseId">
         function testFunction() {
            alert('Hello World');
The Workaround

Basically what we need to do is create a function to process a javascript content type. This method should do nothing more than call the javascript "eval" built-in function. The modification code is commented with "// Add This" below.

_processAjaxResponse: function( xmlResponseElements ) {
  for ( var i = 0 ; i < xmlResponseElements.length ; i++ ) {
     var responseElement = xmlResponseElements[i];

     // only process nodes of type element.....
     if ( responseElement.nodeType != 1 )

     var responseType = responseElement.getAttribute("type");
     var responseId   = responseElement.getAttribute("id");

     if ( responseType == "object" )
        this._processAjaxObjectUpdate( this.ajaxObjects[ responseId ], responseElement );
     else if ( responseType == "element" )
        this._processAjaxElementUpdate( this.ajaxElements[ responseId ], responseElement );
     // Add This
     else if ( responseType == "javascript" )
        this._processAjaxJavascript( this.ajaxElements[ responseId ], responseElement );
        alert('unrecognized AjaxResponse type : ' + responseType );

_processAjaxObjectUpdate: function( ajaxObject, responseElement ) {
    ajaxObject.ajaxUpdate( responseElement );

_processAjaxElementUpdate: function( ajaxElement, responseElement ) {
    ajaxElement.innerHTML = RicoUtil.getContentAsString(responseElement);

// Add this
_processAjaxJavascript: function( ajaxElement, responseElement ) {
    eval( RicoUtil.getContentAsString( responseElement ) );

No comments:

Popular Posts

Featured Post

Encrypting Properties File Values with Jasypt

Encrypting Properties File Values with Jasypt What's the fuzz all about? Property files are text resources in your standard web applic...