bluescreen303-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Nov-16 07:25 UTC
getting notified of element removal
I bought the excellent book "Prototype and script.aculo.us" which was released when prototype 1.6 came out. One paragraph came to my attention: ===========================================Prototype will soon bundle a series of built-in custom events to make several lifecycle maintenance tasks easier (for example, react to DOM fragments being updated or removed, react to drag and drops in better ways, and so on). So far, you can still use this facility for adding your own events to DOM elements. =========================================== I''ve been looking for this functionality for some time and I''ve resorted to some hacky workarounds that I''dd like to get rid of. At the moment I have a lot of objects in memory which are linked to an element (this.element). If those objects are presented in one or more lists, the list itself is also an object where this.element is the list itself. Also all elements belonging to the list are now added and removed through list.addItem and list.removeItem, so they get stored in an array in the list (list._items). Also the items that get added to lists get a ''containedIn'' attribute that links back to the list. This feels very very redundant, as the list and the item objects don''t do very much by themselves. They are just there for easy navigation and manipulation. For example: If I want to remove a list, the object takes care of destroying all item objects, which then take care of removing their element. After this, the list itself removes its element. I do it like this, because I need to track a few other things about those items. Since the items represent some data (fetched through jester atm), and this data can be presented on multiple elements at the same time, I need to make sure the data/jester object keeps track of all elements that cary a representation of its data. So removing an element should trigger a removal from the data object''s ''visuals'' array. As you can see... this gets very very tangled in every bit of the code, so I would really like the behavior described in the book. Especially if a bunch of elements get removed because their containing element updates itself or gets removed. That way I don''t need to track deletion om many many levels with manual propagation to the contained elements. Does anyone have a pointer to a better way to do this? Maybe some pre-alpha code on top of prototype that implements custom event firing when DOM elements get manipulated? It would be very helpful. thanks, Mathijs --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Well, you could wrap the "interesting" Element.Methods and fire events there. Something like (untested): Element.addMethods({ remove: Element.Methods.remove.wrap (function(proceed) { this.fire("element:removed"); proceed(); }) }); Might do the trick. More about Elemet#wrap in http://thinkweb2.com/projects/prototype/2007/09/14/wrap-it-up/ Best, -Nicolas On Nov 16, 2007 5:25 AM, bluescreen303-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <bluescreen303-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I bought the excellent book "Prototype and script.aculo.us" which was > released when prototype 1.6 came out. > One paragraph came to my attention: > > ===========================================> Prototype will soon bundle a series of built-in custom events to make > several lifecycle maintenance tasks easier (for example, react to DOM > fragments being updated or removed, react to drag and drops in better > ways, and so on). So far, you can still use this facility for adding > your > own events to DOM elements. > ===========================================> > I''ve been looking for this functionality for some time and I''ve > resorted to some hacky workarounds that I''dd like to get rid of. > > At the moment I have a lot of objects in memory which are linked to an > element (this.element). > If those objects are presented in one or more lists, the list itself > is also an object where this.element is the list itself. > Also all elements belonging to the list are now added and removed > through list.addItem and list.removeItem, so they get stored in an > array in the list (list._items). Also the items that get added to > lists get a ''containedIn'' attribute that links back to the list. > > This feels very very redundant, as the list and the item objects don''t > do very much by themselves. They are just there for easy navigation > and manipulation. > > For example: > If I want to remove a list, the object takes care of destroying all > item objects, which then take care of removing their element. After > this, the list itself removes its element. > > I do it like this, because I need to track a few other things about > those items. > Since the items represent some data (fetched through jester atm), and > this data can be presented on multiple elements at the same time, I > need to make sure the data/jester object keeps track of all elements > that cary a representation of its data. So removing an element should > trigger a removal from the data object''s ''visuals'' array. > > As you can see... this gets very very tangled in every bit of the > code, so I would really like the behavior described in the book. > Especially if a bunch of elements get removed because their containing > element updates itself or gets removed. That way I don''t need to track > deletion om many many levels with manual propagation to the contained > elements. > > Does anyone have a pointer to a better way to do this? > Maybe some pre-alpha code on top of prototype that implements custom > event firing when DOM elements get manipulated? > > It would be very helpful. > > thanks, > Mathijs > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---