Matt a écrit :
> I'm having some difficulty with the periodical executer, I use it
> within one of my objects and when I use 'this' it doesn't actually
> reference the object. Here is the example:

Binding 101, Matt.  Change your initialize line:

> new PeriodicalExecuter(this.pollRadio, 2);

To this:

> new PeriodicalExecuter(this.pollRadio.bind(this), 2);

BTW, you may probably want to store that PE ref somewhere in case you
need to stop the PE from running at some point.

> However I have no idea with the reference to 'this' isn't the right
> one and there's probably a better way. Any idea? If there's no better

Look at http://prototypejs.org/api/function/bind or grab some good JS
book (or mine, for that matter) and read on "this", execution contexts,
binding, etc.

Essentially, the trick is: whenever you pass a method reference around,
it loses its "this", okay?  The only way for a method to be properly
bound to an instance is to call it directly on the instance (e.g.
"this.method(a, b)" or "someObj.method(a, b)".  Anything else will lose
the binding.

Prototype provides a bind method for functions that spews out an
anonymous function (not associated to your original object in itself, so
you can safely pass it around), and this new function essentially makes
sure to call your original method on its proper instance.

-- 
Christophe Porteneuve a.k.a. TDD
"[They] did not know it was impossible, so they did it." --Mark Twain
Email: [EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to