maybe using the anonymous function was a bad example. my point is just the Event.observers array. it can get quite big if you have a ton of observers. let say you have a hundred elements each in a partial and each element has an observer for mouseover, mouseout and click a total of 300 observers
if i re-render that partial instead of three hundred, I now have 600 because it doesn't clear the old elements from the array. and if the user lets say re-renders the partial 20 times I now have 6000 objects in array. not to say that there is a performance issue because the events attach, detach regardless if there in the array or not. I'm just saying it should do a little house keeping on the array. :) cause I'm righting some test I would like to call some function assert it has added 2 observers to the array call remove and assert it has 0 observers in the array or maybe I'm too specific with my event listener. On Dec 14, 1:16 pm, "Martin Bialasinski" <[EMAIL PROTECTED]> wrote: > On 12/14/06, heidmotron <[EMAIL PROTECTED]> wrote: > > > script is: > > Event.observe('test', 'click', function(){ alert('I am so happy') }) > > > Your Event.observers.length is 1 > > > then you want to remove test, so before you actually remove it from the > > dom you are kind enough to say, > > "hey, test div, stop observing that click and go have a beer" > > > Event.stopObserving('test', 'click', function(){ alert('I am so happy') > > }) > > > You would think that observer would have beer and get out of the > > Event.observers array so length would equal Zero.The observers array is not > > the problem. You want the browser to stop > executing the function on click, and stopObserving does work this way. > The problem with your code is, that you say "react to click by > executing anonymous function1" and then say "do not longer react to > click by executing anonymous function2". Your functions look the same, > but they are not the same objects. So the browser can't remove the > event listener. > > Try this in firebug: > (function(){ foo }) === (function(){ foo }) > > The result is "false". > > You need to save the function on observe and pass it on stopObserving. > > var mycallback = function(){ alert('I am so happy') }; > // function(ev){ alert('I am so happy') }.bindAsEventListener(); to > get the event object > > Event.observe('test', 'click', mycallback); > Event.stopObserving('test', 'click', mycallback); > > It will not remove the entry from the observers array, but it will > remove the event listener, and this is what counts. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---