hey all, im having a problem: var a = Class.create({ initialize: function(s) { this.s = s; $(''some_element'').observe(''keyup'',handler); }, handler: function(event) { console.log(this.s); } }); this is just a quick example of the problem, not the actual code im using. for some reason, the reference to this.s, is not being passed to handler(). i get console output "undefined", iv done alot of OOP in my time, and i cant see why this isnt working, any help would be great! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
It seems to me that it''s a matter of which element ''this'' refers to. In this case, when the ''keyup'' fires, ''this'' will refer to $ (''some_element''), not to the element you probably expected. Take a look at http://www.prototypejs.org/api/function and try this: $(''some_element'').observe(''keyup'', this.handler.bind(this)); If anyone has a more elegant way to solve this, please let me know. Fabio xzyfer escreveu:> hey all, > > im having a problem: > > var a = Class.create({ > > initialize: function(s) { > this.s = s; > $(''some_element'').observe(''keyup'',handler); > }, > > handler: function(event) > { > console.log(this.s); > } > > }); > > this is just a quick example of the problem, not the actual code im > using. > > for some reason, the reference to this.s, is not being passed to > handler(). > i get console output "undefined", iv done alot of OOP in my time, and > i cant see why this isnt working, > > any help would be great!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
quick and dirty solution is: $(''someElement'').observe(''keyup'', this.handler.bindAsEventListener(this)); Note both that you have to say "this.handler"... and you also have to do ".bindAsEventListener(this)". .bind(this) would work also if the handler wasn''t expecting the event object to be passed in, but as it is you need the former. Long term better way to do class creation and have truly instance only members (and true private/public accessors) though, is to encapsulate everything that''s supposed to be instance only inside the constructor. This goes a bit against the grain as far as what most prototype users are accustomed to though. You can read more in my blog ( www.someElement.com)... The post about multiple inheritance is the one where I explain this a bit. The way you have written this is what I call "prototype static". While you can say "well I just wouldn''t do this".. it''s entirely possibly to later write the static line " a.prototype.handler(something)"... which would cause errors. I know... a lot of people will argue that my flavor of encapsulation isn''t truly necessary, but that''s my opinion anyway, take it for what it''s worth :) I prefer true access level definitions on class members vs. pseudo. On 11/21/07, xzyfer <xzyfer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > wrote:> > > hey all, > > im having a problem: > > var a = Class.create({ > > initialize: function(s) { > this.s = s; > $(''some_element'').observe(''keyup'',handler); > }, > > handler: function(event) > { > console.log(this.s); > } > > }); > > this is just a quick example of the problem, not the actual code im > using. > > for some reason, the reference to this.s, is not being passed to > handler(). > i get console output "undefined", iv done alot of OOP in my time, and > i cant see why this isnt working, > > any help would be great! > > >-- Ryan Gahl Manager, Senior Software Engineer Nth Penguin, LLC http://www.nthpenguin.com -- Architect WebWidgetry.com / MashupStudio.com Future Home of the World''s First Complete Web Platform -- Inquire: 1-262-951-6727 Blog: http://www.someElement.com LinkedIn Profile: http://www.linkedin.com/in/ryangahl --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
i solved the problem about an hour ago, you were correct though. since v1.6 there is auto binding, meaning $ (''some_element'').observe(''event'', handler); automatically sets ''this'' to the referencing object, in this case $ (''some_element''). the way i fixed it was to go back to the traditional Event.observe($ (''some_element''),''event'', function(event) { handler(event); } ); so no binding is neccessary, its not all that elegant, but i couldnt manage to pass the event to my handler directly with wrapping it in the anonymous function. --~--~---------~--~----~------------~-------~--~----~ 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 ryan for your insight on bindAsEventListener(this). thats exactly what was required. as for your thoughts on encapsulation, im on your side, im an avid believer in proper OOP design principles, however i have ofter found it hard to translate the style or OOP from java, python and the like to javascript. from alot of what i have read, classes are a fickle thing in javascript. very hard to find decent reading material. On Nov 22, 6:32 am, xzyfer <xzy...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> i solved the problem about an hour ago, you were correct though. > since v1.6 there is auto binding, meaning $ > (''some_element'').observe(''event'', handler); > automatically sets ''this'' to the referencing object, in this case $ > (''some_element''). > > the way i fixed it was to go back to the traditional Event.observe($ > (''some_element''),''event'', function(event) { handler(event); } ); > so no binding is neccessary, its not all that elegant, but i couldnt > manage to pass the event to my handler directly with wrapping it in > the anonymous function.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---