Hi, I''ve been trying to do my javascript code "right", or at least more elegantly than a bunch of global variables and functions. That in mind, I am trying to use the Class system from Prototype. It''s kind of alien to me, as it''s got a few weird things that I''m not used to (being more used to the C++/Java models) Basically I have a ship object that has some data, and that ship owns an object that represents that ships batteries (gun batteries). So a few things I''m not quite sure on. The first is having the object load itself. I had all sorts of problems on my first attempt - the method to load itself was calling Ajax.Request with an onSuccess method - when the onSuccess method fired, the anon function no longer had the right context (the owning Ship object).. I tried it another way (calling a method of the object) and that didn''t work either. I had to break it out into a global function, which is far from ideal. What''s the proper way to do that? --~--~---------~--~----~------------~-------~--~----~ 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, From your description, you may want to look at the "bind" method that Prototype adds to functions: http://www.prototypejs.org/api/function#method-bind I think that might be what''s missing. If that doesn''t do it for you, perhaps post a small cut-down example page showing what you''re trying to do and where it''s failing? Hope this helps, -- T.J. Crowder tj / crowder software / com On Feb 21, 8:14 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> Hi, > > I''ve been trying to do my javascript code "right", or at least more > elegantly than a bunch of global variables and functions. That in > mind, I am trying to use the Class system from Prototype. It''s kind of > alien to me, as it''s got a few weird things that I''m not used to > (being more used to the C++/Java models) > > Basically I have a ship object that has some data, and that ship owns > an object that represents that ships batteries (gun batteries). > > So a few things I''m not quite sure on. The first is having the object > load itself. I had all sorts of problems on my first attempt - the > method to load itself was calling Ajax.Request with an onSuccess > method - when the onSuccess method fired, the anon function no longer > had the right context (the owning Ship object).. I tried it another > way (calling a method of the object) and that didn''t work either. I > had to break it out into a global function, which is far from ideal. > What''s the proper way to do that?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yep, it sure did. adding .bindAsEventListener(this) onto the end of the onSuccess anon function fixed that right up. Thanks much. On Feb 21, 2:11 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > From your description, you may want to look at the "bind" method that > Prototype adds to functions:http://www.prototypejs.org/api/function#method-bind > I think that might be what''s missing. > > If that doesn''t do it for you, perhaps post a small cut-down example > page showing what you''re trying to do and where it''s failing? > > Hope this helps, > -- > T.J. Crowder > tj / crowder software / com > > On Feb 21, 8:14 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > Hi, > > > I''ve been trying to do my javascript code "right", or at least more > > elegantly than a bunch of global variables and functions. That in > > mind, I am trying to use the Class system from Prototype. It''s kind of > > alien to me, as it''s got a few weird things that I''m not used to > > (being more used to the C++/Java models) > > > Basically I have a ship object that has some data, and that ship owns > > an object that represents that ships batteries (gun batteries). > > > So a few things I''m not quite sure on. The first is having the object > > load itself. I had all sorts of problems on my first attempt - the > > method to load itself was calling Ajax.Request with an onSuccess > > method - when the onSuccess method fired, the anon function no longer > > had the right context (the owning Ship object).. I tried it another > > way (calling a method of the object) and that didn''t work either. I > > had to break it out into a global function, which is far from ideal. > > What''s the proper way to do that?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Glad that helped! I think bindAsEventListener() is mostly intended for events such as those from DOM elements. Did you try bind(), or am I missing something? (I''m relatively new.) -- T.J. Crowder tj / crowder software / com On Feb 21, 11:27 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> Yep, it sure did. adding .bindAsEventListener(this) onto the end of > the onSuccess anon function fixed that right up. > > Thanks much. > > On Feb 21, 2:11 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > From your description, you may want to look at the "bind" method that > > Prototype adds to functions:http://www.prototypejs.org/api/function#method-bind > > I think that might be what''s missing. > > > If that doesn''t do it for you, perhaps post a small cut-down example > > page showing what you''re trying to do and where it''s failing? > > > Hope this helps, > > -- > > T.J. Crowder > > tj / crowder software / com > > > On Feb 21, 8:14 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > > Hi, > > > > I''ve been trying to do my javascript code "right", or at least more > > > elegantly than a bunch of global variables and functions. That in > > > mind, I am trying to use the Class system from Prototype. It''s kind of > > > alien to me, as it''s got a few weird things that I''m not used to > > > (being more used to the C++/Java models) > > > > Basically I have a ship object that has some data, and that ship owns > > > an object that represents that ships batteries (gun batteries). > > > > So a few things I''m not quite sure on. The first is having the object > > > load itself. I had all sorts of problems on my first attempt - the > > > method to load itself was calling Ajax.Request with an onSuccess > > > method - when the onSuccess method fired, the anon function no longer > > > had the right context (the owning Ship object).. I tried it another > > > way (calling a method of the object) and that didn''t work either. I > > > had to break it out into a global function, which is far from ideal. > > > What''s the proper way to do that?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Just did, they appear to work identically. On Feb 21, 3:37 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Glad that helped! I think bindAsEventListener() is mostly intended > for events such as those from DOM elements. Did you try bind(), or am > I missing something? (I''m relatively new.) > -- > T.J. Crowder > tj / crowder software / com > > On Feb 21, 11:27 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > Yep, it sure did. adding .bindAsEventListener(this) onto the end of > > the onSuccess anon function fixed that right up. > > > Thanks much. > > > On Feb 21, 2:11 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hi, > > > > From your description, you may want to look at the "bind" method that > > > Prototype adds to functions:http://www.prototypejs.org/api/function#method-bind > > > I think that might be what''s missing. > > > > If that doesn''t do it for you, perhaps post a small cut-down example > > > page showing what you''re trying to do and where it''s failing? > > > > Hope this helps, > > > -- > > > T.J. Crowder > > > tj / crowder software / com > > > > On Feb 21, 8:14 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > > > Hi, > > > > > I''ve been trying to do my javascript code "right", or at least more > > > > elegantly than a bunch of global variables and functions. That in > > > > mind, I am trying to use the Class system from Prototype. It''s kind of > > > > alien to me, as it''s got a few weird things that I''m not used to > > > > (being more used to the C++/Java models) > > > > > Basically I have a ship object that has some data, and that ship owns > > > > an object that represents that ships batteries (gun batteries). > > > > > So a few things I''m not quite sure on. The first is having the object > > > > load itself. I had all sorts of problems on my first attempt - the > > > > method to load itself was calling Ajax.Request with an onSuccess > > > > method - when the onSuccess method fired, the anon function no longer > > > > had the right context (the owning Ship object).. I tried it another > > > > way (calling a method of the object) and that didn''t work either. I > > > > had to break it out into a global function, which is far from ideal. > > > > What''s the proper way to do that?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Not quite, looking at the source bindAsEventListener is definitely for browser events as opposed to callbacks, it''s doing some explicit handling around the event param. FWIW, I''d go with bind(). -- T.J. Crowder tj / crowder software / com On Feb 21, 11:43 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> Just did, they appear to work identically. > > On Feb 21, 3:37 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Glad that helped! I think bindAsEventListener() is mostly intended > > for events such as those from DOM elements. Did you try bind(), or am > > I missing something? (I''m relatively new.) > > -- > > T.J. Crowder > > tj / crowder software / com > > > On Feb 21, 11:27 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > > Yep, it sure did. adding .bindAsEventListener(this) onto the end of > > > the onSuccess anon function fixed that right up. > > > > Thanks much. > > > > On Feb 21, 2:11 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi, > > > > > From your description, you may want to look at the "bind" method that > > > > Prototype adds to functions:http://www.prototypejs.org/api/function#method-bind > > > > I think that might be what''s missing. > > > > > If that doesn''t do it for you, perhaps post a small cut-down example > > > > page showing what you''re trying to do and where it''s failing? > > > > > Hope this helps, > > > > -- > > > > T.J. Crowder > > > > tj / crowder software / com > > > > > On Feb 21, 8:14 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > > > > Hi, > > > > > > I''ve been trying to do my javascript code "right", or at least more > > > > > elegantly than a bunch of global variables and functions. That in > > > > > mind, I am trying to use the Class system from Prototype. It''s kind of > > > > > alien to me, as it''s got a few weird things that I''m not used to > > > > > (being more used to the C++/Java models) > > > > > > Basically I have a ship object that has some data, and that ship owns > > > > > an object that represents that ships batteries (gun batteries). > > > > > > So a few things I''m not quite sure on. The first is having the object > > > > > load itself. I had all sorts of problems on my first attempt - the > > > > > method to load itself was calling Ajax.Request with an onSuccess > > > > > method - when the onSuccess method fired, the anon function no longer > > > > > had the right context (the owning Ship object).. I tried it another > > > > > way (calling a method of the object) and that didn''t work either. I > > > > > had to break it out into a global function, which is far from ideal. > > > > > What''s the proper way to do that?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The only difference between #bind and #bindAsEventListener is that the latter one makes sure event handler is invoked with event object as a first argument. - kangax On Feb 21, 8:17 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Not quite, looking at the source bindAsEventListener is definitely for > browser events as opposed to callbacks, it''s doing some explicit > handling around the event param. FWIW, I''d go with bind(). > -- > T.J. Crowder > tj / crowder software / com > > On Feb 21, 11:43 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> Just did, they appear to work identically. > > > On Feb 21, 3:37 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Glad that helped! I think bindAsEventListener() is mostly intended > > > for events such as those from DOM elements. Did you try bind(), or am > > > I missing something? (I''m relatively new.) > > > -- > > > T.J. Crowder > > > tj / crowder software / com > > > > On Feb 21, 11:27 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > > > Yep, it sure did. adding .bindAsEventListener(this) onto the end of > > > > the onSuccess anon function fixed that right up. > > > > > Thanks much. > > > > > On Feb 21, 2:11 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Hi, > > > > > > From your description, you may want to look at the "bind" method that > > > > > Prototype adds to functions:http://www.prototypejs.org/api/function#method-bind > > > > > I think that might be what''s missing. > > > > > > If that doesn''t do it for you, perhaps post a small cut-down example > > > > > page showing what you''re trying to do and where it''s failing? > > > > > > Hope this helps, > > > > > -- > > > > > T.J. Crowder > > > > > tj / crowder software / com > > > > > > On Feb 21, 8:14 pm, Backov <bac...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > > > > > Hi, > > > > > > > I''ve been trying to do my javascript code "right", or at least more > > > > > > elegantly than a bunch of global variables and functions. That in > > > > > > mind, I am trying to use the Class system from Prototype. It''s kind of > > > > > > alien to me, as it''s got a few weird things that I''m not used to > > > > > > (being more used to the C++/Java models) > > > > > > > Basically I have a ship object that has some data, and that ship owns > > > > > > an object that represents that ships batteries (gun batteries). > > > > > > > So a few things I''m not quite sure on. The first is having the object > > > > > > load itself. I had all sorts of problems on my first attempt - the > > > > > > method to load itself was calling Ajax.Request with an onSuccess > > > > > > method - when the onSuccess method fired, the anon function no longer > > > > > > had the right context (the owning Ship object).. I tried it another > > > > > > way (calling a method of the object) and that didn''t work either. I > > > > > > had to break it out into a global function, which is far from ideal. > > > > > > What''s the proper way to do that?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---