I''m using the prototype Ajax.Request with an anonymous functions inside of a javascript object. However, once I get into the anonymous function of the Ajax.Request onSuccess, I loose the bindng to my object and this now refers to window instead of my object. How can I maintain binding from within the anonymous function? Code snippet: CampaignView.prototype.DataBind=function(event) { var c=this; //binding is still good - refers to CampaignView object new Ajax.Request(''url.aspx'', { method: ''post'', onSuccess: function(resp) { var d=this; //binding is lost - refers to window } }); } --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On May 30, 5:52 pm, eggie5 <egg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m using the prototype Ajax.Request with an anonymous functions > inside of a javascript object. However, once I get into the anonymous > function of the Ajax.Request onSuccess, I loose the bindng to my > object and this now refers to window instead of my object. > > How can I maintain binding from within the anonymous function? > > Code snippet: > > CampaignView.prototype.DataBind=function(event) > { > var c=this; //binding is still good - refers to CampaignView > object > > new Ajax.Request(''url.aspx'', > { > method: ''post'', > onSuccess: function(resp) > { > var d=this; //binding is lost - refers to windowWhat "binding"? The value of a function''s this keyword is set by the calling function. It will be one of: the object that the function is called as a method of, whatever is set using call() or apply(), or the window object. What do you want it to refer to? -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey, eggie5 a écrit :> onSuccess: function(resp) > { > var d=this; //binding is lost - refers to window > }Which is exactly why all Prototype docs tell you to bind any function you''re not calling directly over its "this" object, *or* save the reference externally and use it later. Solution A: new Ajax.Request(''url.aspx'', { onSuccess: function(resp) { this.... }.bind(theObjectYouWantAsThisInThere) }); Solution B: var view = this; new Ajax.Request(''url.aspx'', { onSuccess: function(resp) { view.... // instead of "this...." } }); -- Christophe Porteneuve aka TDD 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 -~----------~----~----~----~------~----~------~--~---
OH ok, I guess I just had that fundamental misunderstanding. On May 30, 1:10 am, RobG <r...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> On May 30, 5:52 pm, eggie5 <egg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I''m using the prototype Ajax.Request with an anonymous functions > > inside of a javascript object. However, once I get into the anonymous > > function of the Ajax.Request onSuccess, I loose the bindng to my > > object and this now refers to window instead of my object. > > > How can I maintain binding from within the anonymous function? > > > Code snippet: > > > CampaignView.prototype.DataBind=function(event) > > { > > var c=this; //binding is still good - refers to CampaignView > > object > > > new Ajax.Request(''url.aspx'', > > { > > method: ''post'', > > onSuccess: function(resp) > > { > > var d=this; //binding is lost - refers to window > > What "binding"? The value of a function''s this keyword is set by the > calling function. It will be one of: the object that the function is > called as a method of, whatever is set using call() or apply(), or the > window object. > > What do you want it to refer to? > > -- > Rob--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---