Hi all, I was hoping someone might be able to help me out with a problem I''ve run into: I have an event listener bound to a form input field, e.g. myInputField.observe(''change'', this.onFieldChange); Because the callback function has to be run with this set to the function''s parent object, I used bindAsEventHandler like so: myInputField.observe(''change'', this.onFieldChange(this)); The problem, however, is that I''m not sure now how to pass the new value along. I can see the new value inside an anonymous function, e.g. myInputField.observe(''change'', function() { console.log(this.value); }); But that Is as far as I''ve gotten... Any suggestions would be greatly appreciated. Thanks all, Keith --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Got it :) In case anyone is interested, I was able to solve the problem using a closure: var self = this; myInputField.observe(''change'', function() {self.onFieldChange(this.value);}); Take care, Keith On Mar 10, 12:22 pm, Keith <keith.hugh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > I was hoping someone might be able to help me out with a problem I''ve > run into: > > I have an event listener bound to a form input field, e.g. > > myInputField.observe(''change'', this.onFieldChange); > > Because the callback function has to be run with this set to the > function''s > parent object, I used bindAsEventHandler like so: > > myInputField.observe(''change'', this.onFieldChange(this)); > > The problem, however, is that I''m not sure now how to pass the new > value along. > I can see the new value inside an anonymous function, e.g. > > myInputField.observe(''change'', function() > { console.log(this.value); }); > > But that Is as far as I''ve gotten... > > Any suggestions would be greatly appreciated. > > Thanks all, > Keith--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Keith, Where is "value" declared? Because it sure looks like you''re referencing a property of the global object from within your event handler. Event handlers are called with "this" referencing the global object. So I''m wondering if there''s an underlying problem and the workaround you found is just hiding that... BTW, if you really want "this" to be the same as where you''re declaring the event handler, you were right in the first place that bindAsEventListener is what you''re looking for; a quick stab at it suggests: myInputField.observe(''change'', (function() { this.onFieldChange(this.value); }).bindAsEventListener(this) ); FWIW... -- T.J. Crowder tj / crowder software / com On Mar 10, 6:12 pm, Keith <keith.hugh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Got it :) > > In case anyone is interested, I was able to solve the problem using a > closure: > > var self = this; > myInputField.observe(''change'', function() > {self.onFieldChange(this.value);}); > > Take care, > Keith > > On Mar 10, 12:22 pm, Keith <keith.hugh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi all, > > > I was hoping someone might be able to help me out with a problem I''ve > > run into: > > > I have an event listener bound to a form input field, e.g. > > > myInputField.observe(''change'', this.onFieldChange); > > > Because the callback function has to be run with this set to the > > function''s > > parent object, I used bindAsEventHandler like so: > > > myInputField.observe(''change'', this.onFieldChange(this)); > > > The problem, however, is that I''m not sure now how to pass the new > > value along. > > I can see the new value inside an anonymous function, e.g. > > > myInputField.observe(''change'', function() > > { console.log(this.value); }); > > > But that Is as far as I''ve gotten... > > > Any suggestions would be greatly appreciated. > > > Thanks all, > > Keith--~--~---------~--~----~------------~-------~--~----~ 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 T.J., "value" here represents the HTML "value" attribute associated with an input element, and Is created on the fly using prototype''s Element constructor: var myInputField = new Element(''input'', {size:''2'', value:startValue}); This all takes place within a method that sites in the same class as the event handler that is called. Combining bindAsEventListener with an anonymous function is a neat approach which I overlooked but it doesn''t work in this case because value is not in the same scope as the onFieldChange method. Thanks for suggestions :) Keith On Mar 10, 8:39 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Keith, > > Where is "value" declared? Because it sure looks like you''re > referencing a property of the global object from within your event > handler. Event handlers are called with "this" referencing the global > object. So I''m wondering if there''s an underlying problem and the > workaround you found is just hiding that... > > BTW, if you really want "this" to be the same as where you''re > declaring the event handler, you were right in the first place that > bindAsEventListener is what you''re looking for; a quick stab at it > suggests: > > myInputField.observe(''change'', (function() { > this.onFieldChange(this.value); > }).bindAsEventListener(this) > ); > > FWIW... > -- > T.J. Crowder > tj / crowder software / com > > On Mar 10, 6:12 pm, Keith <keith.hugh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Got it :) > > > In case anyone is interested, I was able to solve the problem using a > > closure: > > > var self = this; > > myInputField.observe(''change'', function() > > {self.onFieldChange(this.value);}); > > > Take care, > > Keith > > > On Mar 10, 12:22 pm, Keith <keith.hugh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hi all, > > > > I was hoping someone might be able to help me out with a problem I''ve > > > run into: > > > > I have an event listener bound to a form input field, e.g. > > > > myInputField.observe(''change'', this.onFieldChange); > > > > Because the callback function has to be run with this set to the > > > function''s > > > parent object, I used bindAsEventHandler like so: > > > > myInputField.observe(''change'', this.onFieldChange(this)); > > > > The problem, however, is that I''m not sure now how to pass the new > > > value along. > > > I can see the new value inside an anonymous function, e.g. > > > > myInputField.observe(''change'', function() > > > { console.log(this.value); }); > > > > But that Is as far as I''ve gotten... > > > > Any suggestions would be greatly appreciated. > > > > Thanks all, > > > Keith--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---