Micah Wedemeyer
2008-Feb-18 18:42 UTC
Adding new javascript function using Insertion (or Element.replace)
I''ve got a Rails partial with a javascript function (let''s say foo) defined inside. I''d like to be able to render this partial as part of an AJAX response which makes foo available to the rest of the javascript on the page. According to the prototype docs, scripts are evaluated, but I''m not sure what that means. I''ve tested with alert, and the alert box pops up. However, with functions, they don''t appear in the generated HTML, and cannot be accessed by function calls. Am I trying something crazy? Any ideas? Thanks, Micah --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Walter Lee Davis
2008-Feb-18 18:52 UTC
Re: Adding new javascript function using Insertion (or Element.replace)
Is this a normal function, like function foo(param,faram){ ... } or an anonymous function like foo = function(){ ... } ? The former won''t work, but the latter will. There''s a bit in the prototype API docs about this if you give a look. Walter On Feb 18, 2008, at 1:42 PM, Micah Wedemeyer wrote:> > I''ve got a Rails partial with a javascript function (let''s say foo) > defined inside. I''d like to be able to render this partial as part of > an AJAX response which makes foo available to the rest of the > javascript > on the page. > > According to the prototype docs, scripts are evaluated, but I''m not > sure > what that means. I''ve tested with alert, and the alert box pops up. > However, with functions, they don''t appear in the generated HTML, and > cannot be accessed by function calls. > > Am I trying something crazy? Any ideas? > > Thanks, > Micah > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Micah Wedemeyer
2008-Feb-18 19:01 UTC
Re: Adding new javascript function using Insertion (or Element.replace)
It''s a normal function. I''m a js newb, so I didn''t even consider anonymous functions. I''ll run through the docs again and see if I can find it. Thanks, Micah Walter Lee Davis wrote:> Is this a normal function, like function foo(param,faram){ ... } or > an anonymous function like foo = function(){ ... } ? The former won''t > work, but the latter will. > > There''s a bit in the prototype API docs about this if you give a look. > > Walter > > On Feb 18, 2008, at 1:42 PM, Micah Wedemeyer wrote: > > >> I''ve got a Rails partial with a javascript function (let''s say foo) >> defined inside. I''d like to be able to render this partial as part of >> an AJAX response which makes foo available to the rest of the >> javascript >> on the page. >> >> According to the prototype docs, scripts are evaluated, but I''m not >> sure >> what that means. I''ve tested with alert, and the alert box pops up. >> However, with functions, they don''t appear in the generated HTML, and >> cannot be accessed by function calls. >> >> Am I trying something crazy? Any ideas? >> >> Thanks, >> Micah >> >> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Justin Perkins
2008-Feb-18 19:09 UTC
Re: Adding new javascript function using Insertion (or Element.replace)
You might want to consider defining the function once in your JS file, then call it from the Rails partial with parameters instead of re-defining the function every time the partial is rendered. There was a thread about this recently, I seem to remember the guy in that thread having a similar problem to the one you are describing. -justin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mickster
2008-Feb-18 21:58 UTC
Re: Adding new javascript function using Insertion (or Element.replace)
I''m afraid I don''t have the solution, but I do have some input: I was trying to do exactly the same as what Micah is trying to do and I got it to work with Firefox, I think I used the syntax that Walter describes. But the problem was that it did not work in Internet Explorer... Can''t really explain why... I had to use the workarounds to either include it in the general JS file, as Justin suggests, or attach the code to an event in the code that is retrieved with the AJAX call - e.g: <div onclick="alert(''This is written when someone clicks this div''); alert(''this too'')">Click me</div> -Mickster On Feb 18, 8:09 pm, "Justin Perkins" <justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You might want to consider defining the function once in your JS file, > then call it from the Rails partial with parameters instead of > re-defining the function every time the partial is rendered. > > There was a thread about this recently, I seem to remember the guy in > that thread having a similar problem to the one you are describing. > > -justin--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---