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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---