Hi, I''m trying to use Ajax.updater within a function which I''ll be calling on a variety of different elements. The element is ''this'' within the function, so I can specify the first argument of Ajax.updater using this.identify() like this: new Ajax.Updater(this.identify(), locationUrl, { method: ''get'', onSuccess: function () { $(''myElement'').enable(); } }); ...but then how do I refer to ''this'' within my onSuccess callback function? In the above I''m having to specify it explicitly using $ (''myElement''), but that''s not going to be any use if I want to call my function on *any* element: onSuccess: function () { this.enable(); // doesn''t work, because ''this'' isn''t ''myElement'' inside callback func } Is there something clever I can do with bindings or something here? Jonny --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> Is there something clever I can do with bindings or something here?Yup! :-) * * * * onSuccess: this.enable.bind(this); * * * * Details: http://www.prototypejs.org/api/function/bind http://www.prototypejs.org/api/function/bindAsEventListener More on this issue in general (disclosure - it''s my own blog): http://blog.niftysnippets.org/2008/04/you-must-remember-this.html Hope this helps, -- T.J. Crowder tj / crowder software / com On Apr 3, 11:04 am, Jonny Nott <jonn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I''m trying to use Ajax.updater within a function which I''ll be calling > on a variety of different elements. The element is ''this'' within the > function, so I can specify the first argument of Ajax.updater using > this.identify() like this: > > new Ajax.Updater(this.identify(), locationUrl, { > method: ''get'', > onSuccess: function () { > $(''myElement'').enable(); > } > > }); > > ...but then how do I refer to ''this'' within my onSuccess callback > function? In the above I''m having to specify it explicitly using $ > (''myElement''), but that''s not going to be any use if I want to call my > function on *any* element: > > onSuccess: function () { > this.enable(); // doesn''t work, because ''this'' isn''t ''myElement'' > inside callback func > > } > > Is there something clever I can do with bindings or something here? > > Jonny--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
From your private reply to me (you probably wanted to reply to the group address, not mine):> I''m guessing I can chain as many different methods for ''this'' as I > want in that line, as long as I end it with the bind(this), yeah?I''m not sure I know what you mean by that. But for example, if you wanted to do three things with the element -- say, enabling it, setting its opacity to 50%, and apply the ''blarg'' style to it -- you could do this: onSuccess: (function() { this.enable(); this.setOpacity(0.5); this.setStyle(''blarg''); }).bind(this) That defines a new anonymous function, then binds ''this'' to it (so that ''this'' within the function will be the reference you expect). Note that parens around the function definition, you need them. Also note that we''re only *defining* the function, not calling it; it''ll get called by the success event. -- T.J. Crowder tj / crowder software / com On Apr 3, 11:41 am, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Is there something clever I can do with bindings or something here? > > Yup! :-) > > * * * * > onSuccess: this.enable.bind(this); > * * * * > > Details:http://www.prototypejs.org/api/function/bindhttp://www.prototypejs.org/api/function/bindAsEventListener > > More on this issue in general (disclosure - it''s my own blog):http://blog.niftysnippets.org/2008/04/you-must-remember-this.html > > Hope this helps, > -- > T.J. Crowder > tj / crowder software / com > > On Apr 3, 11:04 am, Jonny Nott <jonn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > I''m trying to use Ajax.updater within a function which I''ll be calling > > on a variety of different elements. The element is ''this'' within the > > function, so I can specify the first argument of Ajax.updater using > > this.identify() like this: > > > new Ajax.Updater(this.identify(), locationUrl, { > > method: ''get'', > > onSuccess: function () { > > $(''myElement'').enable(); > > } > > > }); > > > ...but then how do I refer to ''this'' within my onSuccess callback > > function? In the above I''m having to specify it explicitly using $ > > (''myElement''), but that''s not going to be any use if I want to call my > > function on *any* element: > > > onSuccess: function () { > > this.enable(); // doesn''t work, because ''this'' isn''t ''myElement'' > > inside callback func > > > } > > > Is there something clever I can do with bindings or something here? > > > Jonny--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That''s again very helpful to know! Can you tell I''m new to prototype? ;) Jonny On Apr 3, 3:43 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m not sure I know what you mean by that. But for example, if you > wanted to do three things with the element -- say, enabling it, > setting its opacity to 50%, and apply the ''blarg'' style to it -- you > could do this: > > onSuccess: (function() { > this.enable(); > this.setOpacity(0.5); > this.setStyle(''blarg''); > > }).bind(this)--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
P.S. I guess I was asking if it could be done like this: onSuccess: this.enable().setOpacity(0.5).setStyle(''blarg'').bind(this) ..from your last reply, I''m guessing not! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, On Apr 3, 3:59 pm, Jonny Nott <jonn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> P.S. I guess I was asking if it could be done like this: > > onSuccess: this.enable().setOpacity(0.5).setStyle(''blarg'').bind(this) > > ..from your last reply, I''m guessing not!No, and let''s examine why. This expression: this.enable returns a reference to the ''enable'' function (specifically, it returns a reference to the function referenced by the ''enable'' property of the ''this'' object). It doesn''t execute the function, it merely returns a reference to the function''s instance (the object representing the function). This expression: this.enable() ...gets the function reference as above and then *executes* the function; the expression takes the value of the result of the function call. These are two completely different things. So by extension, this line of code: this.enable.bind() Looks up the function referenced by the property ''enable'' on the ''this'' object and then calls its ''bind'' function (the one on the function object) and returns the result. It does not execute the ''enable'' function, it executes ''bind''. Whereas this: this.enable().bind() ...executes ''enable'' and then tries to execute ''bind'' on whatever the result of ''enable'' is (probably an element, so it''ll fail). So looking at your line of code:> this.enable().setOpacity(0.5).setStyle(''blarg'').bind(this)...it breaks down like this: result1 = this.enable(); result2 = result1.setOpacity(0.5); result3 = result2.setStyle(''blarg''); result4 = result3.bind(this); So in other words, enable(), setOpacity(), setStyle(), and bind() are all executed immediately. We don''t want to call them immediately, we want to call them when the event fires; and in any case, the bind() call will fail, because the return value of setStyle() isn''t a function, it''s an element. bind() is for functions. Hope this helps, -- T.J. Crowder tj / crowder software / com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Again, very revelatory! I think you are to Javascript OO what I am to guitar tuition ;) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---