Hello, please help me with this: Thank you! var foo = Class.create(); foo.prototype = { testVar: null, script: ''/script.foo.php'', initialize: function(){}, sendRequest: function(){ textVar = "test string."; new Ajax.Request(this.script, {onSuccess:this.handleResponse}); }, handleResponse: function(response){ // I can''t access foo''s properties or methods from within // here after called from onSuccess. alert(this.textVar); // returns undefined instead of "test string." alert(this); // alert(this) returns [object Window] in Firefox and [object] in IE6 // instead of [object Object] as is foo. } }; --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Use bind: new Ajax.Request(this.script, {onSuccess:this.handleResponse.bind(this)}); http://prototypejs.org/api/function Colin SAR wrote:> Hello, please help me with this: > Thank you! > > var foo = Class.create(); > foo.prototype = { > testVar: null, > script: ''/script.foo.php'', > initialize: function(){}, > sendRequest: function(){ > textVar = "test string."; > new Ajax.Request(this.script, {onSuccess:this.handleResponse}); > }, > handleResponse: function(response){ > // I can''t access foo''s properties or methods from within > // here after called from onSuccess. > alert(this.textVar); // returns undefined instead of "test string." > alert(this); > // alert(this) returns [object Window] in Firefox and [object] in > IE6 > // instead of [object Object] as is foo. > } > }; > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 was the solution! I also realized that referencing the object outside the call would work as well: var thisObject = this; new Ajax.Request(this.script, {onSuccess:function(response) {thisObject.handleXMLResponse(response);}}); But your solution was the one I was looking for for a while! Thanks! On Mar 5, 1:15 am, Colin Mollenhour <eliteii...-NPSFNn/7+NYVo650/ln6uw@public.gmane.org> wrote:> Use bind: > > new Ajax.Request(this.script, {onSuccess:this.handleResponse.bind(this)}); > > http://prototypejs.org/api/function > > Colin > > SAR wrote: > > Hello, please help me with this: > > Thank you! > > > var foo = Class.create(); > > foo.prototype = { > > testVar: null, > > script: ''/script.foo.php'', > > initialize: function(){}, > > sendRequest: function(){ > > textVar = "test string."; > > new Ajax.Request(this.script, {onSuccess:this.handleResponse}); > > }, > > handleResponse: function(response){ > > // I can''t access foo''s properties or methods from within > > // here after called from onSuccess. > > alert(this.textVar); // returns undefined instead of "test string." > > alert(this); > > // alert(this) returns [object Window] in Firefox and [object] in > > IE6 > > // instead of [object Object] as is foo. > > } > > };--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Christophe Porteneuve
2007-Mar-05 08:14 UTC
Re: Get back into object''s scope after onSuccess?
Hey there, SAR a écrit :> var foo = Class.create(); > foo.prototype = {Aside from Colin''s answer (the usual bind issue), note that you''re stepping on your own foot here: if you use Class.create, you do so to leverage an initialize function in your prototype when the object is created. So instead of rewriting your whole prototype like this, you should go: Object.extend(foo.prototype, { ... }); JM2C, -- 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 -~----------~----~----~----~------~----~------~--~---
Christophe Porteneuve wrote:> Aside from Colin''s answer (the usual bind issue), note that you''re > stepping on your own foot here: if you use Class.create, you do so to > leverage an initialize function in your prototype when the object is > created. So instead of rewriting your whole prototype like this, you > should go: > > Object.extend(foo.prototype, { > ... > }); > > JM2C, >Actually there is no reason to do this. initialize is never a property of prototype until you make it one. Class.create returns a function that will call this.initialize and no properties of the prototype of that function are set beforehand so the YourClass.prototype = {...} works just fine and looks prettier :) Colin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Christophe Porteneuve
2007-Mar-05 09:51 UTC
Re: Get back into object''s scope after onSuccess?
Hey Colin! Colin Mollenhour a écrit :> will call this.initialize and no properties of the prototype of that > function are set beforehand so the YourClass.prototype = {...} works > just fine and looks prettier :)--> D''oh! Of course. That''s what you get for posting while waking up in front of your tea mug... -- 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 -~----------~----~----~----~------~----~------~--~---