Well, for those which this might interest, I extracted some code from
Rico 1.2 (that seems to be absent in the new release -- 2.0), and
created a new Ajax object using Prototype code base. I thought it
might interest someone. I didn't open any ticket on this matter,
simply because I don't have time to provide unit testing for it
(although there's no known bugs for the moment...)

NOTE : as this code is extracted from Rico 1.2, and adapted, it is
under the Rico 1.2 license (see here for license : 
http://www.openrico.org/downloads)

-yanick


---------------------------------------------------------------------

Ajax.XMLUpdater = Class.create();

Object.extend(Ajax.XMLUpdater, {

  _elementCache: {},
  _objectCache: {},

  registerElement: function(anId,anElement) {
    if ( !anElement ) {
      anElement = $(anId);

      if ( !anElement )
        throw Exception("Ajax.XMLUpdater: element not found : " +
anId );
    }

    return Ajax.XMLUpdater._elementCache[anId] = anElement;
  },

  registerObject: function(anId,anObject) {
    if ( typeof anObject.ajaxUpdate != 'function' )
      throw Exception("Ajax.XMLUpdater: object does not have an
ajaxUpdate function : " + anId );

    return Ajax.XMLUpdater._objectCache[anId] = anObject;
  },

  getElementFromCache: function(anId) {
    var element = Ajax.XMLUpdater._elementCache[anId];
    if ( element )
      return element;
    else {
      element = $(anId);
      if ( element ) Ajax.XMLUpdater._elementCache[anId] = element;
      return element;
    }
  },

  getObjectFromCache: function(anId) {
    return Ajax.XMLUpdater._objectCache[anId] || null;
  }

} );

Object.extend(Object.extend(Ajax.XMLUpdater.prototype,
Ajax.Request.prototype), {
  initialize: function(url, options) {
    this.transport = Ajax.getTransport();
    this.setOptions(options);

    var onComplete = this.options.onComplete ||
Prototype.emptyFunction;
    this.options.onComplete = this._onRequestComplete.bind(this);
    this.request(url);
  },

  _onRequestComplete: function() {
    if ( this.success() ) {

      if ( this.transport.responseXML ) {

        var response =
this.transport.responseXML.getElementsByTagName("ajax-response");

        if (response == null || response.length != 1)
           return;

        this._processResponse( response[0].childNodes );
      }

      if (this.onComplete)
        setTimeout(this.onComplete.bind(this), 10);
    }
  },

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

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

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

       if ( responseType == "object" )
 
this._processObjectUpdate( Ajax.XMLUpdater.getObjectFromCache(responseId),
responseElement );
       else if ( responseType == "element" )
 
this._processElementUpdate( Ajax.XMLUpdater.getElementFromCache(responseId),
responseElement );
       else
         alert('Ajax.XMLUpdate: unrecognized response type : ' +
responseType );
    }
  },

  _processElementUpdate: function(anElement, response) {
    if ( anElement ) {
      var insertMode = response.getAttribute("insertion");
      var html = RicoUtil.getContentAsString(response);

      if ( insertMode )
        new Insertion[insertMode](anElement, html);
      else
        Element.update(anElement, html);
    }
  },

  _processObjectUpdate: function(anObject, response) {
    //alert( " Object Update " + anObject );

    if ( anObject ) {
      anObject.ajaxUpdate(response);
    }
  },

});

--------------------------------------------------------

Usage :

Ajax.XMLUpdater.registerObject( "objectId",
objectWithAnAjaxUpdateFunction );
...
new Ajax.XMLUpdater( url, options );


Parameters are the same as the Ajax.Request object consctructor.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to