Instant methods and stopObserve

16 views
Skip to first unread message

kstubs

unread,
Oct 2, 2011, 7:29:30 AM10/2/11
to prototype-s...@googlegroups.com
Here is a pattern I am using more frequently:

var some_function = function() { ... }
some_function();

Consider:
a_function: function() {
  // my instance method
  var some_function = function()  {
     // only need to do this once
     Event.stopObserve(document, 'some:custom_dom_event', some_function);
     // here we do something ...
   };

  Event.bindAsEventListener(document, 'some:custom_dom_event', this);  
  SomeComponent.CallSomeFunction();  // in turn will fire the event 'some:custom_dom_event'
}

Problem:
My instance event handler is not letting go.  Is this because I am inside the instance method calling stopObserve?  Or should this work?  Based on the pattern above I really don't see another anywhere else to tear down the handler.

Thanks,
Karl..

kstubs

unread,
Oct 2, 2011, 7:44:13 AM10/2/11
to prototype-s...@googlegroups.com
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));  

T.J. Crowder

unread,
Oct 4, 2011, 4:43:59 AM10/4/11
to Prototype & script.aculo.us
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

kstubs

unread,
Oct 6, 2011, 1:30:54 PM10/6/11
to prototype-s...@googlegroups.com
T.J.

Had to think outside the box on that one, I didn't know you could first:  create the event listener event, then second: pass it in as the method argument for observe.  That's a good one!

Thanks for the help on this.
Karl..
Reply all
Reply to author
Forward
0 new messages