Hello, I am making a ajax call from an object I defined but I am unable to change the local variables. Look at this code: var TestClass= Class.create(); TestClass.prototype = { // constructor initialize: function(resultDiv) { this.output = resultDiv; // Div to output current value this.test = 0; searchAjax(0,100); alert(this.test); }, searchAjax: function(start,end) { var url = ''http://localhost:8080/search.php''; var pars = ''action=0''; var myAjax = new Ajax.Request( url, { method: ''get'', parameters: pars, onComplete: this.searchResponse.bind(this), }); }, searchResponse: function(originalRequest) { this.test originalRequest.responseText; this.output.innerHTML = originalRequest.responseText + " clones"; } }; In searchResponse I am unable to change this.test. originalRequest.responseText just containes a number. this.output is a HTMLDivElement and works fine but the alert after searchAjax() does not change from 0. Anybody have any idea? I am hoping it is just a simple scoping problem I am overlooking? Thanks, Emmanuel --~--~---------~--~----~------------~-------~--~----~ 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 Emmanuel, Emmanuel a écrit :> initialize: function(resultDiv) { > this.output = resultDiv; // Div to output current > this.test = 0; > searchAjax(0,100); > alert(this.test); > this.output is a HTMLDivElement and works fine but the alert after > searchAjax() does not change from 0.Your AJAX request is, as it should be, asynchronous. When you alert, your onComplete handler hasn''t triggered yet. Put your alert in the handler, after setting this.test... Also, if you intend to manipulate this as a number, explicitely parse your text in the handler: this.test = parseInt(originalRequest.responseText, 10); ''HTH -- 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 -~----------~----~----~----~------~----~------~--~---
Hello Christophe! You are right! It works fine when I put the alert inside the onComplete handler but What I am trying to do here is set the local variables with information from the Ajax request so that I can use it in the rest of my Ajax application. For example, in my HTML page I have: var thetest = new TestClass($(''resultDiv'')); alert(thetest.test); But thetest.test = 0 and not what is supposed to be there. Thanks for your help. Emmanuel On Apr 16, 11:16 pm, Christophe Porteneuve <t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> Hey Emmanuel, > > Emmanuel a écrit : > > > initialize: function(resultDiv) { > > this.output = resultDiv; // Div to output current > > this.test = 0; > > searchAjax(0,100); > > alert(this.test); > > this.output is a HTMLDivElement and works fine but the alert after > > searchAjax() does not change from 0. > > Your AJAX request is, as it should be, asynchronous. When you alert, > your onComplete handler hasn''t triggered yet. Put your alert in the > handler, after setting this.test... > > Also, if you intend to manipulate this as a number, explicitely parse > your text in the handler: > > this.test = parseInt(originalRequest.responseText, 10); > > ''HTH > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Have you tried binding the function for your onComplete action to the object itself? -- Dash -- Emmanuel wrote:> Hello Christophe! > You are right! It works fine when I put the alert inside the > onComplete handler but > What I am trying to do here is set the local variables with > information from the Ajax request so that > I can use it in the rest of my Ajax application. For example, in my > HTML page I have: > var thetest = new TestClass($(''resultDiv'')); > alert(thetest.test); > > But thetest.test = 0 and not what is supposed to be there. > > Thanks for your help. > Emmanuel > > On Apr 16, 11:16 pm, Christophe Porteneuve <t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote: > >> Hey Emmanuel, >> >> Emmanuel a écrit : >> >> >>> initialize: function(resultDiv) { >>> this.output = resultDiv; // Div to output current >>> this.test = 0; >>> searchAjax(0,100); >>> alert(this.test); >>> this.output is a HTMLDivElement and works fine but the alert after >>> searchAjax() does not change from 0. >>> >> Your AJAX request is, as it should be, asynchronous. When you alert, >> your onComplete handler hasn''t triggered yet. Put your alert in the >> handler, after setting this.test... >> >> Also, if you intend to manipulate this as a number, explicitely parse >> your text in the handler: >> >> this.test = parseInt(originalRequest.responseText, 10); >> >> ''HTH >> >> -- >> 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 -~----------~----~----~----~------~----~------~--~---
Hello David!! I think I did that but the problem is that I was trying to retrieve information that was not returned by the Ajax object yet thus not setting it''s variables! If I just wait a lil bit for the response to return and then try to access the local variables it works! Asynchronization!! Thanks! Emmanuel --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---