Hi,
Two things:
1. It's `stopObserving`[1], not `stopObserve`. So that's kind of going
to be an issue right there. ;-) The error should be showing up in the
error console.
2. If you pass a function reference into `stopObserving` in order to
tell it to stop using that particular function, it has to be the
*same* function you gave `observe`. But that's not what you're doing.
`bindAsEventListener` returns a *new* function that will, when called,
call your original function with the `this` value and arguments you
give (and the event argument). So you have to give that same function
reference to `stopObserving`:
a_function: function() {
// my instance method
var some_bound_function = (function() {
// only need to do this once
Event.stopObserve(document, 'some:custom_dom_event',
some_bound_function);
// here we do something ...
}).bindAsEventListener(this);
Event.observe(document, 'some:custom_dom_event',
some_bound_function);
SomeComponent.CallSomeFunction(); // in turn will fire the event
'some:custom_dom_event'
}
Or what I'd more likely do, because I don't like anonymous
functions[2]:
a_function: function() {
// my instance method
var some_bound_function = some_function.bindAsEventListener(this);
Event.observe(document, 'some:custom_dom_event',
some_bound_function);
SomeComponent.CallSomeFunction(); // in turn will fire the event
'some:custom_dom_event'
function some_function() {
// only need to do this once
Event.stopObserve(document, 'some:custom_dom_event',
some_bound_function);
// here we do something ...
}
}
(Actually I'd take it a step further so that the function you're
assigning to `a_function` isn't anonymous either, but that's off-
topic.)
[1] http://api.prototypejs.org/dom/Event/stopObserving/
[2] http://blog.niftysnippets.org/2010/03/anonymouses-anonymous.html
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com
On Oct 2, 12:44 pm, kstubs wrote:
> Subject line correction: Instance methods and stopObserve
>
> Consider correction:
> Event.bindAsEventListener(document, 'some:custom_dom_event', this);
>
> Should be:
> Event.observe(document, 'some:custom_dom_event',
> some_function.bindAsEventListener(this));
--
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com.
To unsubscribe from this group, send email to
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en.