Hi!
In my opinion prototype.js is a good library. With it I learn more javascript than in my whole life but I need to create another class to work like I prefer

I would like to have a Ajax class that replace instead of update a container

Why?

I love something like this:

<div id='List1' class='List'>
   <div id='Element1' class='Element'>
       Element1
<a href="javascript: Ajax.Replacer('Element1', 'http://myserver/Edit/Element1', options)">Edit</a>
   </div>
   <div id='Element2' class='Element'>
       Element2
<a href="javascript: Ajax.Replacer('Element2', 'http://myserver/Edit/Element2', options)">Edit</a>
   </div>
   <div id='Element3' class='Element'>
       Element3
<a href="javascript: Ajax.Replacer('Element3', 'http://myserver/Edit/Element3', options)">Edit</a>
   </div>
</div>

I would like to maintain the right structure :
List1
   Element1
   Element2
   Element3

But I would like to change the Element1 div complete because I don't know what the server give to me

For these I create Ajax.Replacer a class that replace the container gived complete

Now I don't want that Sam or Thomas or anyone include these class at prototype library (I only hope so) but I would like to know if I create the class correctly or there are another better way to write it

Thanks!

PD: I have Ajax.Adder too to add content to a web (not update its content). If you think you need it search Ajax.Replacer at my weblog (http://blogs.sistes.net/Garito)

Ajax.Replacer

Ajax.Replacer = Class.create();

Object.extend(Object.extend(Ajax.Replacer.prototype, Ajax.Request.prototype), {
 initialize: function(container, url, options) {
   this.containers = {
     success: container.success ? $(container.success) : $(container),
     failure: container.failure ? $(container.failure) :
       (container.success ? null : $(container))
   }

   this.transport = Ajax.getTransport();
   this.setOptions(options);

   var onComplete = this.options.onComplete || Prototype.emptyFunction;
   this.options.onComplete = (function(transport, object) {
     this.replaceContent();
     onComplete(transport, object);
   }).bind(this);

   this.request(url);
 },

 replaceContent: function() {
   var receiver = this.responseIsSuccess() ?
     this.containers.success : this.containers.failure;
   var response = this.transport.responseText;
if (!this.options.evalScripts)
     response = response.stripScripts();

   if (receiver) {
     if (this.options.insertion) {
       new this.options.insertion(receiver, response);
     } else {
               var parent = receiver.parentNode
               div = parent.appendChild(document.createElement('div'))
               Element.update(div, response)
               for(i = 0; div.childNodes.length; i++)
               {
if(div.childNodes[i].nodeName != '#text' && div.childNodes[i].nodeName != 'META') { break; }
               }
               parent.replaceChild(div.childNodes[i], receiver)
               parent.removeChild(div)
               setTimeout(function() {response.evalScripts()}, 10)
     }
   }

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

--
Mis Cosas
http://blogs.sistes.net/Garito/


_______________________________________________
Rails-spinoffs mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to