Hello, all. I''m trying to create a class using prototype that uses an Ajax call to set its initial attributes. I''m having trouble with the timing of things, and may do a better job of explaining this through an example. If the syntax is slightly off, I apologize - but I think the main problem has to do with my approach: <script type="text/javascript"> var Library = Class.create({ initialize: function() { this.books = new Array(); var url = "library.xml"; Ajax.Request(url, { method: get, onSuccess: this.load } }; }, load: function(t) { // stuff to load the books array } }); var lib = new Library(); alert(lib.books[0]); </script> When I run the last two lines, the alert returns "undefined". After doing some research on when things are being called, it appears that the script isn''t waiting for the load() method to finish before trying to access the first item in the books array. If I add a setTimeout() with a decent amount of time before calling the alert, things work, but that''s not an ideal solution. Am I approaching this problem the wrong way? Or am I just missing an important step to make sure that things are loaded before I''m referring to them? Is there an event I can listen to for when a method (in this case, load()) is finished running? Thanks in advance! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
bsweigard your code is correct but you are fetching the data in the wrong way. First, you are making a Ajax call, so it''s an asynchronous call , so you can''t get the data before until the call was complete, the callback for the Ajax call is onSuccess method when the call have finished. when this call is done, and you has populate your Array, that''s where you must to try get the data not before. The Event you are looking for is the onSuccess method. A good idea is make the alert or something inside the "load" method. I hope you understand. Cheers CZ> Hello, all. > > I''m trying to create a class using prototype that uses an Ajax call to > set its initial attributes. I''m having trouble with the timing of > things, and may do a better job of explaining this through an > example. If the syntax is slightly off, I apologize - but I think the > main problem has to do with my approach: > > <script type="text/javascript"> > > var Library = Class.create({ > > initialize: function() { > this.books = new Array(); > var url = "library.xml"; > Ajax.Request(url, { > method: get, > onSuccess: this.load > } > }; > }, > > load: function(t) { > // stuff to load the books array > } > > }); > > var lib = new Library(); > alert(lib.books[0]); > > </script> > > > When I run the last two lines, the alert returns "undefined". After > doing some research on when things are being called, it appears that > the script isn''t waiting for the load() method to finish before > trying to access the first item in the books array. If I add a > setTimeout() with a decent amount of time before calling the alert, > things work, but that''s not an ideal solution. > > Am I approaching this problem the wrong way? Or am I just missing an > important step to make sure that things are loaded before I''m > referring to them? Is there an event I can listen to for when a > method (in this case, load()) is finished running? > > Thanks in advance! > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You just have to remember that Ajax.Request runs asynchronously (by default) and that your code keeps executing while this request is in a progress. By the time you alert "lib.books[0]" it is most likely that no results have been received from the server. That''s exactly why it returns undefined. There are few ways to work around it - you could fire an event when request is completed (and have other methods "listen" to it); or you could let method accept a callback function as one of its arguments (and then invoke it internally). Hope this helps, kangax On Mar 20, 7:59 pm, bsweigard <bsweig...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, all. > > I''m trying to create a class using prototype that uses an Ajax call to > set its initial attributes. I''m having trouble with the timing of > things, and may do a better job of explaining this through an > example. If the syntax is slightly off, I apologize - but I think the > main problem has to do with my approach: > > <script type="text/javascript"> > > var Library = Class.create({ > > initialize: function() { > this.books = new Array(); > var url = "library.xml"; > Ajax.Request(url, { > method: get, > onSuccess: this.load > } > }; > }, > > load: function(t) { > // stuff to load the books array > } > > }); > > var lib = new Library(); > alert(lib.books[0]); > > </script> > > When I run the last two lines, the alert returns "undefined". After > doing some research on when things are being called, it appears that > the script isn''t waiting for the load() method to finish before > trying to access the first item in the books array. If I add a > setTimeout() with a decent amount of time before calling the alert, > things work, but that''s not an ideal solution. > > Am I approaching this problem the wrong way? Or am I just missing an > important step to make sure that things are loaded before I''m > referring to them? Is there an event I can listen to for when a > method (in this case, load()) is finished running? > > Thanks in advance!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tried changing, Ajax.Request(url, { method: get, onSuccess: this.load } to, Ajax.Request(url, { asynchronous: false, method: get, onSuccess: this.load } It''s ugly but in your case more or less ok. I would however write a function that creates the class by passing the parameters given by the ajax request instead, that would be much more practical and activity indicators would make more sense. /Jonas On 21 Mar, 00:59, bsweigard <bsweig...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, all. > > I''m trying to create a class using prototype that uses an Ajax call to > set its initial attributes. I''m having trouble with the timing of > things, and may do a better job of explaining this through an > example. If the syntax is slightly off, I apologize - but I think the > main problem has to do with my approach: > > <script type="text/javascript"> > > var Library = Class.create({ > > initialize: function() { > this.books = new Array(); > var url = "library.xml"; > Ajax.Request(url, { > method: get, > onSuccess: this.load > } > }; > }, > > load: function(t) { > // stuff to load the books array > } > > }); > > var lib = new Library(); > alert(lib.books[0]); > > </script> > > When I run the last two lines, the alert returns "undefined". After > doing some research on when things are being called, it appears that > the script isn''t waiting for the load() method to finish before > trying to access the first item in the books array. If I add a > setTimeout() with a decent amount of time before calling the alert, > things work, but that''s not an ideal solution. > > Am I approaching this problem the wrong way? Or am I just missing an > important step to make sure that things are loaded before I''m > referring to them? Is there an event I can listen to for when a > method (in this case, load()) is finished running? > > Thanks in advance!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
http://pastie.caboo.se/168649 That''s the quick-and-dirty way of using custom events to solve this problem. A more refined approach would be to ensure the handler is cleaned up after it''s done -- so that it doesn''t get called twice if you declare a second instance of Library. Cheers, Andrew --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you to all of you for your responses. I hadn''t stumbled upon the Element#fire() method before, but I have a feeling I''ll be seeing a lot of it now. Thanks, again. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---