bpickens-53BQRuF0lHYybS5Ee8rs3A@public.gmane.org
2008-May-16 21:45 UTC
dealing with the scope change in an onclick event assignment
Hi guys. My problem before dealt with variable values rather than function access. Here I''m creating an onclick handler, which I want to execute a sibling function within the object that created it. I''ve got an object with several methods, like so: object { addArticle () removeArticle() addElement() } Inside of my addElement I have an onclick assignment like so: deletef = function(id) { // create <img> and <a> elements... deleteLink.observe(''click'',function(id){this.removeArticle(id);}); <--- return deleteLink; div.appendChild(deletef(id)); I know that the problem is that when I click the link, the function is now executing in the scope of the <a> element, not in that function. I tried to assign ''this'' to a variable and pass that through, but that didn''t seem to work either. ie. addElement (id) { var scope = this; deletef = function(id,scope) { // create <img> and <a> elements... deleteLink.observe(''click'',function(id,scope) {scope.removeArticle(id);}); <--- } } Is there a way for me to preserve the scope from when It was originally created? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
bpickens-53BQRuF0lHYybS5Ee8rs3A@public.gmane.org
2008-May-16 21:58 UTC
Re: dealing with the scope change in an onclick event assignment
whoa, sorry guys. the answer was right under my nose in the form of the bind() function... hehe, chalk up another useless post for the new guy. :) On May 16, 4:45 pm, "bpick...-53BQRuF0lHYybS5Ee8rs3A@public.gmane.org" <slim...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi guys. > My problem before dealt with variable values rather than function > access. Here I''m creating an onclick handler, which I want to execute > a sibling function within the object that created it. I''ve got an > object with several methods, like so: > > object { > addArticle () > removeArticle() > addElement() > > } > > Inside of my addElement I have an onclick assignment like so: > > deletef = function(id) { > // create <img> and <a> elements... > deleteLink.observe(''click'',function(id){this.removeArticle(id);}); > <--- > return deleteLink; > div.appendChild(deletef(id)); > > I know that the problem is that when I click the link, the function is > now executing in the scope of the <a> element, not in that function. I > tried to assign ''this'' to a variable and pass that through, but that > didn''t seem to work either. ie. > > addElement (id) { > var scope = this; > > deletef = function(id,scope) { > // create <img> and <a> elements... > deleteLink.observe(''click'',function(id,scope) > {scope.removeArticle(id);}); <--- > } > > } > > Is there a way for me to preserve the scope from when It was > originally created?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---