Todd is exactly right. What's also important to realize about this, is that 
when your DOM elements are destroyed by the update to innerHTML, any event 
handlers that were tied to them (onclick, etc..), are in danger of staying 
around in memory (especially if the handlers are closures which hold references 
to the DOM elements, called a circular reference). These memory leaks can be 
very detrimental to your application if they go unchecked.

Think of the control you're updating (the treeview) as a miniature page. Every 
time it's refreshed (via innerHTML), you need to re-setup all of it's client 
behaviors (which is what you're seeing now). But you also should take care to 
clean up all of it's resources before each update. You can do this by adding a 
dispose() method on your objects that detaches DOM event handlers, unregisters 
draggables and droppable... etc (basically any memory that the DOM elements 
which will be refreshed have a hold of needs to be released), and making sure 
dispose() gets called before the refresh happens.


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Todd Ross
Sent: Thursday, February 23, 2006 5:49 AM
To: rails-spinoffs@lists.rubyonrails.org
Subject: Re: [Rails-spinoffs] reloading fragments of pages

On 2/23/06, Tarek Ziadé <[EMAIL PROTECTED]> wrote:
>  When the dom is reloaded, the drag/drop features reloads Droppable.drops
> elements.

I think this statement is the heart of your problem.  There's nothing
active about scriptaculous (where you're getting the
Draggables/Droppables from) that would check for a DOM "reload" (I'm
not sure what that is?  any change, perhaps?); I'm not even sure that
kind of information is available to JavaScript.

I don't know much about scriptaculous, so don't take this as the final
answer, but the magic that makes Draggables/Droppables work are the
events that are attached to each element that you make
Draggable/Droppable.  When you use .innerHTML on those elements'
parent, you're effectively destroying the existing elements (and their
event associations) and creating what might look like identical
elements, but they're not; they don't have the events unless you
programatically re-initialize the Draggables/Droppables.

>  refreshPortletCompleted: function(originalRequest) {
>  // getting new positions from the server
>  result = originalRequest.responseText;
>
>  if (result!='') {
>  $(this.last_portlet_id).innerHTML = result;
>  var newdiv = document.createElement("div");
>  newdiv.innerHTML = result;
>  newdiv.id = this.last_portlet_id;
>  olddiv = $(this.last_portlet_id);
>  parent = olddiv.parentNode;
>  olddiv.parentNode.replaceChild(newdiv, olddiv);
>  }
>  },

Isn't most of this code redundant?  You only need to use innerHTML on
the existing div, or create a new div and replace the old one.  Either
one achieves the same DOM structure in the end.  You also never use
'parent' anywhere.

Todd
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

The information transmitted in this electronic mail is intended only for the
person or entity to which it is addressed and may contain confidential,
proprietary, and/or privileged material.  Any review, retransmission, 
dissemination or other use of, or taking of any action in reliance upon,
this information by persons or entities other than the intended recipient
is prohibited. If you received this in error, please contact the sender and
delete the material from all computers.

_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to