Sebastian Sastre
2008-Mar-22 19:33 UTC
Replace existing handlers for the same element+event pair or not?
Hi there, I need some help to undestand what is goin on with this event handler and if I need or not the feature of replace existing hadlers when observing an event. From Prototype docs in Element.observe(): "...It does not replace existing handlers for that same element+event pair. It adds to the list of handlers for that pair. Using observe will never incapacitate earlier calls. ..." That is a good thing but... I have a component (read div element with some sub elements) which observes onKeyDown when is loaded. The element initializer registers as handler to call a just previously installed function (of that very element). As reaction of a key down event the handler executes an updater on the element (executes an updater on itself). As the updated div has the same #id, I was expecting the observation to remain valid after the updater do its job but, when user press a second time a key on it, it isn''t reacting with the handler (is not calling again that updater). So I''ve interpreted that observations are not valid after and updater execution so I''ve used the onComplete handler of the updater to make a re-initialization of that DOM element after the updates. That way, it registers the event observation again for the same element +event pair with the same handler. Doing that the result was I have an always incremental quantity of updater requests reacting to the key down (shoot in the foot). A kind of lazy solution will be to have a way to query an element if it is already observing an event with some provided handler and make an observation only if it is not observing that already. That way onComplete can be used to always initialize and register the observations without worry of previous observations of that event +handler pair. A more elegant solution is to observe just once like I did in first place but I''m unable to figure out why isn''t working after the fisrt updater. Updaters brings a new element instance and deletes the original object? if so (and besides the incremental thing) why using onComplete to initialize again the element after the updater was making 2 updater requests instead of one after the first updater? Any clue? thanks, Sebastian --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Walter Lee Davis
2008-Mar-22 19:57 UTC
Re: Replace existing handlers for the same element+event pair or not?
You could try observing an object higher up the chain that you don''t replace. If you are observing a keydown event, that''s global to the window, right? Walter On Mar 22, 2008, at 3:33 PM, Sebastian Sastre wrote:> > Hi there, > > [snip] > As reaction of a key down event the handler executes an updater on > the element (executes an updater on itself). As the updated div has > the same #id, I was expecting the observation to remain valid after > the updater do its job but, when user press a second time a key on it, > it isn''t reacting with the handler (is not calling again that > updater). > > So I''ve interpreted that observations are not valid after and > updater execution so I''ve used the onComplete handler of the updater > to make a re-initialization of that DOM element after the updates. > That way, it registers the event observation again for the same > element > +event pair with the same handler. > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sebastian Sastre
2008-Mar-23 18:05 UTC
Re: Replace existing handlers for the same element+event pair or not?
Hi Walter, making aside the problem of misplaced responsabilities in parent components, I''ve tested what you propose. For the record: the result was exactly the same. So after that I was searching a way to extend prototype''s objects so I can observe an event only if not previously observed (making another observation method with this feature). In the middle of that (failed) process I''ve realized that making with every updater a #stopObserving and #observe again, the event should be hooked only once. And it did it. I don''t know what is going on there but for now I''ve solved my problem. My guess is about some trouble between the updated instance and the observed event for it. thanks anyway, Sebastian On Mar 22, 4:57 pm, Walter Lee Davis <wa...-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> You could try observing an object higher up the chain that you don''t > replace. If you are observing a keydown event, that''s global to the > window, right? > > Walter > > On Mar 22, 2008, at 3:33 PM, Sebastian Sastre wrote: > > > > > Hi there, > > > [snip] > > As reaction of a key down event the handler executes an updater on > > the element (executes an updater on itself). As the updated div has > > the same #id, I was expecting the observation to remain valid after > > the updater do its job but, when user press a second time a key on it, > > it isn''t reacting with the handler (is not calling again that > > updater). > > > So I''ve interpreted that observations are not valid after and > > updater execution so I''ve used the onComplete handler of the updater > > to make a re-initialization of that DOM element after the updates. > > That way, it registers the event observation again for the same > > element > > +event pair with the same handler.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---