Hi, 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: Dubya.RadioHandler = Class.create(); Object.extend(Dubya.RadioHandler.prototype, { initialize: function(element) { element = $(element); this.element = element; this.songCount = 0; this.lastResponse = ''''; new PeriodicalExecuter(this.pollRadio, 2); }, pollRadio: function(executer) { caller = this; new Ajax.Request(''/radio/current'', { method: ''GET'', onSuccess: function(transport) { response = transport.responseText.strip(); if (!response.empty() && response != caller.lastResponse) { caller.lastResponse = response; caller.addSong(response); } } }); }, addSong: function(title) { new Insertion.Top(''active_radio'', ''<p id=\"'' + this.songCount++ + ''\">'' + title + ''</p>''); } }); In the ''pollRadio'' method, ''this'' isn''t Dubya.RadioHandler, so the addSong method isn''t available (it''s undefined). I worked it around by passing a reference to this in the PeriodicalExecuter constructor and passing it back in the callback. 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 way, maybe passing ''this'' to the PeriodicalExecuter constructor should be the default behavior? Thanks, Matthieu --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 betterLook 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: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Matt ...in addition to utilizing Prototype''s bind in the manner Chris described ... you might also want to reference/read up on ''closures<http://www.jibbering.com/faq/faq_notes/closures.html>'' in JavaScript<http://www.amazon.com/JavaScript-Definitive-Guide-David-Flanagan/dp/0596101996/ref=pd_bbs_sr_1/002-9021338-4100056?ie=UTF8&s=books&qid=1184745874&sr=8-1>, as JS closures are related to Prototype''s bind. cheers- On 7/18/07, Christophe Porteneuve <tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> > > 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: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I''m probably developing too late, I should have thought of the missing binding. But thanks a lot for the nice explanations, I was missing the theoretical part here (I''m just a lazy reader). Cheers, Matthieu On Jul 18, 1:08 am, "Mark Holton" <holto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Matt ...in addition to utilizing Prototype''s bind in the manner Chris > described ... you might also want to reference/read up on > ''closures<http://www.jibbering.com/faq/faq_notes/closures.html>'' > in JavaScript<http://www.amazon.com/JavaScript-Definitive-Guide-David-Flanagan/dp/0...>, > as JS closures are related to Prototype''s bind. > > cheers- > > On 7/18/07, Christophe Porteneuve <t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote: > > > > > 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 athttp://prototypejs.org/api/function/bindor 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: t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---