[Proto-Scripty] Re: Instance methods and stopObserve

2011-10-06 Thread kstubs
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..

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/prototype-scriptaculous/-/6MlBXMqxNtcJ.
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.



[Proto-Scripty] Re: Instance methods and stopObserve

2011-10-04 Thread T.J. Crowder
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.